Find the answer to your Linux question:
Results 1 to 4 of 4
Hi I am trying to port some MS Visual Studio C++ code to Linux/wxWidgets. The code is socket-based and very similar between Windows and Linux. In Linux the socket, bind, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    3

    Problem receiving UDP multicast


    Hi
    I am trying to port some MS Visual Studio C++ code to Linux/wxWidgets. The code is socket-based and very similar between Windows and Linux. In Linux the socket, bind, and setsocketopt all return with no error. See:
    Code:
    BOOL CAV15Input::SetupSocket(BOOL Out)
    {
            if(pU->PrDebug)
            {
                    sprintf(_send,"CAV15Input::SetupSocket for port %d",pSonar->dPort);
                    pU->OutMsg(_send,1,0);
            }
           
            pSonar->UDP_Sock=0;
            pSonar->UDPError=0;
            pSonar->UDP_Sock=socket(AF_INET,SOCK_DGRAM,0);
            if(pSonar->UDP_Sock<0 )
            {
                    pU->OutMsg("socket failed on SetupSocket",11,0);
                    pSonar->UDP_Sock=0;
                    pSonar->UDPError|=4;
                    return FALSE;
            }
            //      the AF_INET address family, which contains IP addresses,
            local.sin_family=AF_INET;
            local.sin_addr.s_addr=inet_addr(pSonar->IP_Add_PC);
            local.sin_port=htons(pSonar->dPort);
            int t=1;
            if(setsockopt(pSonar->UDP_Sock,
                SOL_SOCKET,
                    SO_REUSEADDR,
                    (char *)&t,sizeof(t))
            <0)
            {
                    CloseSock();
                    pU->OutMsg("set SO_REUSEADDR failed in SetupSocket",11,0);
                    pSonar->UDPError|=4;
                    return FALSE;
            }
    
            if(bind(pSonar->UDP_Sock,(sockaddr *)&local,sizeof(local)))
            {
                    CloseSock();
                    pU->OutMsg("bind failed in SetupSocket",11,0);
                    pSonar->UDPError|=8;
                    return FALSE;
            }
            if(pU->PrDebug)
            {
                    sprintf(_send,"socket setup & bind OK for port %d",pSonar->dPort);
                    pU->OutMsg(_send,1,0);
            }
      // Join multicast group
            if(pSonar->datatype)    // for element data
                    mreq.imr_multiaddr.s_addr=inet_addr(pSonar->IP_Add_ELD);       
            else    // for beam data
              mreq.imr_multiaddr.s_addr=inet_addr(pSonar->IP_Add_HRB);
            mreq.imr_interface.s_addr=inet_addr(pSonar->IP_Add_PC);
            if(setsockopt(pSonar->UDP_Sock,
                IPPROTO_IP,
                    IP_ADD_MEMBERSHIP,
                    (char *)&mreq,sizeof(mreq))
            <0)
            {
                    CloseSock();
                    pU->OutMsg("join multicast failed in SetupSocket",11,0);
                    pSonar->UDPError|=16;
                    return FALSE;
            }
            if(pU->PrDebug)
            {
                    if(pSonar->datatype)    // for element data
                            sprintf(_send,"join multicast OK on %s",pSonar->IP_Add_ELD);   
                    else    // for beam data
                            sprintf(_send,"join multicast OK on %s",pSonar->IP_Add_HRB);
                    pU->OutMsg(_send,1,0);
            }
            jMCast=1;
    
            return TRUE;
    }
    But then when I try
    recv(pSonar->UDP_Sock,&pUDP->u_dg_cM.cM[8],reedlen,0);
    it blocks as if there is no data. (In Windows this works fine.)
    I tried no wait and found that recv returns -1 (no data).
    Is there something else in Linux and/or wxWidgets that I'm missing that could prevent UDP datagrams from being received??
    I looked at ifconfig and netstat outputs and they show that multicast is setup and that packets are being received with no errors, BUT my app gets nothing (recv returns -1).
    Thanks.
    Gordon

  2. #2
    Just Joined!
    Join Date
    Nov 2010
    Posts
    2
    GordonAkst,

    do the following (you will need root access on the receiver box for some of the cmds)

    (1) first make sure the receiver box is mc capable
    run 'netstat -r', if you see a line starting with "224.0.0.0", then you are fine.
    if not then do '/sbin/route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0' and verify again
    (note: use eth0 or whatever interface you plan to use)

    (2) make sure your receiver is able participate in the mc traffics
    from any box on your network, run 'ping 224.0.0.1', if you see ping responses from your receiver box, then you are fine.
    if not then do, '/sbin/sysctl net.ipv4.icmp_echo_ignore_broadcasts=0' and verify again

    (3) on the receiver box, run the cmd 'netstat -g' to verify that you have joined the correct mc group on the interface. if your mc group shows up on the interface, then it could be the local f/w blocking the mc traffic.
    just disable the local firewall to check if this is the case.
    run '/sbin/service iptables stop' to disable the local f/w

    also run tcpdump on the receiver box to check if your mc traffic shows up.

    good luck.

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Posts
    3

    linux multicast test

    Thanks for the input; I've tried it and am confused by results.
    Attached file shows commands and outputs; the tcpdump output really has me wondering; it shows most of the packets "dropped by kernel".
    Another point: I used RHEL4 Network Tools - Devices which shows a counter for received packets. It only increments counters when my app is running, but my app gets no data.
    Very confusing??
    Gordon
    Attached Files Attached Files

  4. #4
    Just Joined!
    Join Date
    Nov 2010
    Posts
    2
    Everthing looks fine to me.
    Your box 192.168.121.245 is receiving the m/c from the host 192.168.121.35.
    The tcpdump output confirms that.

    However you app is still not able to recv the data ???
    I can suggest 2 more things
    (1) run the sender & reciever on the same box, check the results
    (2) modify your app to use INADDR_ANY instead of the specific interface (eth0) & check the results.
    Do it at both the places ie the before the bind() & at 'mreq.imr_interface.s_addr' before doing the mcast join.

    also curious, what is the packet size you are sending.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •