Socket.EndSend 方法
定义
结束挂起的异步发送。Ends a pending asynchronous send.
重载
| EndSend(IAsyncResult) |
结束挂起的异步发送。Ends a pending asynchronous send. |
| EndSend(IAsyncResult, SocketError) |
结束挂起的异步发送。Ends a pending asynchronous send. |
EndSend(IAsyncResult)
结束挂起的异步发送。Ends a pending asynchronous send.
public:
int EndSend(IAsyncResult ^ asyncResult);
public:
int EndSend(IAsyncResult ^ result);
public int EndSend (IAsyncResult asyncResult);
public int EndSend (IAsyncResult result);
member this.EndSend : IAsyncResult -> int
member this.EndSend : IAsyncResult -> int
Public Function EndSend (asyncResult As IAsyncResult) As Integer
Public Function EndSend (result As IAsyncResult) As Integer
参数
- asyncResultresult
- 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.
例外
asyncResult 为 null。asyncResult is null.
对 BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) 方法的调用未返回 asyncResult。asyncResult was not returned by a call to the BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) method.
先前为异步发送已调用过 EndSend(IAsyncResult)。EndSend(IAsyncResult) was previously called for the asynchronous send.
尝试访问套接字时出错。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
注解
EndSend 完成在中启动的异步发送操作 BeginSend 。EndSend 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 以获取发送 Socket 。Within 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.
另请参阅
- BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object)
- AsyncCallback
- AsyncState
- SetSocketOption(SocketOptionLevel, SocketOptionName, Int32)
- NoDelay
适用于
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 * SocketError -> int
Public Function EndSend (asyncResult As IAsyncResult, ByRef errorCode As SocketError) As Integer
参数
- asyncResult
- IAsyncResult
IAsyncResult,它存储此异步操作的状态信息。An IAsyncResult that stores state information for this asynchronous operation.
- errorCode
- 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.
例外
asyncResult 为 null。asyncResult is null.
对 BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) 方法的调用未返回 asyncResult。asyncResult was not returned by a call to the BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) method.
先前为异步发送已调用过 EndSend(IAsyncResult)。EndSend(IAsyncResult) was previously called for the asynchronous send.
尝试访问套接字时出错。An error occurred when attempting to access the socket.
注解
EndSend 完成在中启动的异步发送操作 BeginSend 。EndSend 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 以获取发送 Socket 。Within 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.
另请参阅
- BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object)
- AsyncCallback
- AsyncState
- SetSocketOption(SocketOptionLevel, SocketOptionName, Int32)
- NoDelay