Click here to Skip to main content
15,886,362 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.6K   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 
    Sorry but the Previos Post was looking like garbage.

    first of All! Thanks for this App & Code.

    I am using the App. to Receive Multicast Data on Network.
    But it Fails.
    Reason:
    ------------------------------------------------------------------------
    ------| Hex Values | Char Values |
    ------------------------------------------------------------------------
    0020 04 20 00 01 00 00 ........... ....
    0030 04 59 5a 2a 2a 31 32 33 00 00 33 a1 92 2c 20 20 .YZ**123..3..,
    0040 1c 27 00 00 00 00 0f 65 5b 75 6e 5f 00 00 33 a1 .'.....e[un_..3.
    0050 92 2c 10 61 31 20 20 20 20 20 20 20 01 d8 00 06 .,.a1 ....
    0060 53 26 50 20 43 4e 58 20 4e 69 66 74 79 20 20 20 S&P CNX Nifty
    0070 20 20 20 20 20 00 00 06 46 a9 00 00 00 00 00 00 ...F.......
    0080 00 00 00 00 00 00 00 06 46 a9 ff ff ff 9b 00 06 ........F.......
    0090 a8 47 00 04 45 b1 00 00 00 00 00 00 00 00 43 1e .G..E.........C.
    00a0 07 63 f1 e0 b8 50 20 00 43 4e 58 20 49 54 20 20 .c...P .CNX IT
    00b0 20 20 20 20 20 20 20 20 20 20 20 20 20 00 00 08 ...
    00c0 0e 30 00 00 00 00 00 00 00 00 00 00 00 00 00 08 .0..............
    00d0 0e 30 ff ff ff ce 00 08 ef ee 00 05 59 f6 00 00 .0..........Y...
    00e0 00 00 00 00 00 00 42 f9 78 5c 4a ad 97 00 20 00 ......B.x\J... .
    00f0 43 4e 58 20 4e 69 66 74 79 20 4a 75 6e 69 6f 72 CNX Nifty Junior
    0100 20 20 20 20 20 00 00 0b fe 14 00 00 00 00 00 00 ...........
    0110 00 00 00 00 00 00 00 0b fe 14 ff ff ff da 00 0c ................
    0120 9c 84 00 07 39 d8 00 00 00 00 00 00 00 00 42 f3 ....9.........B.
    0130 f5 ce f4 40 61 a0 20 00 53 26 50 20 43 4e 58 20 ...@a. .S&P CNX
    0140 44 65 66 74 79 20 20 20 20 20 20 20 20 00 00 05 Defty ...
    0150 4e d9 00 00 00 00 00 00 00 00 00 00 00 00 00 05 N...............
    0160 4e d9 ff ff ff 59 00 05 b2 48 00 03 39 41 00 00 N....Y...H..9A..
    0170 00 00 00 00 00 00 42 c7 74 64 17 85 2c 44 20 00 ......B.td..,D .
    0180 42 41 4e 4b 20 4e 69 66 74 79 20 20 20 20 20 20 BANK Nifty
    0190 20 20 20 20 20 00 00 09 40 ac 00 00 00 00 00 00 ...@.......
    01a0 00 00 00 00 00 00 00 09 40 ac ff ff ff 74 00 0a ........@....t..
    01b0 10 81 00 05 35 66 00 00 00 00 00 00 00 00 42 ef ....5f........B.
    01c0 85 e7 1d 5f 77 bf 20 00 43 4e 58 20 4d 69 64 63 ..._w. .CNX Midc
    01d0 61 70 20 20 20 20 20 20 20 20 20 20 20 00 00 08 ap ...
    01e0 67 45 00 00 00 00 00 00 00 00 00 00 00 00 00 08 gE..............
    01f0 67 45 ff ff ff d7 00 08 be 6b 00 05 67 b1 00 00 gE.......k..g...
    0200 00 00 00 00 00 00 42 f8 05 cf 43 d2 03 40 20 00 ......B...C..@ .

    Above is the Ethereal Packet Capture.
    [BOLD] face is Data Field ( ignore the rest )
    -------------------------------------------------------
    So, when i run the App. it only Receives Hex[04] & [20] "uNDERLINED"
    & ignores the Rest of Data.
    -------------------------------------------------------
    Hex--> ASCII
    [04] --> EOT
    [20] --> space
    [00] --> NULL
    [01] --> SOH

    i will be highly obliged 4 your Help in Modification of the Code to Include all Data.

    and also length of String Buffer that i receive is "Null"
    the Chat window only Shows "" char

    Kunal J Vyas
    kunaljvyas@yahoo.com
    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.