title: Receiving UDP broadcast datagrams
body: <p>My system has 3 active network interfaces:</p>
  
  <ul>
  <li>192.168.1.7    (Wireless adapter)</li>
  <li>192.168.247.1  (virtual VMWare Ethernet adapter)</li>
  <li>169.254.54.231 (another VMWare Ethernet adapter)</li>
  </ul>
  
  <p>I'm trying to set up an UDP socket listening for <a href="http://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol" rel="nofollow">SSDP</a> broadcasts on port 1900 on all interfaces, however I don't seem to receive all broadcast datagrams but only some.</p>
  
  <p>This is my code:</p>
  
  <pre><code>static void Main(string[] args) {
      IPEndPoint broadcastEP =
          new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
  
      using (var udp = new UdpClient(broadcastEP.Port)) {
          udp.JoinMulticastGroup(broadcastEP.Address);
          while (true) {
              IPEndPoint remoteEP = null;
              Console.WriteLine("Listening for data on port " + broadcastEP.Port);
              byte[] buffer = udp.Receive(ref remoteEP);
              Console.WriteLine("Received " + buffer.Length + " data bytes from " + remoteEP);
          }
      }   
  }
  </code></pre>
  
  <p>If I now sent a broadcast datagram from another process, the above code should pick it up, right?</p>
  
  <p>However when I execute this code in another process, the first process will only pick up the broadcast, if it's been sent from the 192.168.1.7 interface. If I send a broadcast from one of the other interfaces, the first process simply won't receive it. I can see that the broadcast is actually being sent in Wireshark...am I missing something?</p>
  
  <pre><code>static void Main(string[] args) {
      var ifs = new IPAddress[] {
              IPAddress.Parse("192.168.1.7"),
              IPAddress.Parse("192.168.247.1"),
              IPAddress.Parse("169.254.54.231")
          };  
      IPEndPoint broadcastEP =
          new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
  
      using (UdpClient cli = new UdpClient(new IPEndPoint(ifs[0], 0))) {
                  IPEndPoint ep = new IPEndPoint(broadcastEP.Address, broadcastEP.Port);
                  int n = cli.Send(new byte[] { 1, 2, 3, 4 }, 4, ep);
                  Console.WriteLine("Sent " + n + " bytes to " + ep);
      }
  }
  </code></pre>
  
  <p>I know Windows runs a service (called SSDPSRV) listening for SSDP broadcasts on port 1900. Could that possibly "swallow" datagrams, so that they won't be delivered to my process? If so, is there anything I can do about this?</p>
  
  <p>Thanks</p>