1

Is it safe to use a single UdpClient instance on two separate threads to simultaneously receive from a client and send to a different client? According to the MSDN docs this is fine for TcpClient but I'm unsure about it for UdpClient. I shortened my code here to get the idea across.

void ReceiveThread () {
    while (running) {
        var payload = udpClient.Receive(ref clientEndPoint);
        QueuePayload(payload);
    }
} 

void SendThread () {
    var payload = DequeuePayload();

    while (running) {
        udpClient.Send(payload, payload.Count, client2EndPoint);
    }
}

Would a good solution to this problem be to create a new UdpClient whenever I send a payload? The payloads will be sent quite frequently though (around 20 per second).

CrashTestDummy
  • 145
  • 1
  • 10

1 Answers1

0

No. Microsoft's standard documentation blurb says

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445