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

定义

将数据异步发送到连接的 SocketSends 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)

将数据异步发送到连接的 SocketSends 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)

将数据异步发送到连接的 SocketSends 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)

将数据异步发送到连接的 SocketSends 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)

将数据异步发送到连接的 SocketSends 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)

将数据异步发送到连接的 SocketSends 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

参数

buffers
IList<ArraySegment<Byte>>

Byte 类型的数组,它包含要发送的数据。An array of type Byte that contains the data to send.

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

返回

引用异步发送的 IAsyncResultAn IAsyncResult that references the asynchronous send.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参见下面的备注部分。See remarks section below.

注解

Connect方法对BeginConnect、 、或BeginAccept方法中建立的远程主机启动异步发送操作。 Accept BeginSendThe BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend如果你不Accept首先调用Connect、、或BeginConnect, BeginAccept将引发异常。BeginSend will throw an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. 通过调用BeginSend方法, 可以在单独的执行线程中发送数据。Calling the BeginSend method gives you the ability to send data within a separate execution thread.

你可以创建一个实现AsyncCallback委托的回调方法, 并将其名称传递BeginSend给方法。You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method. 为此, 你state的参数至少必须包含用于通信的已连接或默认值。 SocketTo do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. 如果回调需要更多的信息, 可以创建一个小型类或结构来保存Socket和其他所需的信息。If your callback needs more information, you can create a small class or structure to hold the Socket and the other required information. 通过参数将此类的一个实例BeginSend传递给方法。 statePass an instance of this class to the BeginSend method through the state parameter.

回调方法应调用EndSend方法。Your callback method should invoke the EndSend method. 当应用程序调用BeginSend时, 系统将使用一个单独的线程来执行指定的回调方法, 并在EndSend Socket发送请求的字节数或引发异常之前阻止。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. 如果希望在调用BeginSend方法后原始线程会被阻止, 请WaitHandle.WaitOne使用方法。If you want the original thread to block after you call the BeginSend method, use the WaitHandle.WaitOne method. 如果希望原始线程继续执行, 请在回调方法中的 T:System.Threading.ManualResetEvent 上调用 Set 方法。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.

尽管适用于面向连接的协议, BeginSend但也适用于无连接协议, 前提是你首先Connect调用BeginConnect或方法来建立默认远程主机。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. 如果你使用的是无连接协议并且打算将数据发送到多个不同的主机, BeginSendTo则应使用。If you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. 即使已使用建立默认BeginSendTo远程Connect主机, 也可以使用。It is okay to use BeginSendTo even after you have established a default remote host with Connect. 还可以在调用BeginSend之前更改默认远程主机, 方法是Connect调用或BeginConnectYou 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. 如果是这样, 则不会发送数据报并BeginSendSocketException引发。If it does, the datagram will not be sent and BeginSend will throw a SocketException.

如果指定DontRoute标志socketflags作为参数, 则不会路由要发送的数据。If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

给定线程启动的所有 i/o 都将在该线程退出时取消。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.

备注

state是用户定义的类的实例化。state is an instantiation of a user-defined class.

备注

成功完成发送并不表示数据已成功传递。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

备注

为异步Socket方法缓存执行上下文 (安全上下文、模拟用户和调用上下文)。The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. 第一次使用特定的上下文 (特定的异步Socket方法、特定Socket的实例和特定的回调) 后, 该上下文的后续使用将会提高性能。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.

另请参阅

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

重要

此 API 不符合 CLS。

将数据异步发送到连接的 SocketSends 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

参数

buffers
IList<ArraySegment<Byte>>

Byte 类型的数组,它包含要发送的数据。An array of type Byte that contains the data to send.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

SocketFlags 值的按位组合。A bitwise combination of the SocketFlags values.

errorCode
SocketError 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.

返回

引用异步发送的 IAsyncResultAn IAsyncResult that references the asynchronous send.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参见下面的备注部分。See remarks section below.

注解

Connect方法对BeginConnect、 、或BeginAccept方法中建立的远程主机启动异步发送操作。 Accept BeginSendThe BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend如果你不Accept首先调用Connect、、或BeginConnect, BeginAccept将引发异常。BeginSend will throw an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. 通过调用BeginSend方法, 可以在单独的执行线程中发送数据。Calling the BeginSend method gives you the ability to send data within a separate execution thread.

你可以创建一个实现AsyncCallback委托的回调方法, 并将其名称传递BeginSend给方法。You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method. 为此, 你state的参数至少必须包含用于通信的已连接或默认值。 SocketTo do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. 如果回调需要更多的信息, 可以创建一个小型类或结构来保存Socket和其他所需的信息。If your callback needs more information, you can create a small class or structure to hold the Socket and the other required information. 通过参数将此类的一个实例BeginSend传递给方法。 statePass an instance of this class to the BeginSend method through the state parameter.

