Breyta

Deila með


Socket.SendToAsync Method

Definition

Overloads

SendToAsync(SocketAsyncEventArgs)

Sends data asynchronously to a specific remote host.

SendToAsync(ArraySegment<Byte>, EndPoint)

Sends data to the specified remote host.

SendToAsync(ArraySegment<Byte>, SocketFlags, EndPoint)

Sends data to the specified remote host.

SendToAsync(ReadOnlyMemory<Byte>, EndPoint, CancellationToken)

Sends data to the specified remote host.

SendToAsync(ReadOnlyMemory<Byte>, SocketFlags, EndPoint, CancellationToken)

Sends data to the specified remote host.

SendToAsync(ReadOnlyMemory<Byte>, SocketFlags, SocketAddress, CancellationToken)

Sends data to a specific endpoint using the specified SocketFlags.

SendToAsync(SocketAsyncEventArgs)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Sends data asynchronously to a specific remote host.

public:
 bool SendToAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool SendToAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.SendToAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function SendToAsync (e As SocketAsyncEventArgs) As Boolean

Parameters

e
SocketAsyncEventArgs

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

A socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

The Socket has been closed.

The protocol specified is connection-oriented, but the Socket is not yet connected.

Remarks

The SendToAsync method starts an asynchronous send operation to the remote host specified in the SocketAsyncEventArgs.RemoteEndPoint property of the e parameter. Calling the SendToAsync method gives you the ability to send data within a separate execution thread. Although this method is intended for connectionless protocols, SendToAsync works with both connectionless and connection-oriented protocols.

To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and attach the callback to the SocketAsyncEventArgs.Completed event.

The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the SendToAsync 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.

If you are using a connection-oriented protocol, you must first call the Accept, AcceptAsync, BeginAccept, BeginConnect, Connect, or ConnectAsync method. Otherwise SendToAsync will throw a SocketException. When using a connection-oriented protocol, the SendToAsync method will ignore the SocketAsyncEventArgs.RemoteEndPoint property and send data to the System.Net.EndPoint established in the Accept, AcceptAsync, BeginAccept, BeginConnect, Connect, or ConnectAsync method.

If you are using a connectionless protocol, you do not need to establish a default remote host with the BeginConnect, Connect, or ConnectAsync method prior to calling SendToAsync. You only need to do this if you intend to call the BeginSend or SendAsync methods. If you do call the BeginConnect, Connect, or ConnectAsync method prior to calling SendToAsync, the SocketAsyncEventArgs.RemoteEndPoint property will override the specified default remote host for that send operation only. You are also not required to call the Bind method. In this case, the underlying service provider will assign the most appropriate local network IP address and port number. Use a port number of zero if you want the underlying service provider to select a free port. If you need to identify the assigned local network IP address and port number, you can use the LocalEndPoint property after the SocketAsyncEventArgs.Completed event is signaled and the associated delegates are called.

If you want to send data to a broadcast address, you must first call the SetSocketOption method and set the socket option for SocketOptionName.Broadcast to true. You must also be sure that the size of your buffer does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and SendToAsync will throw a SocketException.

If you specify the DontRoute flag in the SocketAsyncEventArgs.SocketFlags property, the data you are sending will not be routed.

For message-oriented sockets, care must be taken not to exceed the maximum message size of the underlying transport. If the size of the buffer exceeds the maximum packet size of the underlying service provider, the datagram is not sent and SendToAsync will throw a SocketException. The successful completion of a SendToAsync method does not indicate that the data was successfully delivered.

See also

Applies to

SendToAsync(ArraySegment<Byte>, EndPoint)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Sends data to the specified remote host.

public:
 System::Threading::Tasks::Task<int> ^ SendToAsync(ArraySegment<System::Byte> buffer, System::Net::EndPoint ^ remoteEP);
public System.Threading.Tasks.Task<int> SendToAsync (ArraySegment<byte> buffer, System.Net.EndPoint remoteEP);
member this.SendToAsync : ArraySegment<byte> * System.Net.EndPoint -> System.Threading.Tasks.Task<int>
Public Function SendToAsync (buffer As ArraySegment(Of Byte), remoteEP As EndPoint) As Task(Of Integer)

Parameters

buffer
ArraySegment<Byte>

The buffer for the data to send.

remoteEP
EndPoint

The remote host to which to send the data.

Returns

An asynchronous task that completes with the number of bytes sent.

Exceptions

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

SendToAsync(ArraySegment<Byte>, SocketFlags, EndPoint)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Sends data to the specified remote host.

public:
 System::Threading::Tasks::Task<int> ^ SendToAsync(ArraySegment<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP);
public System.Threading.Tasks.Task<int> SendToAsync (ArraySegment<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP);
member this.SendToAsync : ArraySegment<byte> * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> System.Threading.Tasks.Task<int>
Public Function SendToAsync (buffer As ArraySegment(Of Byte), socketFlags As SocketFlags, remoteEP As EndPoint) 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.

remoteEP
EndPoint

The remote host to which to send the data.

Returns

An asynchronous task that completes with the number of bytes sent.

Exceptions

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

SendToAsync(ReadOnlyMemory<Byte>, EndPoint, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Sends data to the specified remote host.

public System.Threading.Tasks.ValueTask<int> SendToAsync (ReadOnlyMemory<byte> buffer, System.Net.EndPoint remoteEP, System.Threading.CancellationToken cancellationToken = default);
member this.SendToAsync : ReadOnlyMemory<byte> * System.Net.EndPoint * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function SendToAsync (buffer As ReadOnlyMemory(Of Byte), remoteEP As EndPoint, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

Parameters

buffer
ReadOnlyMemory<Byte>

The buffer for the data to send.

remoteEP
EndPoint

The remote host to which to send 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

remoteEP is null.

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

SendToAsync(ReadOnlyMemory<Byte>, SocketFlags, EndPoint, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Sends data to the specified remote host.

public System.Threading.Tasks.ValueTask<int> SendToAsync (ReadOnlyMemory<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP, System.Threading.CancellationToken cancellationToken = default);
member this.SendToAsync : ReadOnlyMemory<byte> * System.Net.Sockets.SocketFlags * System.Net.EndPoint * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function SendToAsync (buffer As ReadOnlyMemory(Of Byte), socketFlags As SocketFlags, remoteEP As EndPoint, 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.

remoteEP
EndPoint

The remote host to which to send 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

remoteEP is null.

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

SendToAsync(ReadOnlyMemory<Byte>, SocketFlags, SocketAddress, CancellationToken)

Source:
Socket.Tasks.cs
Source:
Socket.Tasks.cs

Sends data to a specific endpoint using the specified SocketFlags.

public System.Threading.Tasks.ValueTask<int> SendToAsync (ReadOnlyMemory<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.SocketAddress socketAddress, System.Threading.CancellationToken cancellationToken = default);
member this.SendToAsync : ReadOnlyMemory<byte> * System.Net.Sockets.SocketFlags * System.Net.SocketAddress * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function SendToAsync (buffer As ReadOnlyMemory(Of Byte), socketFlags As SocketFlags, socketAddress As SocketAddress, 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.

socketAddress
SocketAddress

The SocketAddress that represents the destination for 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

socketAddress is null.

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