Socket.BeginSend Method

Definition

Sends data asynchronously to a connected Socket.

Overloads

BeginSend(IList<ArraySegment<Byte>>, SocketFlags, AsyncCallback, Object)

Sends data asynchronously to a connected Socket.

BeginSend(IList<ArraySegment<Byte>>, SocketFlags, SocketError, AsyncCallback, Object)

Sends data asynchronously to a connected Socket.

BeginSend(Byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object)

Sends data asynchronously to a connected Socket.

BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object)

Sends data asynchronously to a connected Socket.

BeginSend(IList<ArraySegment<Byte>>, SocketFlags, AsyncCallback, Object)

Sends data asynchronously to a connected Socket.

public:
 IAsyncResult ^ BeginSend(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginSend (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags, AsyncCallback? callback, object? state);
public IAsyncResult BeginSend (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags, AsyncCallback callback, object state);
member this.BeginSend : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (buffers As IList(Of ArraySegment(Of Byte)), socketFlags As SocketFlags, callback As AsyncCallback, state As Object) As IAsyncResult

Parameters

buffers
IList<ArraySegment<Byte>>

An array of type Byte that contains the data to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

callback
AsyncCallback

The AsyncCallback delegate.

state
Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

buffers is null.

buffers is empty.

.NET Framework and .NET 5 and earlier only: An error occurred when attempting to access the socket. See remarks section below.

The Socket has been closed.

Remarks

Important

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

You can pass a callback that implements AsyncCallback to BeginSend in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginSend. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginSend method.

The asynchronous BeginSend operation must be completed by calling the EndSend method. Typically, the method is invoked by the AsyncCallback delegate. EndSend will block the calling thread until the operation is completed.

Although intended for connection-oriented protocols, BeginSend also works for connectionless protocols, provided that you first call the Connect or BeginConnect method to establish a default remote host. If you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. It is okay to use BeginSendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling BeginSend by making another call to Connect or BeginConnect. With connectionless protocols, 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 BeginSend will throw a SocketException.

If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.

Note

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Note

state is an instantiation of a user-defined class.

Note

The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

Note

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

See also

Applies to

BeginSend(IList<ArraySegment<Byte>>, SocketFlags, SocketError, AsyncCallback, Object)

Sends data asynchronously to a connected Socket.

public:
 IAsyncResult ^ BeginSend(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult? BeginSend (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode, AsyncCallback? callback, object? state);
public IAsyncResult BeginSend (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode, AsyncCallback callback, object state);
member this.BeginSend : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags * SocketError * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (buffers As IList(Of ArraySegment(Of Byte)), socketFlags As SocketFlags, ByRef errorCode As SocketError, callback As AsyncCallback, state As Object) As IAsyncResult

Parameters

buffers
IList<ArraySegment<Byte>>

An array of type Byte that contains the data to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

errorCode
SocketError

A SocketError object that stores the socket error.

callback
AsyncCallback

The AsyncCallback delegate.

state
Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

buffers is null.

buffers is empty.

.NET Framework and .NET 5 and earlier only: An error occurred when attempting to access the socket. See remarks section below.

The Socket has been closed.

Remarks

Important

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

You can pass a callback that implements AsyncCallback to BeginSend in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginSend. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginSend method.

The asynchronous BeginSend operation must be completed by calling the EndSend method. Typically, the method is invoked by the AsyncCallback delegate. EndSend will block the calling thread until the operation is completed.

Although intended for connection-oriented protocols, BeginSend also works for connectionless protocols, provided that you first call the Connect or BeginConnect method to establish a default remote host. If you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. It is okay to use BeginSendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling BeginSend by making another call to Connect or BeginConnect. With connectionless protocols, 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 BeginSend will throw a SocketException.

If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.

Note

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Note

state is an instantiation of a user-defined class.

Note

The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

Note

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

See also

Applies to

BeginSend(Byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object)

Sends data asynchronously to a connected Socket.

public:
 IAsyncResult ^ BeginSend(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult? BeginSend (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode, AsyncCallback? callback, object? state);
public IAsyncResult BeginSend (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode, AsyncCallback callback, object state);
member this.BeginSend : byte[] * int * int * System.Net.Sockets.SocketFlags * SocketError * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (buffer As Byte(), offset As Integer, size As Integer, socketFlags As SocketFlags, ByRef errorCode As SocketError, callback As AsyncCallback, state As Object) As IAsyncResult

Parameters

buffer
Byte[]

An array of type Byte that contains the data to send.

offset
Int32

The zero-based position in the buffer parameter at which to begin sending data.

size
Int32

The number of bytes to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

errorCode
SocketError

A SocketError object that stores the socket error.

callback
AsyncCallback

The AsyncCallback delegate.

state
Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

buffer is null.

.NET Framework and .NET 5 and earlier only: An error occurred when attempting to access the socket. See remarks section below.

offset is less than 0.

-or-

offset is less than the length of buffer.

-or-

size is less than 0.

-or-

size is greater than the length of buffer minus the value of the offset parameter.

The Socket has been closed.

Remarks

Important

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

You can pass a callback that implements AsyncCallback to BeginSend in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginSend. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginSend method.

The asynchronous BeginSend operation must be completed by calling the EndSend method. Typically, the method is invoked by the AsyncCallback delegate. EndSend will block the calling thread until the operation is completed.

Although intended for connection-oriented protocols, BeginSend also works for connectionless protocols, provided that you first call the Connect or BeginConnect method to establish a default remote host. If you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. It is okay to use BeginSendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling BeginSend by making another call to Connect or BeginConnect. With connectionless protocols, 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 BeginSend will throw a SocketException.

If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.

Note

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Note

state is an instantiation of a user-defined class.

Note

The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

Note

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

See also

Applies to

BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object)

Sends data asynchronously to a connected Socket.

public:
 IAsyncResult ^ BeginSend(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginSend (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, AsyncCallback? callback, object? state);
public IAsyncResult BeginSend (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, AsyncCallback callback, object state);
member this.BeginSend : byte[] * int * int * System.Net.Sockets.SocketFlags * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (buffer As Byte(), offset As Integer, size As Integer, socketFlags As SocketFlags, callback As AsyncCallback, state As Object) As IAsyncResult

Parameters

buffer
Byte[]

An array of type Byte that contains the data to send.

offset
Int32

The zero-based position in the buffer parameter at which to begin sending data.

size
Int32

The number of bytes to send.

socketFlagssocket_flags
SocketFlags

A bitwise combination of the SocketFlags values.

callback
AsyncCallback

The AsyncCallback delegate.

state
Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

buffer is null.

.NET Framework and .NET 5 and earlier only: An error occurred when attempting to access the socket. See remarks section below.

offset is less than 0.

-or-

offset is less than the length of buffer.

-or-

size is less than 0.

-or-

size is greater than the length of buffer minus the value of the offset parameter.

The Socket has been closed.

Remarks

Important

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

You can pass a callback that implements AsyncCallback to BeginSend in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginSend. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginSend method.

The asynchronous BeginSend operation must be completed by calling the EndSend method. Typically, the method is invoked by the AsyncCallback delegate. EndSend will block the calling thread until the operation is completed.

Although intended for connection-oriented protocols, BeginSend also works for connectionless protocols, provided that you first call the Connect or BeginConnect method to establish a default remote host. If you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. It is okay to use BeginSendTo even after you have established a default remote host with Connect. You can also change the default remote host prior to calling BeginSend by making another call to Connect or BeginConnect. With connectionless protocols, 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 BeginSend will throw a SocketException.

If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.

Note

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Note

state is an instantiation of a user-defined class.

Note

The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

Note

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

See also

Applies to