views:

1780

answers:

4

I need to Send & Recv at the same time, which should be the best:

1 thread handling send & recv with non-blocking socket or 2 threads with One handling blocking recv() + one handling send()

Or is there another option?

I am expecting to have up to about 50 2 way connections.. Which result in 50 thread in option1, and 100 thread in option 2.

Thanks

A: 

You could use one thread with two non-blocking sockets and use select() to wait for incoming input and space in output queue.

Then you do not need to poll because select() blocks without using processor time.

rstevens
poll, not select! :-P (Okay, if you're using WinSock, select is probably more portable, but you can also use WSAWaitForMultipleEvents in that case, which is like poll. :-P)
Chris Jester-Young
It was an answer to the "Or is there another option?" part of his question. And since he didn't mentioned any platform I had chosen the most portable version which is select() [Available in C/C++, on Windows/Linux, PERL and many, many other languages!)
rstevens
+1  A: 

You should use non-blocking sockets, but rather than polling them manually, you should have the kernel do it for you. Use either poll or select for this (the former is preferred, because it can handle more sockets at once). When you do this, you will end up with 1 thread in option 1, or 2 threads in option 2. :-P

Chris Jester-Young
is poll a standard C/C++ function? Can't seem to find it..Thanks
It's apparently an "X/Open System Interfaces Extension": http://www.opengroup.org/onlinepubs/009695399/functions/poll.html
Chris Jester-Young
In that sense, select is probably "more standard": http://www.opengroup.org/onlinepubs/009695399/functions/select.html (However, I still support using poll, for platforms that support it.)
Chris Jester-Young
+2  A: 

I wouldn't use either of those approaches.

Take a look at this SO question. I would use a worker threads model where you would have N thread handling all traffic with non-blocking sockets.

If you absolutely HAVE to follow one of the approaches you've just described, go with non-blocking IMHO.

Pablo Santa Cruz
+1 Although this question isn't a Java question, it lends itself really, really well to using Executors in Java, which implements pretty much the model you've described in your linked post.
Chris Jester-Young
Yes. Executors are great. Definitely.
Pablo Santa Cruz
A: 

If you are concerned about performance and scalability, try IOCP (async socket read/write): http://stackoverflow.com/questions/869744/how-to-write-a-scalable-tcp-ip-based-server/908766#908766

Note that it is a lot trickier to implement IOCP than 'a thread per connection', and since you are only going to have 50 connections (or 100 threads as you suggest), that may be 'good enough' and simpler to implement correctly.

Try the simple approach and measure it... but if you either: need more performance, or, are going to scale (far?) beyond 50 connections, seriously consider IOCP as a better solution.

jerryjvl