Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set up a socket for UDP multicast with 2 network cards present?
    primarykey
    data
    text
    <p>I'm trying to get udp multicast data using sockets and c++ (c). I have a server with 2 network cards so I need to bind socket to specific interface. Currently I'm testing on another server that has only one network card.</p> <p>When I use INADDR_ANY I can see the udp data, when I bind to specific interface I don't see any data. Function inet_addr is not failing (I removed checking for return value for now).</p> <p>Code is below. On a server with one network card, my IP address is 10.81.128.44. I receive data when I run as: ./client 225.0.0.37 12346</p> <p>This gives me no data: ./client 225.0.0.37 12346 10.81.128.44</p> <p>Any suggestions? (Hope the code compiles, I removed comments ...)</p> <pre><code> #include &lt;stdlib.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;time.h&gt; #include &lt;string.h&gt; #include &lt;stdio.h&gt; #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; #define HELLO_PORT 12345 #define HELLO_GROUP "225.0.0.37" #define MSGBUFSIZE 256 int main(int argc, char *argv[]) { string source_iface; string group(HELLO_GROUP); int port(HELLO_PORT); if (!(argc &lt; 2)) group = argv[1]; if (!(argc &lt; 3)) port = atoi(argv[2]); if (!(argc &lt; 4)) source_iface = argv[3]; cout &lt;&lt; "group: " &lt;&lt; group &lt;&lt; " port: " &lt;&lt; port &lt;&lt; " source_iface: " &lt;&lt; source_iface &lt;&lt; endl; int fd; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) { perror("socket"); exit(1); } u_int yes = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(yes)) &lt; 0) { perror("Reusing ADDR failed"); exit(1); } struct sockaddr_in addr; memset(&amp;addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = (source_iface.empty() ? htonl(INADDR_ANY) : inet_addr(source_iface.c_str())); if (bind(fd,(struct sockaddr *)&amp;addr, sizeof(addr)) &lt; 0) { perror("bind"); exit(1); } struct ip_mreq mreq; mreq.imr_multiaddr.s_addr = inet_addr(group.c_str()); mreq.imr_interface.s_addr = (source_iface.empty() ? htonl(INADDR_ANY) : inet_addr(source_iface.c_str())); if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &amp;mreq, sizeof(mreq)) &lt; 0) { perror("setsockopt"); exit(1); } socklen_t addrlen; int nbytes; char msgbuf[MSGBUFSIZE]; while (1) { memset(&amp;msgbuf, 0, MSGBUFSIZE); addrlen = sizeof(addr); if ((nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *)&amp;addr, &amp;addrlen)) &lt; 0) { perror("recvfrom"); exit(1); } cout.write(msgbuf, nbytes); cout.flush(); } return 0; } </code></pre> <p>Thanks in advance ...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload