Socket.EndSend Socket.EndSend Socket.EndSend Socket.EndSend Method

定义

结束挂起的异步发送。Ends a pending asynchronous send.

重载

EndSend(IAsyncResult) EndSend(IAsyncResult) EndSend(IAsyncResult) EndSend(IAsyncResult)

结束挂起的异步发送。Ends a pending asynchronous send.

EndSend(IAsyncResult, SocketError) EndSend(IAsyncResult, SocketError) EndSend(IAsyncResult, SocketError) EndSend(IAsyncResult, SocketError)

结束挂起的异步发送。Ends a pending asynchronous send.

EndSend(IAsyncResult) EndSend(IAsyncResult) EndSend(IAsyncResult) EndSend(IAsyncResult)

结束挂起的异步发送。Ends a pending asynchronous send.

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

参数

asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

IAsyncResult,它存储此异步操作的状态信息。An IAsyncResult that stores state information for this asynchronous operation.

返回

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

异常

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

示例

下面的代码示例结束挂起的异步发送。The following code example ends a pending asynchronous send.

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

int send = s->EndSend( ar );

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

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

int send = s.EndSend(ar);

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

s.Close();
Public Shared Sub Send_Callback(ar As IAsyncResult)

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

注解

EndSend 完成异步发送操作中启动BeginSendEndSend completes the asynchronous send operation started in BeginSend.

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

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

如果您使用的是无连接协议,EndSend会阻塞,直到发送数据报。If you are using a connectionless protocol, EndSend will block until the datagram is sent. 如果使用面向连接的协议EndSend会阻塞,直到某些缓冲区已发送。If you are using a connection-oriented protocol, EndSend will block until some of the buffer was sent. 如果的返回值EndSend指示的缓冲区不是完全发送,调用BeginSend方法同样,修改要保存的未发送的数据的缓冲区。If the return value from EndSend indicates that the buffer was not completely sent, call the BeginSend method again, modifying the buffer to hold the unsent data.

则您发送的数据将立即显示在网络不能保证。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. 成功完成BeginSend方法意味着的基础系统已为你的数据的网络发送的缓冲区的空间。A successful completion of the BeginSend 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.

备注

由给定线程启动的所有 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.

备注

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

另请参阅

EndSend(IAsyncResult, SocketError) EndSend(IAsyncResult, SocketError) EndSend(IAsyncResult, SocketError) EndSend(IAsyncResult, SocketError)

结束挂起的异步发送。Ends a pending asynchronous send.

public:
 int EndSend(IAsyncResult ^ asyncResult, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int EndSend (IAsyncResult asyncResult, out System.Net.Sockets.SocketError errorCode);
member this.EndSend : IAsyncResult *  -> int
Public Function EndSend (asyncResult As IAsyncResult, ByRef errorCode As SocketError) As Integer

参数

asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

IAsyncResult,它存储此异步操作的状态信息。An IAsyncResult that stores state information for this asynchronous operation.

errorCode
SocketError SocketError SocketError SocketError

一个 SocketError 对象,它存储套接字错误。A SocketError object that stores the socket error.

返回

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

异常

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

注解

EndSend 完成异步发送操作中启动BeginSendEndSend completes the asynchronous send operation started in BeginSend.

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

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

如果您使用的是无连接协议,EndSend会阻塞,直到发送数据报。If you are using a connectionless protocol, EndSend will block until the datagram is sent. 如果使用面向连接的协议EndSend会阻塞,直到某些缓冲区已发送。If you are using a connection-oriented protocol, EndSend will block until some of the buffer was sent. 如果的返回值EndSend指示的缓冲区不是完全发送,调用BeginSend方法同样,修改要保存的未发送的数据的缓冲区。If the return value from EndSend indicates that the buffer was not completely sent, call the BeginSend method again, modifying the buffer to hold the unsent data.

则您发送的数据将立即显示在网络不能保证。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. 成功完成BeginSend方法意味着的基础系统已为你的数据的网络发送的缓冲区的空间。A successful completion of the BeginSend 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.

备注

由给定线程启动的所有 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.

备注

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

另请参阅

适用于