UDP Socket with multiple network cards

A

Alphamacaroon

All,

I'm wondering if anyone can help me with a strange problem I'm having.
First off, here's what I'm trying to do:

I'm developing a UDP network application that should allow the end user
to choose the network interface they want to send and receive UDP
packets on. Simple enough.

If the computer has a single network card and IP address this all works
great. The application binds to the local IP address and works.

Now if we introduce multiple network cards (in my configuration, one is
an ethernet card and one is a wireless 802.11 card), I can only seem to
get one card to work. To test, I have a test client on two machines. On
the machine with two cards, I bind to card number 1 and send a UDP
packet and receive it just fine on machine 2. Now if I bind to card
number 2 and send a UDP packet to machine 2, I don't get anything on
machine number 2. Now what's even more strange, if I disable either
network card and bind to the remaining enabled card, I can receive the
UDP packet just fine on machine number 2. This rules out a network
configuration issue because both cards will work if only one is
enabled, but only one will work when they are both enabled.

What could possibly be causing this? Following is some simple code I'm
using for the test client;

private void BindSocket()
{
if(udpThread != null)
{
udpThread.Abort();
udpThread = null;
}

if(udpSocket != null)
{
udpSocket.Close();
udpSocket = null;
}

udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
udpSocket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.ReuseAddress, 1);

IPEndPoint localEP = new
IPEndPoint(IPAddress.Parse(cboAddresses.Text), (int)numPort.Value);

udpSocket.Bind(localEP);

udpThread = new Thread(new ThreadStart(this.UdpThreadProc));
udpThread.IsBackground = true;
udpThread.Start();

AddOutputText("Bound to " + cboAddresses.Text + ":" +
(int)numPort.Value);

btnSend.Enabled = true;
}

private void UdpThreadProc()
{
byte[] buffer = new byte[5000];

while(true)
{
buffer.Initialize();

EndPoint remoteEP = (EndPoint)new IPEndPoint(IPAddress.Any, 0);

udpSocket.ReceiveFrom(buffer, ref remoteEP);


AddOutputText("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

AddOutputText(((IPEndPoint)remoteEP).Address.ToString() + ":" +
((IPEndPoint)remoteEP).Port);
AddOutputText("");
AddOutputText(System.Text.ASCIIEncoding.ASCII.GetString(buffer));

Thread.Sleep(1);
}
}

private void SendData()
{
string[] address = txtSendTo.Text.Split(':');

string host = address[0];
int port = 5060;

if(address.Length > 1)
{
port = Convert.ToInt32(address[1]);
}

IPEndPoint remoteEP = new
IPEndPoint(Dns.GetHostByName(host).AddressList[0], port);

byte[] sendData =
System.Text.ASCIIEncoding.ASCII.GetBytes(txtSendData.Text);

udpSocket.SendTo(sendData, remoteEP);


AddOutputText(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
AddOutputText(remoteEP.Address.ToString() + ":" + port);
AddOutputText("");
AddOutputText(txtSendData.Text);
}

If you want the full code for the test client, let me know and I'll
Email it to you. Any help would be greatly appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top