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.

注解

BeginSend方法启动异步发送操作中建立的远程主机ConnectBeginConnectAccept,或BeginAccept方法。The BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend 如果不首先调用将引发异常AcceptBeginAcceptConnect,或BeginConnectBeginSend 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参数必须包含已连接或默认Socket正用于通信。To 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方法通过state参数。Pass 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. 当你想要继续执行的原始线程时请在回调方法调用 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无连接协议,也适用,前提是先调用ConnectBeginConnect方法以建立默认远程主机。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. 如果使用无连接协议,并且打算将数据发送到多个不同的主机,则应使用BeginSendToIf you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. 可以使用BeginSendTo甚至已建立默认远程主机使用后ConnectIt is okay to use BeginSendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用BeginSend到另一个调用,从而ConnectBeginConnectYou 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. 如果是这样,将不发送数据报并BeginSend将引发SocketExceptionIf 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.

注解

BeginSend方法启动异步发送操作中建立的远程主机ConnectBeginConnectAccept,或BeginAccept方法。The BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend 如果不首先调用将引发异常AcceptBeginAcceptConnect,或BeginConnectBeginSend 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参数必须包含已连接或默认Socket正用于通信。To 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方法通过state参数。Pass 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. 在调用 Set 方法ManualResetEvent在回调方法时所需的原始线程继续执行。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无连接协议,也适用,前提是先调用ConnectBeginConnect方法以建立默认远程主机。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. 如果使用无连接协议,并且打算将数据发送到多个不同的主机,则应使用BeginSendToIf you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. 可以使用BeginSendTo甚至已建立默认远程主机使用后ConnectIt is okay to use BeginSendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用BeginSend到另一个调用,从而ConnectBeginConnectYou 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. 如果是这样,将不发送数据报并BeginSend将引发SocketExceptionIf 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.

注解

BeginSend方法启动异步发送操作中建立的远程主机ConnectBeginConnectAccept,或BeginAccept方法。The BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend 如果不首先调用将引发异常AcceptBeginAcceptConnect,或BeginConnectBeginSend 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参数必须包含已连接或默认Socket正用于通信。To 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方法通过state参数。Pass 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. 当你想要继续执行的原始线程时请在回调方法调用 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无连接协议,也适用,前提是先调用ConnectBeginConnect方法以建立默认远程主机。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. 如果使用无连接协议,并且打算将数据发送到多个不同的主机,则应使用BeginSendToIf you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. 可以使用BeginSendTo甚至已建立默认远程主机使用后ConnectIt is okay to use BeginSendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用BeginSend到另一个调用,从而ConnectBeginConnectYou 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. 如果是这样,将不发送数据报并BeginSend将引发SocketExceptionIf 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

注解

BeginSend方法启动异步发送操作中建立的远程主机ConnectBeginConnectAccept,或BeginAccept方法。The BeginSend method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept method. BeginSend 如果不首先调用将引发异常AcceptBeginAcceptConnect,或BeginConnectBeginSend 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参数必须包含已连接或默认Socket正用于通信。To 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方法通过state参数。Pass 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. 当你想要继续执行的原始线程时请在回调方法调用 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无连接协议,也适用,前提是先调用ConnectBeginConnect方法以建立默认远程主机。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. 如果使用无连接协议,并且打算将数据发送到多个不同的主机,则应使用BeginSendToIf you are using a connectionless protocol and plan to send data to several different hosts, you should use BeginSendTo. 可以使用BeginSendTo甚至已建立默认远程主机使用后ConnectIt is okay to use BeginSendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用BeginSend到另一个调用,从而ConnectBeginConnectYou 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. 如果是这样,将不发送数据报并BeginSend将引发SocketExceptionIf 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.

另请参阅

适用于