Multicast recvfrom not working??????

Discussion in 'Linux Networking' started by cs3266, Jun 23, 2006.

  1. cs3266

    cs3266 Guest

    I'm trying to setup a multicast client that listens in on eth1 witht
    the address:
    the address on which the stream is coming in is:
    the socket is setup fine without any problems but i still don't get any the recvfrom call
    i've gone through almost every reference i've found...but still can't
    figure out whats going on...can anyone help...
    below is the code that I'm using to set it up:

    //all the used variables are defined in the header file (no compile
    issues here):
    //running it on a debian box enabled for multicast

    memset(&mcast_addr, 0, sizeof(mcast_addr));
    mcast_addr.sin_family = AF_INET;
    mcast_addr.sin_port = htons(mPort);
    mcast_addr.sin_addr.s_addr = inet_addr("");

    memset(&cli_addr, 0, sizeof(cli_addr));
    cli_addr.sin_family = AF_INET;
    cli_addr.sin_port = htons(mPort);
    cli_addr.sin_addr.s_addr = inet_addr("");

    if((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))< 0)

    int iSize = 65535;
    if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char*)&iSize,
    sizeof(iSize)) < 0)

    int bTrue = 1;
    if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &bTrue, sizeof(bTrue))
    < 0){

    int ret;
    if ((ret = bind(sock, (struct sockaddr*)&cli_addr, sizeof(cli_addr))
    )< 0)

    //unsigned long Addr = cli_addr.sin_addr.s_addr;
    struct in_addr interface_addr;
    if(inet_aton("", &(interface_addr) ) != 1)
    cout << "the addr thing failed" << endl;

    if(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
    (char*)&interface_addr, sizeof(interface_addr)) < 0)

    if(inet_aton(mAddress.c_str() , &(mreq.imr_multiaddr) ) != 1)
    cout << "imr_multiaddr failed" << endl;
    if(inet_aton("", &(mreq.imr_interface)) != 1)
    cout << "imr_interface failed" << endl;

    if(setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mreq,
    sizeof(mreq)) < 0)

    int iTTL =0;
    if(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&iTTL,
    sizeof(iTTL)) < 0)

    if(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (char*)&bFalse,
    sizeof(bFalse)) < 0)

    int iFromLen = sizeof(from);
    if(recvfrom(sock, mBuffer, MAX_SIZE, 0, &from,
    (socklen_t*)&iFromLen) > 0){
    cout << "got a message" << endl;
    cs3266, Jun 23, 2006
    1. Advertisements

  2. While is a multicast address, it is not in the recommended range of to
    It is usually a good idea to use INADDR_ANY.

    I assume you are using the SO_REUSEADDR socket option because the client and
    server are on the same computer?

    Generally you never need to set the IP_MULTICAST_IF option unless you have a
    multihomed computer.

    Why are you setting the TTL to 0? I believe it needs to be 1 even for loopback
    on the same computer.
    Phil Frisbie, Jr., Jun 23, 2006
    1. Advertisements

  3. cs3266

    cs3266 Guest

    thanks Phil
    the address is what the servers uses, unfortunately I can't
    do much about it.
    I am notusing INADDR_ANY since I'm certain that eth1 is connected
    receives the multicast packets, but should that cause any problems?
    No. The client and the server are running on two different machines. My
    client is on the same LAN. If i don't use that option, bind fails for
    some reason.
    Could you please explain what you mean by a multihomed computer?
    I don't plan to send out messages, so I don't expect setting TTL to 0
    causing any problems

    I'm going to try all that you have suggested, and see if it works.
    Besides the above, do you have any other clues as to why i'm not
    getting anything in the recvfrom call?
    cs3266, Jun 24, 2006
  4. It makes your software more fragile, and one more thing the user must set up in
    a config file. Just use INADDR_ANY and let the OS use the proper address.
    If bind() fails then there is already another application bound to that port.
    You need to find out what that is and either shut it down or use another port.
    Multihomed means you have two or more network cards attached to different networks.
    Then just leave it at the default which is 1.
    I suspect the bind() problem could be the cause, but keep us posted.
    Phil Frisbie, Jr., Jun 26, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.