UDP Socket with multiple network cards

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alphamacaroon

    UDP Socket with multiple network cards

    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(AddressF amily.InterNetw ork, SocketType.Dgra m,
    ProtocolType.Ud p);
    udpSocket.SetSo cketOption(Sock etOptionLevel.I P,
    SocketOptionNam e.ReuseAddress, 1);

    IPEndPoint localEP = new
    IPEndPoint(IPAd dress.Parse(cbo Addresses.Text) , (int)numPort.Va lue);

    udpSocket.Bind( localEP);

    udpThread = new Thread(new ThreadStart(thi s.UdpThreadProc ));
    udpThread.IsBac kground = true;
    udpThread.Start ();

    AddOutputText(" Bound to " + cboAddresses.Te xt + ":" +
    (int)numPort.Va lue);

    btnSend.Enabled = true;
    }

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

    while(true)
    {
    buffer.Initiali ze();

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

    udpSocket.Recei veFrom(buffer, ref remoteEP);


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

    AddOutputText(( (IPEndPoint)rem oteEP).Address. ToString() + ":" +
    ((IPEndPoint)re moteEP).Port);
    AddOutputText(" ");
    AddOutputText(S ystem.Text.ASCI IEncoding.ASCII .GetString(buff er));

    Thread.Sleep(1) ;
    }
    }

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

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

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

    IPEndPoint remoteEP = new
    IPEndPoint(Dns. GetHostByName(h ost).AddressLis t[0], port);

    byte[] sendData =
    System.Text.ASC IIEncoding.ASCI I.GetBytes(txtS endData.Text);

    udpSocket.SendT o(sendData, remoteEP);


    AddOutputText(" >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>");
    AddOutputText(r emoteEP.Address .ToString() + ":" + port);
    AddOutputText(" ");
    AddOutputText(t xtSendData.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!

Working...