Click here to Skip to main content
11,704,768 members (48,986 online)
Click here to Skip to main content

Tagged as

A Multicast Wrapper Class

, 3 Dec 1999 157.3K 4.7K 56
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

    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

    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

    Share

    About the Author

    No Biography provided

    You may also be interested in...

    Comments and Discussions

     
    QuestionHow to make this work ? Pin
    ldaoust7-Nov-08 7:57
    memberldaoust7-Nov-08 7:57 
    AnswerRe: How to make this work ? Pin
    ldaoust7-Nov-08 9:53
    memberldaoust7-Nov-08 9:53 
    GeneralEnd task through Task Manager Pin
    mailtochandra2000@yahoo.com6-Feb-08 2:12
    membermailtochandra2000@yahoo.com6-Feb-08 2:12 
    GeneralKeep alive Pin
    mailtochandra2000@yahoo.com19-Sep-07 2:59
    membermailtochandra2000@yahoo.com19-Sep-07 2:59 
    GeneralRe: Keep alive Pin
    ESTANNY28-Sep-07 1:34
    memberESTANNY28-Sep-07 1:34 
    Generalabout TTI and Loopback Pin
    mailtochandra2000@yahoo.com17-Sep-07 3:22
    membermailtochandra2000@yahoo.com17-Sep-07 3:22 
    Generalmultipule joins and leave group Pin
    cabessa5-Jul-07 20:17
    membercabessa5-Jul-07 20:17 
    QuestionLimitation on Received Stream Characters Pin
    kunaljvyas13-Jun-07 6:24
    memberkunaljvyas13-Jun-07 6:24 
    QuestionIP multicasting Pin
    xlcuo24-Dec-06 14:40
    memberxlcuo24-Dec-06 14:40 
    GeneralError Joining Host Group on NT4 Pin
    Mark222-Mar-06 10:23
    memberMark222-Mar-06 10:23 
    GeneralWindows Firewall Pin
    DanBaker9-Mar-05 12:57
    memberDanBaker9-Mar-05 12:57 
    GeneralSend long message(&gt;32,000 characters). Pin
    pcccong6-Jun-04 23:16
    memberpcccong6-Jun-04 23:16 
    GeneralRe: Send long message(&gt;32,000 characters). Pin
    KarstenK6-Jun-04 23:54
    memberKarstenK6-Jun-04 23:54 
    GeneralMulticast Addresses Pin
    Nas18-Dec-03 0:43
    memberNas18-Dec-03 0:43 
    Generalnot able to run Pin
    onlyrakesh1-Sep-03 3:19
    memberonlyrakesh1-Sep-03 3:19 
    QuestionHow create multicast group address Pin
    nagareshwar9-Jun-03 3:11
    sussnagareshwar9-Jun-03 3:11 
    AnswerRe: How create multicast group address Pin
    elhajjim16-Oct-04 17:28
    memberelhajjim16-Oct-04 17:28 
    GeneralMulticast View Pin
    Ashok Raj22-May-03 20:57
    sussAshok Raj22-May-03 20:57 
    GeneralJoin multicast group ! Pin
    enachemc20-Nov-02 19:44
    memberenachemc20-Nov-02 19:44 
    GeneralRe: Join multicast group ! Pin
    Anonymous1-May-03 3:07
    sussAnonymous1-May-03 3:07 
    GeneralRe: Join multicast group ! Pin
    AntonDel1-Oct-03 3:50
    memberAntonDel1-Oct-03 3:50 
    GeneralMulticast and Multi-NIC computers Pin
    Naji Sourani21-Jan-02 0:03
    memberNaji Sourani21-Jan-02 0:03 
    GeneralRe: Multicast and Multi-NIC computers Pin
    Anonymous11-Feb-02 0:31
    memberAnonymous11-Feb-02 0:31 
    GeneralRe: Multicast and Multi-NIC computers Pin
    KS LAU4-Sep-02 19:24
    memberKS LAU4-Sep-02 19:24 
    GeneralDemo? HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
    Mansukh Patidar10-Jan-02 14:22
    memberMansukh Patidar10-Jan-02 14:22 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.150819.1 | Last Updated 4 Dec 1999
    Article Copyright 1999 by Adeel Jamil Siddiqui
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid