Click here to Skip to main content
14,978,905 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I had been working on a Server Client aplication where Server is going to service(sendto + receivefrom)'x' number of Clients at a time. For this purpose, I have created 'x' number of threads on Server side so that each thread is dadicated to one single client. Inside each thread there is a specific socket for just for its client. I was thinking to make these sockets non-blocking but now I think using blocking socket inside each thread is a better idea. Blocking Socket continuously waits to receive data and whenever there is a need to send anything, sendto() is called. Is using blocking Socket in such a situation a good approach or should I use non-blocking sockets? :(
Waiting for help!!!
Updated 12-May-13 21:07pm

1 solution

I think that using threads with blocking sockets instead of asynchronous API is much better. In most cases, communication is sequential in its nature and the flow of operations is logically independent from other threads. Therefore, using a separate thread for each communication channel (in case of TCP) is way more straightforward, logically.

Of course, you pay for it by using thread synchronization. But you learn thread synchronization once and then use this knowledge for a lifetime, while asynchronous APIs are application-specific.

For some ideas, please see my past answers:
Multple clients from same port Number[^],
an amateur question in socket programming[^].

pasztorpisti 13-May-13 4:02am
5ed, Agree that programming sequentially is a good approach whenever possible. In case of sockets I would add 1 more thing, I usually write my own socket class that has a blocking interface but it uses nonblocking socket handle + select/WSAWaitForMultipleEvents/... inside. This way I can wait for both a socket and an event so I can cancel the socket operations any time from a different thread in case of graceful exit.
Thank you very much. Your idea seems interesting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900