A Multicast Wrapper Class

A multicast wrapper class has been requested by several of the visitors
at CodeGuru and a few months back I was one of them. Surprisingly, there
are very few resources on the Internet for multicast programming under
WinSock and I think that this class that I wrote for my project will come
in handy to some of you. The Code is amply commented and not too hard to
follow. Points worth mentioning are discussed below and if any help is
required don’t hesitate to contact me.

I will assume that you are aware of the concept of multicasting and
the socket API’s. The class CMulticastSocket derives from MFC CAsyncSocket
and will allow the programmer to use the following facilities:




    • Join a multicast host group given its multicast IP address and Port

    • Leave the host group

    • Send/receive data to/from the host group

    • Set the time to live (TTL) and Loopback options on the sending socket



Join a Group

To join a multicast group the following API call can be used

    Bool CMulticastSocket::JoinGroup (CString
HostGroupIP, UINT nHostGroupPort, UINT nTTL, BOOL nLoopback)

The function returns true if successful, otherwise false.

All fields are pretty self explanatory but, for those who may try to
go into the details of the code, I’ll mention a technical problem that
is faced with the loopback option. Not all multicast interfaces support
the loopback option in which case, the loopback is by default enabled.
Any packet sent to the host group will loop back to the sending interface.
In short, the sender will receive any packet that he sends whether he/she
likes it or not.

CMulticastSocket handles this problem internally. If the multicast
interface does not support loopback option and the user wants to disable
loopback, the class ignore any packets that it receives from the IP/Port
address of the sending socket of the user. This way, all messages sent
by you that loopback to the interface are ignored by the OnReceive function
of the class.

nTTL     contains the value of Time to live
for the packets. The TTL determine the number of hops of the packet can
make before it dies.

 

Leave a Group

To leave a multicast hostgroup that you are connected to use

    Bool CMulticastSocket::LeaveGroup()

Returns true if successful, otherwise false.

 

Send Data to host group

    Bool CMulticastSocket::SendTo(const char*
strMessage, int nSize)

Returns true if successful, otherwise false.

strMessage contains the Message to be sent.

nSize is the size of the Message

 

Receive Data from host group

    void CMulticastSocket::OnReceive(int nErrorCode)

This function overrides the CAsyncSocket OnReceive Notification Message.
Within this message, there are check to ensure loopback is working correctly.
The programmer can write his/her code in this function to handle the data
that is received.

 

Class Data Members

A description of the class data members is as follows. This should come
in particularly handy if you want to make changes to the code to suit your
needs.

 





















m_mrMReq The mreq structure containing the host group IP and interface
m_saHostGroup SOCKADDR_IN structure containing IP/Port of host group
m_SendSocket Another CAsyncSocket object for sending data to the host group
m_strLocalIP CString containing IP Address of the local machine (needed for forcing
no loopback)
m_nLocalPort UINT containing Port Number of the receiving socket (needed for forcing
no loopback)
m_strBuffer A character array containing the data received
m_strSendersIP CString containing IP of the Sender of data to the host group
m_nSendersPort UINT containing Port Number of the Sender of data to the host group
bForceNoLoopback Boolean indicating whether loopback has to be forcefully disabled or
not (don’t probe. For internal use only)
Data Members of CMulticastSocket

Downloads

Download Demo Project – 19 Kb

Download Source – 3Kb

Date Posted : June 28, 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read