Click here to Skip to main content
Rate this: bad
good
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
v2

1 solution

Rate this: bad
good
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[^].
 
—SA
  Permalink  
Comments
pasztorpisti at 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.
Sergey Alexandrovich Kryukov at 13-May-13 4:05am
   
Thank you very much. Your idea seems interesting.
—SA

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

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 13 May 2013
Copyright © CodeProject, 1999-2014
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