Sending from The Same Udp Socket in Multiple Threads

Sending from the same UDP socket in multiple threads

Yes, I think you can.

As the packets are sent out individually, although the order they are received will be nondeterministic, it is already with UDP.

So sending in multiple threads in the same socket is fine.

Although, if you're doing other stuff with the socket, such as bind(), close(), then you could end up with race conditions, so you might want to be careful.

Use of multiple sockets in Multithreaded UDP server

Once socket is enough because you are able to identify the client each time you receive a packet by calling recvfrom by using its source address (src_addr):

recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);

Now, this solution has a single reception point. That means the server has to receive the data and then pass it to the thread that should perform the process. This reduce the parallelism of the single socket solution since you are using one port only.


In my opinion your solution is better in terms of parallelism since your threads are running on different ports, so they can serve several clients in parallel specially if your machine has several processors and several network cards that could be used to attend your clients. In the best case you may have several threads running in parallel, in different processors each one serving a client coming from different network interface.

EDIT:

After discussing your design with David, he convinced me that having several sockets would lead to several context switches which may slow down the server performance. So the best in this case is to use several threads but a single socket for all the clients.

UDP server sockets in multiple threads with SO_REUSEPORT

But looks like if there are multiple server sockets (bind calls) for unicast packets kernel will deliver the packet only to one of the threads.

This is correct, the packet will only be delivered to one socket.

... enable per session routing of the incoming packets to different sockets ?

There are no sessions in UDP and thus no per sessions routing.

It is possible though to connect UDP sockets, i.e. call both bind and connect. In this case the packet will be delivered to the connected socket. If there are multiple connected sockets for the same source it will again be delivered to only one. And note that a connected socket will only receive packets for this connection, i.e. not from arbitrary sources like an unconnected socket.

Pointer to the relevant functions in the kernel code that delivers the incoming packet to the socket will also be helpful.

This behavior is not specific to Linux.

Linux socket using multiple threads to send

The kernel will synchronize access to underlying file descriptor for you, so you don't need a separate mutex. There would be a problem with this approach if you were using TCP, but since we are talking about UDP this should be safe, though not necessarily best way.



Related Topics



Leave a reply



Submit