When to use blocking sockets over non blocking sockets on UDP?

Question 1:What's is recommended for the following scenario ?

Multiple clients send data over multiple UDP sockets, i.e. say client1 sends data over sockets 100, 101, 102,..,105 . client2 on sockets 110,111,112...115 etc. Port numbers are fixed for clients. Number of clients are fixed(10). So totally 10 * 5 sockets.

Data on each sockets(atleast 3 sockets) is sent every few milliseconds. The other 2 sockets are for sending commands and getting data.

After receiving the data there is some IO involved where i write to disks. (I cant change any of the above. i.e. number of sockets ,etc )

Question 2:Is it best to use Threads for this scenario (or) Asynchronous IO multiplexing with nonblocking sockets?

  • Are that many sockets really neccessary? Anyway, I'd use select() to read from any socket, then dispatch to a thread. – ott-- Apr 4 '13 at 17:34
  • I can't change that as its already decided that way. You mean select() with non blocking sockets ? – m4n07 Apr 4 '13 at 17:44
  • 1
    select() with blocking. For non-blocking you could use recvfrom() for each socket in a separate thread. – ott-- Apr 4 '13 at 17:58
  • In blocking whats that you are planning to use to receive data other than recvfrom(). Can you please point to an example or pseudocode. Thanks – m4n07 Apr 4 '13 at 18:06

The usual way is to use poll() or select(), which ever you find more to your liking, with blocking sockets. Since they accept any file descriptors, same loop can be used for inter-thread or inter-process communication with a socket pair, or reading input from stdin.

A good, quite short and to the point, reading is Beej's Guide to Network Programming Using Internet Sockets. Check out section about select() for a small example program source. That uses TCP and not UDP, so you'll need to do some adapting, but UDP will probably just make things simpler (unless your use case is re-implementing TCP functionality on top of UDP, which almost never is a good idea).

You may want to do processing in separate threads (or even processes, depending on requirements), or just inside that same loop, depending on how heavy this is: "After receiving the data there is some IO involved where i write to disks." I'd start with a single-threaded implementation and see how it goes, before making it more complex.

| improve this answer | |
  • Thanks. How do i create threads per socket on using Select(). – m4n07 Apr 7 '13 at 4:08
  • @m4n07 point of select is, you don't need thread per socket, you can use for example a thread pool. So when your select loop in main thread gets an UDP packet, you put it to a queue and notify worker threads to pick it up. If you have a static number of sockets, you can create thread per socket, each working independently (with blocking read, or a select loop in each thread). – hyde Apr 7 '13 at 6:30
  • Thanks hyde. For the first method can you pls point to a example.As i tried im able to get the data from multiple sockets using select() , How do i do notify the worker threads in C. – m4n07 Apr 7 '13 at 7:27
  • @m4n07 Well, for ring buffer implementation, this question has example answers about ring buffer. Other than that, google... But I'd create first version without threads, just isolating the UDP packet handling to a function which takes just one struct as argument (so you can easily move it to another thread once single-threaded code works). – hyde Apr 8 '13 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.