Socket.SendAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
SendAsync(ReadOnlyMemory<Byte>, CancellationToken) |
Sends data on a connected socket. |
SendAsync(IList<ArraySegment<Byte>>, SocketFlags) |
Sends data on a connected socket. |
SendAsync(ArraySegment<Byte>, SocketFlags) |
Sends data on a connected socket. |
SendAsync(ReadOnlyMemory<Byte>, SocketFlags, CancellationToken) |
Sends data on a connected socket. |
SendAsync(IList<ArraySegment<Byte>>) |
Sends data on a connected socket. |
SendAsync(ArraySegment<Byte>) |
Sends data on a connected socket. |
SendAsync(SocketAsyncEventArgs) |
Sends data asynchronously to a connected Socket object. |
SendAsync(ReadOnlyMemory<Byte>, CancellationToken)
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
Sends data on a connected socket.
public System.Threading.Tasks.ValueTask<int> SendAsync (ReadOnlyMemory<byte> buffer, System.Threading.CancellationToken cancellationToken = default);
member this.SendAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function SendAsync (buffer As ReadOnlyMemory(Of Byte), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)
Parameters
- buffer
- ReadOnlyMemory<Byte>
The buffer for the data to send.
- cancellationToken
- CancellationToken
A cancellation token that can be used to cancel the asynchronous operation.
Returns
An asynchronous task that completes with the number of bytes sent.
Exceptions
The cancellation token was canceled. This exception is stored into the returned task.
Applies to
SendAsync(IList<ArraySegment<Byte>>, SocketFlags)
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
Sends data on a connected socket.
public:
System::Threading::Tasks::Task<int> ^ SendAsync(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags);
public System.Threading.Tasks.Task<int> SendAsync (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags);
member this.SendAsync : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags -> System.Threading.Tasks.Task<int>
Public Function SendAsync (buffers As IList(Of ArraySegment(Of Byte)), socketFlags As SocketFlags) As Task(Of Integer)
Parameters
- buffers
- IList<ArraySegment<Byte>>
A list of buffers for the data to send.
- socketFlags
- SocketFlags
A bitwise combination of SocketFlags values that will be used when sending the data.
Returns
An asynchronous task that completes with the number of bytes sent.
Exceptions
buffers
is null
.
An error occurred when attempting to access the socket.
The Socket has been closed.
Remarks
This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Send(IList<ArraySegment<Byte>>, SocketFlags).
Applies to
SendAsync(ArraySegment<Byte>, SocketFlags)
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
Sends data on a connected socket.
public:
System::Threading::Tasks::Task<int> ^ SendAsync(ArraySegment<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags);
public System.Threading.Tasks.Task<int> SendAsync (ArraySegment<byte> buffer, System.Net.Sockets.SocketFlags socketFlags);
member this.SendAsync : ArraySegment<byte> * System.Net.Sockets.SocketFlags -> System.Threading.Tasks.Task<int>
Public Function SendAsync (buffer As ArraySegment(Of Byte), socketFlags As SocketFlags) As Task(Of Integer)
Parameters
- buffer
- ArraySegment<Byte>
The buffer for the data to send.
- socketFlags
- SocketFlags
A bitwise combination of SocketFlags values that will be used when sending the data.
Returns
An asynchronous task that completes with the number of bytes sent.
Exceptions
An error occurred when attempting to access the socket.
The Socket has been closed.
Applies to
SendAsync(ReadOnlyMemory<Byte>, SocketFlags, CancellationToken)
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
Sends data on a connected socket.
public System.Threading.Tasks.ValueTask<int> SendAsync (ReadOnlyMemory<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Threading.CancellationToken cancellationToken = default);
member this.SendAsync : ReadOnlyMemory<byte> * System.Net.Sockets.SocketFlags * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function SendAsync (buffer As ReadOnlyMemory(Of Byte), socketFlags As SocketFlags, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)
Parameters
- buffer
- ReadOnlyMemory<Byte>
The buffer for the data to send.
- socketFlags
- SocketFlags
A bitwise combination of SocketFlags values that will be used when sending the data.
- cancellationToken
- CancellationToken
A cancellation token that can be used to cancel the asynchronous operation.
Returns
An asynchronous task that completes with the number of bytes sent.
Exceptions
An error occurred when attempting to access the socket.
The Socket has been closed.
The cancellation token was canceled. This exception is stored into the returned task.
Applies to
SendAsync(IList<ArraySegment<Byte>>)
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
Sends data on a connected socket.
public:
System::Threading::Tasks::Task<int> ^ SendAsync(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers);
public System.Threading.Tasks.Task<int> SendAsync (System.Collections.Generic.IList<ArraySegment<byte>> buffers);
member this.SendAsync : System.Collections.Generic.IList<ArraySegment<byte>> -> System.Threading.Tasks.Task<int>
Public Function SendAsync (buffers As IList(Of ArraySegment(Of Byte))) As Task(Of Integer)
Parameters
- buffers
- IList<ArraySegment<Byte>>
A list of buffers for the data to send.
Returns
An asynchronous task that completes with the number of bytes sent.
Exceptions
buffers
is null
.
An error occurred when attempting to access the socket.
The Socket has been closed.
Remarks
This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Send(IList<ArraySegment<Byte>>).
Applies to
SendAsync(ArraySegment<Byte>)
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
- Source:
- Socket.Tasks.cs
Sends data on a connected socket.
public:
System::Threading::Tasks::Task<int> ^ SendAsync(ArraySegment<System::Byte> buffer);
public System.Threading.Tasks.Task<int> SendAsync (ArraySegment<byte> buffer);
member this.SendAsync : ArraySegment<byte> -> System.Threading.Tasks.Task<int>
Public Function SendAsync (buffer As ArraySegment(Of Byte)) As Task(Of Integer)
Parameters
- buffer
- ArraySegment<Byte>
The buffer for the data to send.
Returns
An asynchronous task that completes with the number of bytes sent.
Exceptions
An error occurred when attempting to access the socket.
The Socket has been closed.
Applies to
SendAsync(SocketAsyncEventArgs)
- Source:
- Socket.cs
- Source:
- Socket.cs
- Source:
- Socket.cs
Sends data asynchronously to a connected Socket object.
public:
bool SendAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool SendAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.SendAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function SendAsync (e As SocketAsyncEventArgs) As Boolean
Parameters
The SocketAsyncEventArgs object to use for this asynchronous socket operation.
Returns
true
if the I/O operation is pending. The Completed event on the e
parameter will be raised upon completion of the operation.
false
if the I/O operation completed synchronously. In this case, The Completed event on the e
parameter will not be raised and the e
object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.
Exceptions
The Buffer or BufferList properties on the e
parameter must reference valid buffers. One or the other of these properties may be set, but not both at the same time.
A socket operation was already in progress using the SocketAsyncEventArgs object specified in the e
parameter.
The Socket has been closed.
The Socket is not yet connected or was not obtained via an Accept(), AcceptAsync(SocketAsyncEventArgs),or BeginAccept, method.
Remarks
The SendAsync method is used to write outgoing data from one or more buffers on a connection-oriented socket. This method can also be used, however, on connectionless sockets that have specified a remote host on a connect operation.
The SendAsync method starts an asynchronous send operation to the remote host established in the Accept, AcceptAsync, BeginAccept, BeginConnect, Connect, or ConnectAsync method.
The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:
SocketAsyncEventArgs.Buffer or SocketAsyncEventArgs.BufferList
SocketAsyncEventArgs.Count if SocketAsyncEventArgs.Buffer is set
SocketAsyncEventArgs.Offset if SocketAsyncEventArgs.Buffer is set
The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the SendAsync method, so that the information will be retrievable in the callback method. If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.
The SendAsync method will throw an exception if you do not first call Accept, AcceptAsync, BeginAcceptBeginConnect, Connect, or ConnectAsync.
Calling the SendAsync method gives you the ability to send data within a separate execution thread.
For message-oriented sockets, do not exceed the maximum message size of the underlying Windows sockets service provider. If the data is too long to pass atomically through the underlying service provider, no data is transmitted and the SendAsync method throws a SocketException with the SocketAsyncEventArgs.SocketError set to the native Winsock WSAEMSGSIZE error code (10040).
Note that the successful completion of the SendAsync method does not indicate that the data was successfully delivered.