I read a lot about blocking and non-blocking sockets for udp, but it's hard for me to understand the merits of one over the other. The vast majority of comments on the Internet seem to indicate that non-blocking is better, but does not very accurately determine which scenarios they will be better in, and I did not find links to when blocking is preferable. I hope this issue helps the community shed some light on this topic.
a little background to my own set of problems, so the answers can be applied both specifically and to the general nature of the question. I have an udp server that I am writing, which will have 40 connections on the local network, due to which there will be a constant stream of data. Data transfer rates will be about 250 MB / s on average with peaks up to 500 + MB / s with avg datagram size of about 1400 bytes. Datagram processing is small, but due to the large volume of messages, efficiency and performance are a high priority to prevent packet loss.
since I could not find contextual information for something resembling this particular set of problems, I had to make several assumptions based on what I was able to learn about locking, not locking. I will just end this with my current hypothesis, and then open it to your input. basically, since it will be an almost constant stream of packets on every connection, I think a blocking socket would be preferable because the time that any recv function actually spends blocked will be very minimal compared to using an event-based event a model that will have an overwhelming number of triggers in asynchronous mode. I feel that my true set of problems will most likely be prioritizing 40 threads, which I plan to use to read from sockets ... so that everyone gets their share of CPU time. I can be wrong in my approach and ideas, so I hope and will be very grateful if the community can help shed light on this issue.
~ ~ Edit
while I'm concerned about how thread design will influence / integrate with the block / non-block issue. I'm really worried about how to consider locking / non-blocking in terms of my set of issues. if multithreading really becomes a problem, I can go with a thread pool solution.
~ ~ Edit2
Firstly, I wanted to tell the tank to you for the answers so far. some of you mentioned that a single thread / socket model with so many sockets might be a bad idea, and I admit that I tried with the solution myself. however, in one of the links in the nikolai answer, the author discusses a single-stream / socket model and refers to a very interesting article, which, as I thought, I would like to refer to, since it dissipates the many myths that I had about streams and events. based models: why events are a bad idea
c design udp sockets
jim
source share