469,922 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,922 developers. It's quick & easy.

Connecting to Multiple Network Servers


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

Nov 15 '05 #1
2 1492
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
"Bruce Vander Werf" <br*****@hotmail.com> wrote in message
news:32********************************@4ax.com...

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

Nov 15 '05 #2
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
"Bruce Vander Werf" <br*****@hotmail.com> wrote in message
news:t2********************************@4ax.com...
Remoting is not an option as communication is with non-Windows
servers.

If the thread is blocking on a receive, how would it process the send?
Are you saying it is unsafe to call send from the main thread?

--Bruce

On Mon, 25 Aug 2003 23:52:51 GMT, "Frank Drebin"
<no*****@imsickofspam.com> wrote:
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 astack (for outbound messages) and make blocking calls - for each connection.And if instead of the main thread calling Socket.Send, it simply puts it onthe stack, for that particular thread.. and the thread gets to it when it
can.. So you have an outbound queue....

hth
"Bruce Vander Werf" <br*****@hotmail.com> wrote in message
news:32********************************@4ax.com.. .

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

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ysrana2001 | last post: by
1 post views Thread by Mark McKay | last post: by
1 post views Thread by Wayne Happ | last post: by
3 posts views Thread by Charles A. Lackman | last post: by
14 posts views Thread by Peter | last post: by
4 posts views Thread by Ryan | last post: by
35 posts views Thread by keerthyragavendran | last post: by
1 post views Thread by goodolpete | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.