Socket.SendToAsync(SocketAsyncEventArgs) Socket.SendToAsync(SocketAsyncEventArgs) Socket.SendToAsync(SocketAsyncEventArgs) Socket.SendToAsync(SocketAsyncEventArgs) Method

定义

以异步方式将数据发送到特定远程主机。Sends data asynchronously to a specific remote host.

public:
 bool SendToAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool SendToAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.SendToAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function SendToAsync (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 参数中指定的 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 尚未连接。The protocol specified is connection-oriented, but the Socket is not yet connected.

注解

SendToAsync方法启动异步发送操作中指定远程主机SocketAsyncEventArgs.RemoteEndPoint属性的e参数。The SendToAsync method starts an asynchronous send operation to the remote host specified in the SocketAsyncEventArgs.RemoteEndPoint property of the e parameter. 调用SendToAsync方法使你能够发送单独的执行线程中的数据。Calling the SendToAsync method gives you the ability to send data within a separate execution thread. 尽管此方法适用于无连接协议,SendToAsync适用于无连接和面向连接的协议。Although this method is intended for connectionless protocols, SendToAsync works with both connectionless and connection-oriented protocols.

若要在完成时得到通知,必须创建实现事件处理程序的回调方法<SocketAsyncEventArgs > 委托并将附加到回调SocketAsyncEventArgs.Completed事件。To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and attach the callback to the SocketAsyncEventArgs.Completed event.

下列属性和事件上的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属性设置为在调用之前所需的任何用户状态对象SendToAsync方法,以便可在回调方法中检索信息。The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the SendToAsync 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.

如果使用的是面向连接的协议,必须首先调用AcceptAcceptAsyncBeginAcceptBeginConnectConnect,或ConnectAsync方法。If you are using a connection-oriented protocol, you must first call the Accept, AcceptAsync, BeginAccept, BeginConnect, Connect, or ConnectAsync method. 否则为SendToAsync将引发SocketExceptionOtherwise SendToAsync will throw a SocketException. 当使用面向连接的协议SendToAsync方法将忽略SocketAsyncEventArgs.RemoteEndPoint属性并将数据发送到System.Net.EndPoint中建立AcceptAcceptAsyncBeginAcceptBeginConnectConnectConnectAsync方法。When using a connection-oriented protocol, the SendToAsync method will ignore the SocketAsyncEventArgs.RemoteEndPoint property and send data to the System.Net.EndPoint established in the Accept, AcceptAsync, BeginAccept, BeginConnect, Connect, or ConnectAsync method.

如果使用的是无连接协议,不需要建立默认远程主机使用BeginConnectConnect,或ConnectAsync方法,然后调用SendToAsyncIf you are using a connectionless protocol, you do not need to establish a default remote host with the BeginConnect, Connect, or ConnectAsync method prior to calling SendToAsync. 只需执行此操作,如果你想要调用BeginSendSendAsync方法。You only need to do this if you intend to call the BeginSend or SendAsync methods. 如果调用BeginConnectConnect,或ConnectAsync方法,然后调用SendToAsync,则SocketAsyncEventArgs.RemoteEndPoint属性将为该发送操作仅重写指定的默认远程主机。If you do call the BeginConnect, Connect, or ConnectAsync method prior to calling SendToAsync, the SocketAsyncEventArgs.RemoteEndPoint property will override the specified default remote host for that send operation only. 也不需要调用Bind方法。You are also not required to call the Bind method. 在这种情况下,基础服务提供程序将分配的最合适的本地网络 IP 地址和端口号。In this case, the underlying service provider will assign the most appropriate local network IP address and port number. 如果你想要选择可用端口的基础服务提供程序,请使用端口号 0。Use a port number of zero if you want the underlying service provider to select a free port. 如果你需要标识分配的本地网络 IP 地址和端口号,则可以使用LocalEndPoint属性后的SocketAsyncEventArgs.Completed事件收到信号状态,调用关联的委托。If you need to identify the assigned local network IP address and port number, you can use the LocalEndPoint property after the SocketAsyncEventArgs.Completed event is signaled and the associated delegates are called.

如果你想要将数据发送到的广播地址,则必须首先调用SetSocketOption方法,设置套接字选项SocketOptionName.Broadcast为 true。If you want to send data to a broadcast address, you must first call the SetSocketOption method and set the socket option for SocketOptionName.Broadcast to true. 您还必须确保在缓冲区的大小不超过基础服务提供程序的最大数据包大小。You must also be sure that the size of your buffer does not exceed the maximum packet size of the underlying service provider. 如果是这样,将不发送数据报并SendToAsync将引发SocketExceptionIf it does, the datagram will not be sent and SendToAsync will throw a SocketException.

如果指定中的 DontRoute 标志SocketAsyncEventArgs.SocketFlags属性中,不会将路由发送的数据。If you specify the DontRoute flag in the SocketAsyncEventArgs.SocketFlags property, the data you are sending will not be routed.

对于面向消息的套接字,必须格外小心不能超过基础传输的最大消息大小。For message-oriented sockets, care must be taken not to exceed the maximum message size of the underlying transport. 如果缓冲区的大小超过基础服务提供程序的最大数据包大小,不发送数据报并SendToAsync将引发SocketExceptionIf the size of the buffer exceeds the maximum packet size of the underlying service provider, the datagram is not sent and SendToAsync will throw a SocketException. 成功完成SendToAsync方法并不表示数据已成功传递。The successful completion of a SendToAsync method does not indicate that the data was successfully delivered.

适用于

另请参阅