Socket.SendAsync(SocketAsyncEventArgs) Socket.SendAsync(SocketAsyncEventArgs) Socket.SendAsync(SocketAsyncEventArgs) Socket.SendAsync(SocketAsyncEventArgs) Method

定义

将数据异步发送到连接的 Socket 对象。Sends data asynchronously to a connected Socket object.

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

参数

e
SocketAsyncEventArgs SocketAsyncEventArgs SocketAsyncEventArgs SocketAsyncEventArgs

要用于此异步套接字操作的 SocketAsyncEventArgs 对象。The SocketAsyncEventArgs object to use for this asynchronous socket operation.

返回

如果 I/O 操作挂起,则为 truetrue if the I/O operation is pending. 操作完成时,将引发 e 参数的 Completed 事件。The Completed event on the e parameter will be raised upon completion of the operation.

如果 I/O 操作同步完成,则为 falsefalse if the I/O operation completed synchronously. 在这种情况下,将不会引发 e 参数的 Completed 事件,并且可能在方法调用返回后立即检查作为参数传递的 e 对象以检索操作的结果。In this case, The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

异常

e 参数的 BufferBufferList 属性必须引用有效的缓冲区。The Buffer or BufferList properties on the e parameter must reference valid buffers. 可以设置这两个属性中的某一个,但不能同时设置这两个属性。One or the other of these properties may be set, but not both at the same time.

已经在使用 e 参数中指定的 SocketAsyncEventArgs 对象执行套接字操作。A socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

此方法需要 Windows XP 或更高版本。Windows XP or later is required for this method.

Socket 尚未连接或者尚未通过 Accept()AcceptAsync(SocketAsyncEventArgs)BeginAccept 方法获得。The Socket is not yet connected or was not obtained via an Accept(), AcceptAsync(SocketAsyncEventArgs),or BeginAccept, method.

注解

SendAsync方法用于从面向连接的套接字上的一个或多个缓冲区写入传出数据。The SendAsync method is used to write outgoing data from one or more buffers on a connection-oriented socket. 此方法也可用,但是,已对连接操作指定远程主机的无连接套接字上。This method can also be used, however, on connectionless sockets that have specified a remote host on a connect operation.

SendAsync方法启动异步发送操作中建立的远程主机AcceptAcceptAsyncBeginAcceptBeginConnectConnect,或ConnectAsync方法。The SendAsync method starts an asynchronous send operation to the remote host established in the Accept, AcceptAsync, BeginAccept, BeginConnect, Connect, or ConnectAsync method.

下列属性和事件上的System.Net.Sockets.SocketAsyncEventArgs成功调用此方法所需的对象:The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

调用方可能会设置SocketAsyncEventArgs.UserToken属性设置为在调用之前所需的任何用户状态对象SendAsync方法,以便可在回调方法中检索信息。The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the SendAsync method, so that the information will be retrievable in the callback method. 如果回调需要比单个对象的详细信息,可以创建一个小类来保存作为成员的其他所需的状态信息。If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

SendAsync方法将引发异常,如果不首先调用AcceptAcceptAsyncBeginAccept BeginConnectConnect,或ConnectAsyncThe SendAsync method will throw an exception if you do not first call Accept, AcceptAsync, BeginAcceptBeginConnect, Connect, or ConnectAsync.

调用SendAsync方法使你能够发送单独的执行线程中的数据。Calling the SendAsync method gives you the ability to send data within a separate execution thread.

对于面向消息的套接字,不超过基础 Windows 套接字服务提供程序的最大消息大小。For message-oriented sockets, do not exceed the maximum message size of the underlying Windows sockets service provider. 如果数据太长,以原子方式通过基础服务提供商,传输任何数据并SendAsync方法会抛出SocketExceptionSocketAsyncEventArgs.SocketError设置为本机 Winsock WSAEMSGSIZE 错误代码 (10040)。If the data is too long to pass atomically through the underlying service provider, no data is transmitted and the SendAsync method throws a SocketException with the SocketAsyncEventArgs.SocketError set to the native Winsock WSAEMSGSIZE error code (10040).

请注意,成功完成SendAsync方法并不表示数据已成功传递。Note that the successful completion of the SendAsync method does not indicate that the data was successfully delivered.

适用于

另请参阅