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


Comments

  • Why I can not set loop back false???

    Posted by max2005 on 05/08/2005 05:52am

    I use JoinGroup(245.10.10.10, 9000, 8, FALSE),but can not set loop back false! Why ??? Please help me.

    Reply
  • Use it.... works great!

    Posted by Katharine on 02/02/2005 12:37pm

    I downloaded this class several years ago when we were porting our software from a VAX platform to a PC. On the VAX we used multicast addressing network protocol. I couldn't find a class to do that anywhere for the PC until I found this. I implemented it (with very few modifications) and it works great for our application. A belated thanks!

    Reply
  • how do I set group address

    Posted by Legacy on 04/30/2003 12:00am

    Originally posted by: cooker

    I have joined the group 224.0.0.1 and a port , it is doing fine across the LAN, but if I joined the group which the address is not equal 224.0.0.1, for example, 224.10.10.10, it doesnt work.
    
    why?

    • Slight flaw in the demo...

      Posted by roninttk on 09/26/2005 08:53pm

      In the demo, the author leaves the creation of the socket up the parent class .  This method creates the socket handle and binds the socket to given IP address and port.  But if you are trying to open multiple instances of this program on the same computer, the derived class will need to over-ride the method .  Between the calls to create the socket  and , you will need to set the SO_REUSEADDR option with .  This will allow more than one socket to connect to the same multicast IP and port.
      
      /////////////////////////////////////////////////////////////////////////////
      // CMulticastSocket member functions
      
      BOOL CMulticastSocket::Create(
              UINT nSocket, 
              int nSocketType, 
              long lEvent,
              LPCTSTR lpszSocketAddress)
      {
         BOOL bMultipleApps = TRUE;    /* allow reuse of local port if needed */
      
         // create a socket handle
         if( Socket(nSocketType, lEvent) )
         {
              // config for IP address reuse for multiple multicast connections
              if( SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET) ) {
                  // bind the socket to the interface
                  if( Bind(nSocket,lpszSocketAddress) ) {
                      return TRUE;
                  }
              }
      
              int nResult = GetLastError();
              Close();
              WSASetLastError(nResult);
         }
         return FALSE;
      
      } //end CMulticastSocket::Create(UINT, int, long, LPCTSTR)

      Reply
    • Slight flaw in the demo...

      Posted by roninttk on 09/26/2005 08:50pm

      In the demo, the author leaves the creation of the socket up the parent class . This method creates the socket handle and binds the socket to given IP address and port. But if you are trying to open multiple instances of this program on the same computer, the derived class will need to over-ride the method . Between the calls to create the socket and , you will need to set the SO_REUSEADDR option with . This will allow more than one socket to connect to the same multicast IP and port. ///////////////////////////////////////////////////////////////////////////// // CMulticastSocket member functions BOOL CMulticastSocket::Create( UINT nSocket, int nSocketType, long lEvent, LPCTSTR lpszSocketAddress) { BOOL bMultipleApps = TRUE; /* allow reuse of local port if needed */ // create a socket handle if( Socket(nSocketType, lEvent) ) { // config for IP address reuse for multiple multicast connections if( SetSockOpt(SO_REUSEADDR, (void*)&bMultipleApps, sizeof(BOOL), SOL_SOCKET) ) { // bind the socket to the interface if( Bind(nSocket,lpszSocketAddress) ) { return TRUE; } } int nResult = GetLastError(); Close(); WSASetLastError(nResult); } return FALSE; } //end CMulticastSocket::Create(UINT, int, long, LPCTSTR)

      Reply
    • 224.0.0.1 has special meaning

      Posted by roninttk on 09/20/2005 11:54am

      Packets sent to the IP address of 224.0.0.1 will be sent to all nodes on this subnet. Similarly, 224.0.0.2 replicates packets to all routers on this subnet. For a complete list of multicast address assignments, refer to RFC 1700.

      Reply
    Reply
  • I want to know how to program network traffic analysis software,please would you mind helping me ?

    Posted by Legacy on 03/30/2003 12:00am

    Originally posted by: berhanu

    Thanks
    

    Reply
  • Overriding OnReceive in COM object

    Posted by Legacy on 03/05/2003 12:00am

    Originally posted by: Scott

    Great wrapper! I'm new at this, but I'm having trouble overriding OnReceive in a COM object. The OnReceive method in the MulticastSocket object is called, but I can't get the event to "percolate" all the way up to the COM object. Any help appreciated!

    Reply
  • How To Multicast using two network interface cards (NICs) ?

    Posted by Legacy on 02/05/2003 12:00am

    Originally posted by: dotan

    hi all,
    here is the situation :
    i have one PC with 2 network cards which connected to 2 different networks.
    i want to tell my application each time thorugh which card (NIC) to send out (multicast) .
    today , my application multicasting on one NIC which windows pick up randomally... :-(
    how can i control this? can i multicast from both cards at the same time?
    please H E L P .... !
    tnx in advance...
    dotan.

    Reply
  • setSocket results in error 10055

    Posted by Legacy on 02/02/2003 12:00am

    Originally posted by: schnupferich

    I've got the same problem mentioned some months ago by someone else: set socket (joinGroup) results in the error 10055 and it's of course not due to some wrong IP address, that's simple. I have no problem with usual Tcp server/client programs though. Anybody an idea?

    Reply
  • Multicasting on one computer?

    Posted by Legacy on 07/30/2002 12:00am

    Originally posted by: Peter Ritchie

    Have you been able to get around the limitation of only being able to open one multicast socket per IP per computer?

    Reply
  • How will it work across WAN

    Posted by Legacy on 07/21/2002 12:00am

    Originally posted by: Sujit Mondal

    I have joined the group 224.0.0.1 and a port , it is doing fine across the LAN. But won't work across the WAN. Can you please let me know whether we can do it programmatically to work it across the WAN or need something to do with Reuters.
    If there is something to do with Reuters , what we need to do for it?

    Reply
  • How to start Multicasting !

    Posted by Legacy on 05/20/2002 12:00am

    Originally posted by: alfred

    recently i have read up some stuff about networking, and then have also touch on a bit on multicasting. I understand that there are a lot of info in the internet regarding multicasting and also have a basic idea of the multicasting protocols. However, interest will die off if its based on reading only. I do not have any idea of how to really implement it on my home computers. What do i need ? How should i start ? What really is the 1st step that i can go in the implementation side? I, thank you, for any form of reply from anyone.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds