title: Broadcasting UDP packets using multiple NICs
body: <p>I'm building an embedded system for a camera controller in Linux (not real-time).  I'm having a problem getting the networking to do what I want it to do.  The system has 3 NICs, 1 100base-T and 2 gigabit ports.  I hook the slower one up to the camera (that's all it supports) and the faster ones are point-to-point connections to other machines.  What I am attempting to do is get an image from the camera, do a little processing, then broadcast it using UDP to each of the other NICs.</p>
  
  <p>Here is my network configuration:</p>
  
  <p>eth0: addr: 192.168.1.200 Bcast 192.168.1.255 Mask: 255.255.255.0  (this is the 100base-t)<br>
  eth1: addr: 192.168.2.100 Bcast 192.168.2.255 Mask: 255.255.255.0<br>
  eth2: addr: 192.168.3.100 Bcast 192.168.3.255 Mask: 255.255.255.0</p>
  
  <p>The image is coming in off eth0 in a proprietary protocol, so it's a raw socket.  I can broadcast it to eth1 or eth2 just fine.  But when I try to broadcast it to both, one after the other, I get lots of network hiccups and errors on eth0.</p>
  
  <p>I initialize the UDP sockets like this:</p>
  
  <pre><code>sock2=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); // Or sock3
  sa.sin_family=AF_INET;
  sa.sin_port=htons(8000);
  inet_aton("192.168.2.255",&amp;sa.sin_addr); // Or 192.168.3.255
  setsockopt(sock2, SOL_SOCKET, SO_BROADCAST, &amp;broadcast, sizeof(broadcast));
  bind(sock2,(sockaddr*)&amp;sa,sizeof(sa));
  
  sendto(sock2,&amp;data,sizeof(data),0,(sockaddr*)&amp;sa,sizeof(sa)); // sizeof(data)&lt;1100 bytes
  </code></pre>
  
  <p>I do this for each socket separately, and call sendto separately.  When I do one or the other, it's fine.  When I try to send on both, eth0 starts getting bad packets.</p>
  
  <p>Any ideas on why this is happening?  Is it a configuration error, is there a better way to do this?</p>
  
  <p>EDIT:
  Thanks for all the help, I've been trying some things and looking into this more.  The issue does not appear to be broadcasting, strictly speaking.  I replaced the broadcast code with a unicast command and it has the same behavior.  I think I understand the behavior better, but not how to fix it.</p>
  
  <p>Here is what is happening.  On eth0 I am supposed to get an image every 50ms.  When I send out an image on eth1 (or 2) it takes about 1.5ms to send the image.  When I try to send on both eth1 and eth2 at the same time it takes about 45ms, occasionally jumping to 90ms.  When this goes beyond the 50ms window, eth0's buffer starts to build.  I lose packets when the buffer gets full, of course.</p>
  
  <p>So my revised question.  Why would it go from 1.5ms to 45ms just by going from one ethernet port to two?</p>
  
  <p>Here is my initialization code:</p>
  
  <pre><code>sock[i]=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  sa[i].sin_family=AF_INET;
  sa[i].sin_port=htons(8000);
  inet_aton(ip,&amp;sa[i].sin_addr);
  
  //If Broadcasting
  char buffer[]="eth1" // or eth2
  setsockopt(sock[i],SOL_SOCKET,SO_BINDTODEVICE,buffer,5);
  int b=1;
  setsockopt(sock[i],SOL_SOCKET,SO_BROADCAST,&amp;b,sizeof(b));
  </code></pre>
  
  <p>Here is my sending code:</p>
  
  <pre><code>for(i=0;i&lt;65;i++) {
    sendto(sock[0],&amp;data[i],sizeof(data),0,sa[0],sizeof(sa[0]));
    sendto(sock[1],&amp;data[i],sizeof(data),0,sa[1],sizeof(sa[1]));
  }
  </code></pre>
  
  <p>It's pretty basic.</p>
  
  <p>Any ideas?  Thanks for all your great help!</p>
  
  <p>Paul</p>