Click here to Skip to main content
11,929,675 members (49,591 online)
Rate this:
Please Sign up or sign in to vote.
See more: Windows Win32 Visual-Studio , +
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? Frown | :(
Waiting for help!!!
Posted 12-May-13 21:04pm
Edited 12-May-13 22:07pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 13 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100