回调方法应调用EndSend方法。Your callback method should invoke the EndSend method. 当应用程序调用BeginSend时, 系统将使用一个单独的线程来执行指定的回调方法, 并在EndSend Socket发送请求的字节数或引发异常之前阻止。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. 如果希望在调用BeginSend方法后原始线程会被阻止, 请WaitHandle.WaitOne使用方法。If you want the original thread to block after you call the BeginSend method, use the WaitHandle.WaitOne method. 当你希望原始线程继续ManualResetEvent执行时, 请在回调方法中调用的 Set 方法。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.

尽管适用于面向连接的协议, BeginSend但也适用于无连接协议, 前提是你首先Connect调用BeginConnect或方法来建立默认远程主机。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. 如果你使用的是无连接协议并且打算将数据发送到多个不同的主机, BeginSendTo则应使用。If you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. 即使已使用建立默认BeginSendTo远程Connect主机, 也可以使用。It is okay to use BeginSendTo even after you have established a default remote host with Connect. 还可以在调用BeginSend之前更改默认远程主机, 方法是Connect调用或BeginConnectYou 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. 如果是这样, 则不会发送数据报并BeginSendSocketException引发。If it does, the datagram will not be sent and BeginSend will throw a SocketException.

如果指定DontRoute标志socketflags作为参数, 则不会路由要发送的数据。If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

给定线程启动的所有 i/o 都将在该线程退出时取消。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.

备注

state是用户定义的类的实例化。state is an instantiation of a user-defined class.

备注

成功完成发送并不表示数据已成功传递。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

备注

为异步Socket方法缓存执行上下文 (安全上下文、模拟用户和调用上下文)。The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. 第一次使用特定的上下文 (特定的异步Socket方法、特定Socket的实例和特定的回调) 后, 该上下文的后续使用将会提高性能。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.

另请参阅

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

将数据异步发送到连接的 SocketSends 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

参数

buffer
Byte[]

Byte 类型的数组,它包含要发送的数据。An array of type Byte that contains the data to send.

offset
Int32 Int32 Int32 Int32

buffer 参数中开始发送数据的位置,该位置从零开始计数。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

SocketFlags 值的按位组合。A bitwise combination of the SocketFlags values.

errorCode
SocketError 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.

返回

引用异步发送的 IAsyncResultAn IAsyncResult that references the asynchronous send.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参见下面的备注部分。See remarks section below.

offset 小于 0。offset is less than 0.

-or- offset 小于 buffer 的长度。offset is less than the length of buffer.

-or- size 小于 0。size is less than 0.

- 或 --or- size 大于 buffer 的长度减去 offset 参数的值。size is greater than the length of buffer minus the value of the offset parameter.

注解

Connect方法对BeginConnect、 、或BeginAccept方法中建立的远程主机启动异步发送操作。 Accept BeginSendThe BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend如果你不Accept首先调用Connect、、或BeginConnect, BeginAccept将引发异常。BeginSend will throw an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. 通过调用BeginSend方法, 可以在单独的执行线程中发送数据。Calling the BeginSend method gives you the ability to send data within a separate execution thread.

你可以创建一个实现AsyncCallback委托的回调方法, 并将其名称传递BeginSend给方法。You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method. 为此, 你state的参数至少必须包含用于通信的已连接或默认值。 SocketTo do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. 如果回调需要更多的信息, 可以创建一个小型类或结构来保存Socket和其他所需的信息。If your callback needs more information, you can create a small class or structure to hold the Socket and the other required information. 通过参数将此类的一个实例BeginSend传递给方法。 statePass an instance of this class to the BeginSend method through the state parameter.

回调方法应调用EndSend方法。Your callback method should invoke the EndSend method. 当应用程序调用BeginSend时, 系统将使用一个单独的线程来执行指定的回调方法, 并在EndSend Socket发送请求的字节数或引发异常之前阻止。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. 如果希望在调用BeginSend方法后原始线程会被阻止, 请WaitHandle.WaitOne使用方法。If you want the original thread to block after you call the BeginSend method, use the WaitHandle.WaitOne method. 如果希望原始线程继续执行, 请在回调方法中的 T:System.Threading.ManualResetEvent 上调用 Set 方法。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.

尽管适用于面向连接的协议, BeginSend但也适用于无连接协议, 前提是你首先Connect调用BeginConnect或方法来建立默认远程主机。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. 如果你使用的是无连接协议并且打算将数据发送到多个不同的主机, BeginSendTo则应使用。If you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. 即使已使用建立默认BeginSendTo远程Connect主机, 也可以使用。It is okay to use BeginSendTo even after you have established a default remote host with Connect. 还可以在调用BeginSend之前更改默认远程主机, 方法是Connect调用或BeginConnectYou 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. 如果是这样, 则不会发送数据报并BeginSendSocketException引发。If it does, the datagram will not be sent and BeginSend will throw a SocketException.

