Click here to Skip to main content
15,881,742 members
Articles / Desktop Programming / MFC
Article

A Multicast Wrapper Class

Rate me:
Please Sign up or sign in to vote.
4.91/5 (14 votes)
3 Dec 1999 195.4K   5.7K   58   35
  • Download demo project - 18 Kb
  • Download source files - 3 Kb
  • 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_mrMReqThe mreq structure containing the host group IP and interface
    m_saHostGroupSOCKADDR_IN structure containing IP/Port of host group
    m_SendSocketAnother CAsyncSocket object for sending data to the host group
    m_strLocalIPCString containing IP Address of the local machine (needed for forcing no loopback)
    m_nLocalPortUINT containing Port Number of the receiving socket (needed for forcing no loopback)
    m_strBufferA character array containing the data received
    m_strSendersIPCString containing IP of the Sender of data to the host group
    m_nSendersPortUINT containing Port Number of the Sender of data to the host group
    bForceNoLoopbackBoolean indicating whether loopback has to be forcefully disabled or not (don't probe. For internal use only)
    Data Members of CMulticastSocket

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Pakistan Pakistan
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionHow to make this work ? Pin
    ldaoust7-Nov-08 7:57
    ldaoust7-Nov-08 7:57 
    AnswerRe: How to make this work ? Pin
    ldaoust7-Nov-08 9:53
    ldaoust7-Nov-08 9:53 
    GeneralEnd task through Task Manager Pin
    mailtochandra2000@yahoo.com6-Feb-08 2:12
    mailtochandra2000@yahoo.com6-Feb-08 2:12 
    GeneralKeep alive Pin
    mailtochandra2000@yahoo.com19-Sep-07 2:59
    mailtochandra2000@yahoo.com19-Sep-07 2:59 
    GeneralRe: Keep alive Pin
    ESTAN28-Sep-07 1:34
    ESTAN28-Sep-07 1:34 
    Generalabout TTI and Loopback Pin
    mailtochandra2000@yahoo.com17-Sep-07 3:22
    mailtochandra2000@yahoo.com17-Sep-07 3:22 
    Generalmultipule joins and leave group Pin
    cabessa5-Jul-07 20:17
    cabessa5-Jul-07 20:17 
    QuestionLimitation on Received Stream Characters Pin
    kunaljvyas13-Jun-07 6:24
    kunaljvyas13-Jun-07 6:24 
    QuestionIP multicasting Pin
    xlcuo24-Dec-06 14:40
    xlcuo24-Dec-06 14:40 
    GeneralError Joining Host Group on NT4 Pin
    Mark222-Mar-06 10:23
    Mark222-Mar-06 10:23 
    GeneralWindows Firewall Pin
    DanBaker9-Mar-05 12:57
    DanBaker9-Mar-05 12:57 
    GeneralSend long message(>32,000 characters). Pin
    phamchicong6-Jun-04 23:16
    phamchicong6-Jun-04 23:16 
    GeneralRe: Send long message(>32,000 characters). Pin
    KarstenK6-Jun-04 23:54
    mveKarstenK6-Jun-04 23:54 
    GeneralMulticast Addresses Pin
    Nas18-Dec-03 0:43
    Nas18-Dec-03 0:43 
    Generalnot able to run Pin
    onlyrakesh1-Sep-03 3:19
    onlyrakesh1-Sep-03 3:19 
    QuestionHow create multicast group address Pin
    Nagareshwar9-Jun-03 3:11
    Nagareshwar9-Jun-03 3:11 
    AnswerRe: How create multicast group address Pin
    elhajjim16-Oct-04 17:28
    elhajjim16-Oct-04 17:28 
    GeneralMulticast View Pin
    ASHOK RAJ22-May-03 20:57
    ASHOK RAJ22-May-03 20:57 
    GeneralJoin multicast group ! Pin
    enachemc20-Nov-02 19:44
    enachemc20-Nov-02 19:44 
    GeneralRe: Join multicast group ! Pin
    Anonymous1-May-03 3:07
    Anonymous1-May-03 3:07 
    GeneralRe: Join multicast group ! Pin
    AntonDel1-Oct-03 3:50
    AntonDel1-Oct-03 3:50 
    GeneralMulticast and Multi-NIC computers Pin
    Naji Sourani21-Jan-02 0:03
    Naji Sourani21-Jan-02 0:03 
    GeneralRe: Multicast and Multi-NIC computers Pin
    11-Feb-02 0:31
    suss11-Feb-02 0:31 
    GeneralRe: Multicast and Multi-NIC computers Pin
    KS LAU4-Sep-02 19:24
    KS LAU4-Sep-02 19:24 
    GeneralDemo? HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
    10-Jan-02 14:22
    suss10-Jan-02 14:22 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.