pls help: Make an asynchronous socket receive messages

J

Juan

I built a socket server that is able to connect clients and echo the message
included woth the client request to connect. I haven´t been able to make the
server listen for further messages. This is the loop where I'm suppose to
make the BeginReceive call... Does any one know how should I place the call?

while (true)

{

allDone.Reset();

Console.WriteLine("Esperando por conexión...");

listener.BeginAccept(

new AsyncCallback(AcceptCallback),listener );

allDone.WaitOne();

}

Regards,

Juan.
 
D

David Browne

Juan said:
I built a socket server that is able to connect clients and echo the message
included woth the client request to connect. I haven´t been able to make the
server listen for further messages. This is the loop where I'm suppose to
make the BeginReceive call... Does any one know how should I place the call?

Yes. But ususally it isn't necessary to use async sockets here.
It's much easier to code if you place your listener thread in a blocking
loop on Accept.
As after Accept just spawn a thread to handle the new socket, or pass it to
a thread pool.

Then just go back to a blocking Accept.

David
 
J

Juan

Thanks Davis... Do you know how scalable is to do it that way? I red that
usin asynchronous calls is the way to go.

Juan.
 
D

David Browne

Juan said:
Thanks Davis... Do you know how scalable is to do it that way? I red that
usin asynchronous calls is the way to go.

It's very scalable. Not quite so scalable as the async route, but quite
scalable. Your listening thread can accept and delegate many many
connections per second. Your worker threads or thread pool will scale out
way before a single listener thread will.

If you have more simultaneous client connections than you can spawn threads
then you may need to go to a thread-pooled or even an async model for
handling the client tcp sessions. But your listener, at least, can use
simple blocking socket accepts.

If you have many simultaneous clients, ask yourself if your server is more
like an FTP server or a HTTP server. An FTP server has many long-lived
requests, with very little activity on each. A web server has many
short-lived requests, where each is a flury of CPU and disk activity. For
an FTP server, you would want to use async sockets to service all your
clients with just a couple threads. For a web server you would want a pool
of threads, and make incoming clients wait until a thread becomes available
to service their requests.

David
 
J

Juan

Thaks David, I've downloaded a series of examples but none ogf them goes
async. I tried the examples from microsoft at msdn for an async server

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconnon-blockingserversocketexample.asp

I haven`t been able to modify the code so it continues to receive messages
from the connected clientes, I alredy ordered two books on .net nerwork
programming moved by my frustration!
I know that this would be simple to solve but I haven´t been able to. I'm
sorry to ask but pls give me any indication onhow to do it, it is really
unconfortable to leave this unsolved!!!!

and thanks!
 

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