title: active network interface using Sockets in C#
body: <p>I'm currently working on a windows application that is supposed to communicate with remote embedded systems over WiFi. The Windows application is sending a broadcast message which the remote units are receiving and extract the sender IP from. The remote units then sends an answer back to the extracted IP. The problem is that when sending the broadcast message, C# is using the Wireless Network Connection, but when I open a Socket to listen for the answer (or sending a message to a specified address) it is using the Local Area Connection. Since these interfaces have different IP-adresses the windows application wont receive the answer from the remote.</p>
  
  <p>So my questions are, why are C# using different network interfaces for the different sockets and is there any way of forcing the program to use the same interface for all sockets?</p>
  
  <p>Here is the code for sending the broadcast message</p>
  
  <pre><code>    sockB = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);    //Create a UDP socket
      sockB.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);        //Enable broadcast on socket
      IPEndPoint briep = new IPEndPoint(IPAddress.Broadcast, 9584);                            //Relate a port to the broadcast address    
      byte[] dat = { 0xAD, 0xEE, BRDCST_CMND, (byte)(0xAD ^ 0xEE ^ BRDCST_CMND) };           //Put in buffer
      sockB.SendTo(dat, briep);              //Broadcast data
  </code></pre>
  
  <p>and here is the code for listening from answers from the remotes</p>
  
  <pre><code>    sockC = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);                 //Create an udp socket
      sockC.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1);                 //set timeout to 5 ms
      sockC.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1);
      IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9999);             //set up an "any" ip address and associate it with port 9854        
      EndPoint ep = (EndPoint)(iep);                                      //Make an endpoint out of previous association
      sockC.Bind(iep);                             //Bind the created socket to listen on port 9854
      byteRecv = sockC.ReceiveFrom(data, ref ep);                  //Receive on socke
  </code></pre>
  
  <p>Thank you!</p>