Asynchronous server socket multiple clients
Asked Answered
B

2

16

I have been working with the following code published on msdn:

http://msdn.microsoft.com/en-us/library/fx6588te.aspx

I understand that the server application is not blocked whilst the application is waiting for new clients.

However can this application (or even sockets) for that matter handle multiple concurrent requests?

  • What would happen if client A and B connected at the same time?

  • If client A connects and the handling of its request takes 5 seconds, if client B connects a second later must it wait for client A to finish before its processing can start?

  • Or will client A and client B's requests be handled concurrently?

I have done some testing with this by putting Thread.Sleep(n) commands in between the receive/send data in the socket listener code. I can then send multiple requests to the socket and they appear to be handled. However the socket always handles them on the same thread id - which makes me believe that it isnt actually happening concurrently.

Especially given the description by microsoft that this app simply doesnt block whilst awaiting for new connections - does that mean it can handle concurrent connections?

Blakeley answered 28/4, 2011 at 8:37 Comment(2)
Actually, the page states that execution of the server application is not suspended while it waits for a connection from a client, because all calls are done using async callbacks, meaning that your main thread won't be blocked while waiting for connections or data. But yes, it can receive data simultaneously on multiple threads, although only one "accept" callback can be active for a single incoming connection (meaning you must do your work quickly when accepting the connection).Cockshy
Hy RemotecUK, Can you please post your updated code for accepting multiple connections. I have also used MSDN example and need multiple connections to handle. Please help.Causalgia
C
34

[Update 2014]: It seems that the example has been modified since this answer was posted, as noted in this thread. The MSDN example now handles multiple incoming connections properly. Anyway, the general approach described here is correct and perhaps it can provide additional clarification.


When doing socket communication, you basically have a single listener socket for all incoming connections, and multiple handler sockets for each connected client.

Listening to incoming connections

When you start listening to a port, you create a socket with a callback method for incoming connections (this is referencing the example you mentioned). That's the one-and-only listener socket for that port number:

listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

This line tells the listener to invoke the AcceptCallback method whenever a new client is connected (new connection callback). That method is the one which should do its work quickly, since it blocks other incoming connections.

Creating dedicated handler sockets

That is also why AcceptCallback must immediately create a dedicated "handler" socket with its own background data callback method (ReadCallback):

// inside AcceptCallback, we switch to the handler socket for communication
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
    new AsyncCallback(ReadCallback), state); // fired on a background thread

From that moment on, ReadCallback method is invoked whenever some data is received by your newly connected client.

Also, before returning, AcceptCallback needs to call listener.BeginAccept again, to continue listening to new incoming connections:

// this is the same server socket we opened previously, which will now 
// continue waiting for other client connections: it doesn't care about
// the actual data transmission between individual clients
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

This part is omitted from the MSDN example, meaning it can only receive a single connection.

Receiving data

As soon as you get a packet of data from your client, ReadCallback method will be invoked. So, inside this data callback method, you need to read and process the received data, and then invoke the same BeginReceive method again (again, with ReadCallback as its data callback method).

[Edit]

The problem with MSDN example is that it allows connection of only a single client (listener.BeginAccept is called only once). To allow mulitple concurrent connections, you need to create a receive socket using handler.BeginReceive, and then call listener.BeginAccept to start listening to new clients.

Cockshy answered 28/4, 2011 at 8:44 Comment(4)
Thanks for this reply! You saved the Champions League for me today :). I called for the new connections BeginAccept before BeginReceive, which took me to a problem.Leakage
Hy Groo, Can you please post your updated MSDN example code for accepting multiple connections. I need it. Please help.Causalgia
@daniel: this actually is the code for accepting multiple connections. The AcceptCallback gets called whenever a new connection is established, and calling listener.BeginAccept inside it returns a dedicated Socket for that connection (which is then associated with its own ReadCallback handler). If you have a specific use case, please post a separate question with details.Cockshy
hy groo, please view my this question #25404614, and help. thanksCausalgia
P
-1

Every socket will have a listen queue associated with it. This will have the pending/partially accepted incoming connections. The max number number of pending connections can be defined programmatically in listen() API, which is nothing but 'listener.Listen(100)' in this example. Having had this as 100 here, the socket 'listener' can have 150 (=2*100/2) pending connections in the listen queue.

Politicize answered 29/4, 2011 at 10:28 Comment(1)
2 * 100 / 2 = 150? No way.Leakage

© 2022 - 2024 — McMap. All rights reserved.