Click here to Skip to main content
13,868,977 members
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 3 Dec 1999

A Multicast Wrapper Class

, 3 Dec 1999
Rate this:
Please Sign up or sign in to vote.
  • Download demo project - 18 Kb
  • Download source files - 3 Kb
  • <!-- Article Starts -->

    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
    <FONT SIZE=+1>

    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.

    <FONT SIZE=+1>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


    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


    About the Author

    No Biography provided

    You may also be interested in...

    Comments and Discussions

    QuestionHow to make this work ? Pin
    ldaoust7-Nov-08 8:57
    memberldaoust7-Nov-08 8:57 
    AnswerRe: How to make this work ? Pin
    ldaoust7-Nov-08 10:53
    memberldaoust7-Nov-08 10:53 
    GeneralEnd task through Task Manager Pin
    mailtochandra2000@yahoo.com6-Feb-08 3:12
    membermailtochandra2000@yahoo.com6-Feb-08 3:12 
    GeneralKeep alive Pin
    mailtochandra2000@yahoo.com19-Sep-07 3:59
    membermailtochandra2000@yahoo.com19-Sep-07 3:59 
    GeneralRe: Keep alive Pin
    ESTAN28-Sep-07 2:34
    memberESTAN28-Sep-07 2:34 
    Generalabout TTI and Loopback Pin
    mailtochandra2000@yahoo.com17-Sep-07 4:22
    membermailtochandra2000@yahoo.com17-Sep-07 4:22 
    Generalmultipule joins and leave group Pin
    cabessa5-Jul-07 21:17
    membercabessa5-Jul-07 21:17 
    QuestionLimitation on Received Stream Characters Pin
    kunaljvyas13-Jun-07 7:24
    memberkunaljvyas13-Jun-07 7:24 
    QuestionIP multicasting Pin
    xlcuo24-Dec-06 15:40
    memberxlcuo24-Dec-06 15:40 
    GeneralError Joining Host Group on NT4 Pin
    Mark222-Mar-06 11:23
    memberMark222-Mar-06 11:23 
    GeneralWindows Firewall Pin
    DanBaker9-Mar-05 13:57
    memberDanBaker9-Mar-05 13:57 
    GeneralSend long message(&gt;32,000 characters). Pin
    phamchicong7-Jun-04 0:16
    memberphamchicong7-Jun-04 0:16 
    GeneralRe: Send long message(&gt;32,000 characters). Pin
    KarstenK7-Jun-04 0:54
    mveKarstenK7-Jun-04 0:54 
    GeneralMulticast Addresses Pin
    Nas18-Dec-03 1:43
    memberNas18-Dec-03 1:43 
    Generalnot able to run Pin
    onlyrakesh1-Sep-03 4:19
    memberonlyrakesh1-Sep-03 4:19 
    QuestionHow create multicast group address Pin
    Nagareshwar9-Jun-03 4:11
    memberNagareshwar9-Jun-03 4:11 
    AnswerRe: How create multicast group address Pin
    elhajjim16-Oct-04 18:28
    memberelhajjim16-Oct-04 18:28 
    GeneralMulticast View Pin
    ASHOK RAJ22-May-03 21:57
    memberASHOK RAJ22-May-03 21:57 
    GeneralJoin multicast group ! Pin
    enachemc20-Nov-02 20:44
    memberenachemc20-Nov-02 20:44 
    GeneralRe: Join multicast group ! Pin
    Anonymous1-May-03 4:07
    memberAnonymous1-May-03 4:07 
    GeneralRe: Join multicast group ! Pin
    AntonDel1-Oct-03 4:50
    memberAntonDel1-Oct-03 4:50 
    GeneralMulticast and Multi-NIC computers Pin
    Naji Sourani21-Jan-02 1:03
    memberNaji Sourani21-Jan-02 1:03 
    GeneralRe: Multicast and Multi-NIC computers Pin
    11-Feb-02 1:31
    suss11-Feb-02 1:31 
    GeneralRe: Multicast and Multi-NIC computers Pin
    KS LAU4-Sep-02 20:24
    memberKS LAU4-Sep-02 20:24 
    GeneralDemo? HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
    10-Jan-02 15:22
    suss10-Jan-02 15: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.

    Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
    Web03 | 2.8.190214.1 | Last Updated 4 Dec 1999
    Article Copyright 1999 by Adeel Jamil Siddiqui
    Everything else Copyright © CodeProject, 1999-2019
    Layout: fixed | fluid