none
UDP package uses wrong ethernet to send package. RRS feed

  • Question

  • I am trying to solve this problem but i do not know where i am making a mistake. There is a PC which program circuit board using windows form application. On that PC, There are two NIC. one for internet and second for connect to the circuit board that is configured by application. In this code, i am sending a UDP message On IP : 169.254.100.100 and port 55000. This package must sent to second ethernet but somehow it is sending it to first ethernet only. I am using the SendMessage(string message) below to send the message. Can you help to force this UDP to send it to second ethernet port. thanks in advance.

    Edit: I used wireshark to check if package is sent or not. If I disable either of the NIC then i can see Destination ip address as 169.254.100.100 but if I run program when both NIC is enable then package sent but Destination ip address changes to 255.255.255.255

       public void SendMessage(string message)
            {   //
                byte[] bytes = Encoding.ASCII.GetBytes(message);
                Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                IPAddress send_to_address = IPAddress.Parse("169.254.100.100");
                IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 55000);
                sending_socket.SendTo(bytes, sending_end_point);


                //byte[] bytes = Encoding.ASCII.GetBytes(message);

                //EndPoint ep = new IPEndPoint(IPAddress.Broadcast, 55000);
                //cltSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, ep, new AsyncCallback(OnSend), null);
                //IPAddress ip = IPAddress.Parse("169.254.100.100");
                //IPEndPoint endpoint = new IPEndPoint(ip, 55000);
                //Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);

                //socket.Bind(endpoint);
                //socket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, endpoint, new AsyncCallback(OnSend), null);

    }

    Thursday, October 29, 2015 1:50 PM

All replies

  • Hi JeffWhite00010,
    I am trying to involve someone familiar with this topic to further look at this issue.
    There might be some time delay. Appreciate your patience.

    Best Regards,

    Grady

    Friday, October 30, 2015 8:51 AM
    Moderator
  • No problem. Thanks for the reply
    Friday, October 30, 2015 12:22 PM
  • Hi JeffWhite00010,

    I've found a solution to your issue.

    What you can do is to fetch all of NIC info and select one among them.

    The code snippet is as followings:

            static Socket GetUdpSender()
            {
                int port = 11001;
                
                IPHostEntry entry = Dns.GetHostEntry(Dns.GetHostName());
    
                //find ip address for your adapter here
                IPAddress[] localAddress = entry.AddressList;
                int length = localAddress.Length;
    
                // Select an IP address corresponding to the NIC, here I just simply choose one, you need to write more codes to select the desired IP
                IPEndPoint localEndPoint = new IPEndPoint(localAddress[4], port);
    
                //use socket instead of a UdpClient
                Socket upd_sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    
                //binds client to the local end point
                upd_sending_socket.Bind(localEndPoint);
    
                return upd_sending_socket;
            }

    Hopefully this will help you.

    Best regards,

    Zhiqing


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Monday, November 9, 2015 8:40 AM