如果指定DontRoute标志socketflags作为参数, 则不会路由要发送的数据。If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

给定线程启动的所有 i/o 都将在该线程退出时取消。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.

备注

state是用户定义的类的实例化。state is an instantiation of a user-defined class.

备注

成功完成发送并不表示数据已成功传递。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

备注

为异步Socket方法缓存执行上下文 (安全上下文、模拟用户和调用上下文)。The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. 第一次使用特定的上下文 (特定的异步Socket方法、特定Socket的实例和特定的回调) 后, 该上下文的后续使用将会提高性能。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.

另请参阅

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)

将数据异步发送到连接的 SocketSends 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

参数

buffer
Byte[]

Byte 类型的数组,它包含要发送的数据。An array of type Byte that contains the data to send.

offset
Int32 Int32 Int32 Int32

buffer 参数中开始发送数据的位置,该位置从零开始计数。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

SocketFlags 值的按位组合。A bitwise combination of the SocketFlags values.

state
Object Object Object Object

一个对象,它包含此请求的状态信息。An object that contains state information for this request.

返回

引用异步发送的 IAsyncResultAn IAsyncResult that references the asynchronous send.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参见下面的备注部分。See remarks section below.

offset 小于 0。offset is less than 0.

- 或 --or- offset 小于 buffer 的长度。offset is less than the length of buffer.

-or- size 小于 0。size is less than 0.

-or- size 大于 buffer 的长度减去 offset 参数的值。size is greater than the length of buffer minus the value of the offset parameter.

示例

下面的代码示例开始将数据异步发送到远程主机。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

注解

Connect方法对BeginConnect、 、或BeginAccept方法中建立的远程主机启动异步发送操作。 Accept BeginSendThe BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend如果你不Accept首先调用Connect、、或BeginConnect, BeginAccept将引发异常。BeginSend will throw an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. 通过调用BeginSend方法, 可以在单独的执行线程中发送数据。Calling the BeginSend method gives you the ability to send data within a separate execution thread.

你可以创建一个实现AsyncCallback委托的回调方法, 并将其名称传递BeginSend给方法。You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method. 为此, 你state的参数至少必须包含用于通信的已连接或默认值。 SocketTo do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. 如果回调需要更多的信息, 可以创建一个小型类或结构来保存Socket和其他所需的信息。If your callback needs more information, you can create a small class or structure to hold the Socket and the other required information. 通过参数将此类的一个实例BeginSend传递给方法。 statePass an instance of this class to the BeginSend method through the state parameter.

回调方法应调用EndSend方法。Your callback method should invoke the EndSend method. 当应用程序调用BeginSend时, 系统将使用一个单独的线程来执行指定的回调方法, 并在EndSend Socket发送请求的字节数或引发异常之前阻止。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. 如果希望在调用BeginSend方法后原始线程会被阻止, 请WaitHandle.WaitOne使用方法。If you want the original thread to block after you call the BeginSend method, use the WaitHandle.WaitOne method. 如果希望原始线程继续执行, 请在回调方法中的 T:System.Threading.ManualResetEvent 上调用 Set 方法。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.

尽管适用于面向连接的协议, BeginSend但也适用于无连接协议, 前提是你首先Connect调用BeginConnect或方法来建立默认远程主机。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. 如果你使用的是无连接协议并且打算将数据发送到多个不同的主机, BeginSendTo则应使用。If you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. 即使已使用建立默认BeginSendTo远程Connect主机, 也可以使用。It is okay to use BeginSendTo even after you have established a default remote host with Connect. 还可以在调用BeginSend之前更改默认远程主机, 方法是Connect调用或BeginConnectYou 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. 如果是这样, 则不会发送数据报并BeginSendSocketException引发。If it does, the datagram will not be sent and BeginSend will throw a SocketException.

如果指定DontRoute标志socketflags作为参数, 则不会路由要发送的数据。If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

给定线程启动的所有 i/o 都将在该线程退出时取消。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.

备注

state是用户定义的类的实例化。state is an instantiation of a user-defined class.

备注

成功完成发送并不表示数据已成功传递。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

备注

为异步Socket方法缓存执行上下文 (安全上下文、模拟用户和调用上下文)。The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. 第一次使用特定的上下文 (特定的异步Socket方法、特定Socket的实例和特定的回调) 后, 该上下文的后续使用将会提高性能。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.

另请参阅

适用于