0

I have a UDP server in C based on libuv, we have a :

  • Main Thread : that receives an UDP packet that and enqueues it to one of the 4 queues and invokes a dequeue callback to one of the 4 workers. The main thread is bind() to port 9930
  • 4 Worker Threads : dequeues from the respective queue on callback invocation and sends back a response. Each thread also binds to the same address

Now, when i send request (src_port:A and dest_port:9930) to the UDP server , the server responds with a UDP packet with src_port:B and dest_port:A. I want B to be equal to 9930.

I went through some articles online and set UV_UDP_REUSEADDR flag in uv_udp_bind() for both the main thread and in the 4 worker thread. But, now the udp server doesn't always accept the request, not even the receive callbacks are invoked. It does sometimes and for these cases the flow is proper with port B=9930.

Tlacenka
  • 520
  • 9
  • 15
melwin_jose
  • 315
  • 3
  • 20

1 Answers1

1

Now, when i send request (src_port:A and dest_port:9930) to the UDP server , the server responds with a UDP packet with src_port:B and dest_port:A. I want B to be equal to 9930.

The server must respond from a socket bound to port 9930 then. E.g. respond from the very same socket that received the request.

Also, the worker threads can share the same one socket the main thread opened. When multiple threads receive on the same UDP socket, only one of them gets the datagram. It is also safe for multiple threads to send on the same UDP socket.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271