Socket.EndSendTo(IAsyncResult) Socket.EndSendTo(IAsyncResult) Socket.EndSendTo(IAsyncResult) Socket.EndSendTo(IAsyncResult) Method


结束挂起的、向指定位置进行的异步发送。Ends a pending asynchronous send to a specific location.

 int EndSendTo(IAsyncResult ^ result);
public int EndSendTo (IAsyncResult result);
member this.EndSendTo : IAsyncResult -> int
Public Function EndSendTo (result As IAsyncResult) As Integer


IAsyncResult IAsyncResult IAsyncResult IAsyncResult

IAsyncResult,它存储此异步操作的状态信息以及所有用户定义的数据。An IAsyncResult that stores state information and any user defined data for this asynchronous operation.


如果成功,则返回已发送的字节数;否则会返回无效 Socket 错误。If successful, the number of bytes sent; otherwise, an invalid Socket error.


尝试访问套接字时出错。An error occurred when attempting to access the socket.


下面的代码示例结束异步发送到特定位置。The following code example ends an asynchronous send to a specific location.

StateObject^ so = safe_cast<StateObject^>(ar->AsyncState);
Socket^ s = so->workSocket;

int send = s->EndSendTo( ar );

Console::WriteLine( "The size of the message sent was : {0}", send );

StateObject so = (StateObject) ar.AsyncState;
Socket s = so.workSocket;

int send = s.EndSendTo(ar);

      Console.WriteLine("The size of the message sent was :" + send.ToString());

   Dim so As StateObject = CType(ar.AsyncState, StateObject)
   Dim s As Socket = so.workSocket
   Dim send As Integer = s.EndSendTo(ar)
   Console.WriteLine(("The size of the message sent was :" + send.ToString()))
End Sub 'SendTo_Callback


EndSendTo 完成异步发送操作中启动BeginSendToEndSendTo completes the asynchronous send operation started in BeginSendTo.

然后再调用BeginSendTo,你需要创建实现一个回调方法AsyncCallback委托。Before calling BeginSendTo, you need to create a callback method that implements the AsyncCallback delegate. 该回调方法在一个单独的线程中执行,并由系统后调用BeginReceive返回。This callback method executes in a separate thread and is called by the system after BeginReceive returns. 回调方法必须接受IAsyncResult返回的BeginSendTo方法作为参数。The callback method must accept the IAsyncResult returned by the BeginSendTo method as a parameter.

在回调方法中,调用AsyncState方法IAsyncResult参数来获取发送SocketWithin the callback method, call the AsyncState method of the IAsyncResult parameter to obtain the sending Socket. 获取后Socket,可以调用EndSendTo方法成功完成发送操作并返回发送的字节数。After obtaining the Socket, you can call the EndSendTo method to successfully complete the send operation and return the number of bytes sent.

如果您使用的是无连接协议,EndSendTo会阻塞,直到发送数据报。If you are using a connectionless protocol, EndSendTo will block until the datagram is sent. 如果使用面向连接的协议EndSendTo会阻塞,直到发送请求的字节数。If you are using a connection-oriented protocol, EndSendTo will block until the requested number of bytes are sent. 则您发送的数据将立即显示在网络不能保证。There is no guarantee that the data you send will appear on the network immediately. 若要增加网络效率,基础系统可能会延迟传输,直到收集了大量传出数据。To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. 成功完成BeginSendTo方法意味着的基础系统已为你的数据的网络发送的缓冲区的空间。A successful completion of the BeginSendTo method means that the underlying system has had room to buffer your data for a network send.


如果你收到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.


当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.