Socket.BeginSend Socket.BeginSend Socket.BeginSend Socket.BeginSend Method

Definition

Sends data asynchronously to a connected Socket.

Overloads

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

Sends data asynchronously to a connected Socket.

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

Sends data asynchronously to a connected Socket.

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

Sends data asynchronously to a connected Socket.

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

Sends data asynchronously to a connected Socket.

BeginSend(IList<ArraySegment<Byte>>, SocketFlags, AsyncCallback, Object) BeginSend(IList<ArraySegment<Byte>>, SocketFlags, AsyncCallback, Object) 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);
member this.BeginSend : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags * AsyncCallback * obj -> IAsyncResult
Parameters
buffers
IList<ArraySegment<Byte>>

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

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

A bitwise combination of the SocketFlags values.

state
Object Object Object Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

An error occurred when attempting to access the socket. See remarks section below.

Remarks

The BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend will throw an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. Calling the BeginSend method gives you the ability to send data within a separate execution thread.

You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method. To do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. If your callback needs more information, you can create a small class or structure to hold the Socket and the other required information. Pass an instance of this class to the BeginSend method through the state parameter.

Your callback method should invoke the EndSend method. When your application calls BeginSend, the system will use a separate thread to execute the specified callback method, and will block on EndSend until the Socket sends the number of bytes requested or throws an exception. If you want the original thread to block after you call the BeginSend method, use the WaitHandle.WaitOne method. Call the Set method on a T:System.Threading.ManualResetEvent in the callback method when you want the original thread to continue executing. For additional information on writing callback methods see Marshaling a Delegate as a Callback Method.

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. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

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 the .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

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

Important

This API is not CLS-compliant.

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);
[System.CLSCompliant(false)]
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 *  * AsyncCallback * obj -> IAsyncResult
Parameters
buffers
IList<ArraySegment<Byte>>

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

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

A bitwise combination of the SocketFlags values.

errorCode
SocketError SocketError SocketError SocketError

A SocketError object that stores the socket error.

state
Object Object Object Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

An error occurred when attempting to access the socket. See remarks section below.

Remarks

The BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend will throw an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. Calling the BeginSend method gives you the ability to send data within a separate execution thread.

You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method. To do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. If your callback needs more information, you can create a small class or structure to hold the Socket and the other required information. Pass an instance of this class to the BeginSend method through the state parameter.

Your callback method should invoke the EndSend method. When your application calls BeginSend, the system will use a separate thread to execute the specified callback method, and will block on EndSend until the Socket sends the number of bytes requested or throws an exception. If you want the original thread to block after you call the BeginSend method, use the WaitHandle.WaitOne method. Call the Set method on a ManualResetEvent in the callback method when you want the original thread to continue executing. For additional information on writing callback methods see Marshaling a Delegate as a Callback Method.

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. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

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 the .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

BeginSend(Byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object) BeginSend(Byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object) 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);
member this.BeginSend : byte[] * int * int * System.Net.Sockets.SocketFlags *  * AsyncCallback * obj -> IAsyncResult
Parameters
buffer
Byte[]

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

offset
Int32 Int32 Int32 Int32

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

size
Int32 Int32 Int32 Int32

The number of bytes to send.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

A bitwise combination of the SocketFlags values.

errorCode
SocketError SocketError SocketError SocketError

A SocketError object that stores the socket error.

state
Object Object Object Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

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.

Remarks

The BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend will throw an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. Calling the BeginSend method gives you the ability to send data within a separate execution thread.

You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method. To do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. If your callback needs more information, you can create a small class or structure to hold the Socket and the other required information. Pass an instance of this class to the BeginSend method through the state parameter.

Your callback method should invoke the EndSend method. When your application calls BeginSend, the system will use a separate thread to execute the specified callback method, and will block on EndSend until the Socket sends the number of bytes requested or throws an exception. If you want the original thread to block after you call the BeginSend method, use the WaitHandle.WaitOne method. Call the Set method on a T:System.Threading.ManualResetEvent in the callback method when you want the original thread to continue executing. For additional information on writing callback methods see Marshaling a Delegate as a Callback Method.

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. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

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 the .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

BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) 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 socket_flags, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginSend (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socket_flags, 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, socket_flags 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 Int32 Int32 Int32

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

size
Int32 Int32 Int32 Int32

The number of bytes to send.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

A bitwise combination of the SocketFlags values.

state
Object Object Object Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

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.

Examples

The following code example begins asynchronously sending data to a remote host.

allDone->Set();
Socket^ s = safe_cast<Socket^>(ar->AsyncState);
s->EndConnect( ar );
StateObject^ so2 = gcnew StateObject;
so2->workSocket = s;
array<Byte>^ buff = Encoding::ASCII->GetBytes( "This is a test" );
s->BeginSend( buff, 0, buff->Length, SocketFlags::None,
   gcnew AsyncCallback( &Async_Send_Receive::Send_Callback ), so2 );
allDone.Set();
Socket s = (Socket) ar.AsyncState;
s.EndConnect(ar);
StateObject so2 = new StateObject();
so2.workSocket = s;
byte[] buff = Encoding.ASCII.GetBytes("This is a test");
s.BeginSend(buff, 0, buff.Length,0,
                   new AsyncCallback(Async_Send_Receive.Send_Callback), so2);    
Public Shared Sub Connect_Callback(ar As IAsyncResult)
   

   allDone.Set()
   Dim s As Socket = CType(ar.AsyncState, Socket)
   s.EndConnect(ar)
   Dim so2 As New StateObject()
   so2.workSocket = s
   Dim buff As Byte() = Encoding.ASCII.GetBytes("This is a test")
   s.BeginSend(buff, 0, buff.Length, 0, New AsyncCallback(AddressOf Async_Send_Receive.Send_Callback), so2)
End Sub 'Connect_Callback

Remarks

The BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend will throw an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. Calling the BeginSend method gives you the ability to send data within a separate execution thread.

You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method. To do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. If your callback needs more information, you can create a small class or structure to hold the Socket and the other required information. Pass an instance of this class to the BeginSend method through the state parameter.

Your callback method should invoke the EndSend method. When your application calls BeginSend, the system will use a separate thread to execute the specified callback method, and will block on EndSend until the Socket sends the number of bytes requested or throws an exception. If you want the original thread to block after you call the BeginSend method, use the WaitHandle.WaitOne method. Call the Set method on a T:System.Threading.ManualResetEvent in the callback method when you want the original thread to continue executing. For additional information on writing callback methods see Marshaling a Delegate as a Callback Method.

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. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

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 the .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