Connecting to Multiple Network Servers

B

Bruce Vander Werf

I am developing a network client application (using the Socket class)
that will need to make simultaneous TCP connections to many (100 or
more) servers. In this case, which would be a better strategy:

1) Create a thread for each connection and use blocking receives,

2) Use asynchronous receives (BeginReceive/EndReceive)

In both cases, I will also be sending data on the socket from the main
thread.

Followup questions -

If using blocking sockets, can I safely call Socket.Send from the main
thread while blocking on Socket.Receive from a secondary thread?

If using asynchronous sockets, can I call BeginReceive for each Socket
as it is connected, using a common EndReceive method? Any problem
making arbitrary Send or BeginSend calls on these sockets from the
main thread while processing receives asynchronously?

Thanks...

--Bruce
 
F

Frank Drebin

This might be a case where you may want to leverage a higher-level
technology - .NET Remoting. It will abstract all the particulars of
establishing a connection and sending data.

But if speed or control is an issue, I would probably create a thread and a
stack (for outbound messages) and make blocking calls - for each connection.
And if instead of the main thread calling Socket.Send, it simply puts it on
the stack, for that particular thread.. and the thread gets to it when it
can.. So you have an outbound queue....

hth
 
F

Frank Drebin

Another option is writing a web service (in .NET or J2EE)... ?

First, with calling send from the main thread.. I would say no, because you
don't know what that thread is doing and you'd have to do error handling to
make sure the thread is still alive and make sure you don't blow up comm
that is currently be sent out... It seems to me, better to add it to the
queue - and the thread will get to it when it can.

Secondly, after thinking about this - good point. What about this.. You have
a 'thread arbiter' - a function that handles all outbound sends. You call
this function and say "for socket 3, send this". And you have some
equivalent of a "CriticalSection" for each socket - so you can only send one
thing at a time - per socket. Meanwhile, you have a seperate thread for each
socket. it sits there and blocks, waiting for data - and then processes it
when it gets it..

You have two "volatile" thing (sort of) - that is sockets and threads... so
I'm also a big fan of building a data structure to track what is using
what.. for example, you have an instance of a class that has all the
information and handles to everything related to a socket.. so, before you
do a send on a particular socket, you check to make sure the receive thread
is still active. etc, etc...

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top