tags:

views:

216

answers:

1

I'm writing a server and need to be able to send a message to all connected clients.

I'm trying to decide if I should create SocketAsyncEventArgs for each connected socket and send all the packets at once.

Another approach would be to use a single SocketAsyncEventArgs and send the packets one after another. Then it would be tempting to call SendAsync in the ProcessSend method (which is called when SendAsync has completed) and have a queue of connected sockets. But what if for some reason SendAsync will not complete in a reasonable amount of time? Is there any scenario that would make it never to complete?

A: 

SendAsync will return immediately. If you're talking about the send operation itself, it won't never complete, but it may take a long time if the socket is in an abnormal state.

Jon B
Yes, I know that the method allways returns immediately. But if the time before the Completed event handler is called might be long then it wouldn't be safe to have a send queue and reuse the same SocketAsyncEventArgs.
remdao
It could even take a long time (longer than usual, anyway) time if there's network congestion; or if it's a flow-cotrolling, guaranteed-delivery protocol I think it might take a long time if the receiver is slow in its reading of previous data (so that the connection's network buffers are full and being flow-controlled).
ChrisW
@remdao: You should assume that SendAsync may take longer than you'd like it to, and code accordingly. In almost every case your complete event will happen very quickly, but you can't count on that.
Jon B
If you want to reuse your resource immediately after invoking the API, then you should use synchronous API instead.
ChrisW