Is Socket.SendAsync thread efficient?

I was messing around with Silverlight TCP communication, and I had to use the System.Net.Sockets.Socket class, which at run time Silverlight has only asynchronous methods.

I was wondering what would happen if two threads call SendAsync on a Socket instance in a very short time, one from the other?

My only concern is to not have mixed bytes passing through the TCP channel.

Being an asynchronous method , I suppose a message is placed in a queue from which one thread is deleted, so such events do not occur (mixing the contents of the message on the wire).

But I'm not sure, and MSDN does not indicate anything in the method description. Is anyone sure of this?

EDIT1: No, locking the object before calling SendAsync, for example:

lock(this._syncObj)
{
    this._socket.SendAsync(arguments);
}

will not help, as it serializes requests to send data, not sent data.

+3
source share
1 answer

To invoke SendAsync, you must first invoke ConnectAsyncwith the instance SocketAsyncEventArgs. Its instance SocketAsyncEventArgs, which is the connection between the client and server. A call SendAsyncwith the same instance SocketAsyncEventArgsthat was just used for the outstanding call SendAsyncwill throw an exception.

SendAsync Socket, SocketAsyncEventArgs. ( , ), HTTP- , . , , , .

+6

Source: https://habr.com/ru/post/1718014/


All Articles