Concurrent send and receive data in one port with udpclient

13,117

Solution 1

You can absolutely have two UdpClient on one port but you need to set socket options before you bind it to an endpoint.

private static void SendAndReceive()
{
  IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, 1234);
  ThreadPool.QueueUserWorkItem(delegate
  {
    UdpClient receiveClient = new UdpClient();
    receiveClient.ExclusiveAddressUse = false;
    receiveClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
    receiveClient.Client.Bind(ep1);
    byte[] buffer = receiveClient.Receive(ref ep1);
  });

  UdpClient sendClient = new UdpClient();
  sendClient.ExclusiveAddressUse = false;
  sendClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  IPEndPoint ep2 = new IPEndPoint(IPAddress.Parse("X.Y.Z.W"), 1234);
  sendClient.Client.Bind(ep1);
  sendClient.Send(new byte[] { ... }, sizeOfBuffer, ep2);
}

Solution 2

Use the same IPEndPoint for receiving that you used for sending.

UdpClient udpClient2 = new UdpClient(50177);
IPEndPoint Ip2 = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 1005);
udpClient2.Send(peerto255, peerto255.Length, Ip2);
var dgram = udpClient2.Receive(ref Ip2);
Share:
13,117
Koorosh Sarfaraz
Author by

Koorosh Sarfaraz

Updated on June 26, 2022

Comments

  • Koorosh Sarfaraz
    Koorosh Sarfaraz almost 2 years

    I'm trying to send and receive data to a specific endpoint with local port 50177. Send data does very good, but when the program tries to receive data it can't receive any. When I sniff the network with Wireshark I see that server sent data to me. I know that I can't have 2 UdpClient on one port at the same time.

    Can any one help me?

    UdpClient udpClient2 = new UdpClient(50177);
    IPEndPoint Ip2 = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 1005);
    udpClient2.Send(peerto255, peerto255.Length, Ip2);
    
    IPEndPoint Ip = new IPEndPoint(IPAddress.Parse("10.10.240.1"), 1005); 
    var dgram = udpClient2.Receive(ref Ip);
    
  • Alex Johnson
    Alex Johnson over 5 years
    Should sendClient.Client.Bind(ep1); be sendClient.Client.Bind(ep2);?
  • Farzan
    Farzan over 5 years
    @AlexJohnson No, ep1 is correct. it's about having two UdpClients being bound to one port.
  • Mariusz Schimke
    Mariusz Schimke about 5 years
    @Farzan, but when you bind multiple sockets to the same endpoint, and use only one for receiving, won't you start losing incoming data? Will the data appear only in the input buffer of the first created socket, or of all of them?
  • Greg
    Greg over 2 years
    This was very helpful. However, I found that if I bound the transmitter to the end point before binding the receiver, it would not work. Binding the receiver first it does work, like in this example.
  • Greg
    Greg over 2 years
    To expand on my above comment, what I mean is that I had to have called receiveClient.Receive(ref ep1) before binding the send client to the endpoint.