Socket.EndSendFile(IAsyncResult) 方法

定义

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

public:
 void EndSendFile(IAsyncResult ^ asyncResult);
public void EndSendFile (IAsyncResult asyncResult);
member this.EndSendFile : IAsyncResult -> unit
Public Sub EndSendFile (asyncResult As IAsyncResult)

参数

asyncResult
IAsyncResult

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

例外

此方法需要 Windows NT。Windows NT is required for this method.

Socket 对象已关闭。The Socket object has been closed.

asyncResult 为空。asyncResult is empty.

BeginSendFile(String, AsyncCallback, Object) 方法的调用未返回 asyncResultasyncResult was not returned by a call to the BeginSendFile(String, AsyncCallback, Object) method.

尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参见下面的备注部分。See remarks section below.

示例

下面的代码示例创建并连接套接字用于异步通信,并开始将文件 "text.txt" 异步发送到远程主机。The following code example creates and connects a socket for asynchronous communication and begins sending the file "text.txt" asynchronously to the remote host. 回调委托调用 EndSendFile 来完成传输。The callback delegate calls EndSendFile to complete the transmission.

   static void AsynchronousFileSend()
   {
      // Send a file to a remote device.
      // Establish the remote endpoint for the socket.
      IPHostEntry^ ipHostInfo = Dns::GetHostEntry( Dns::GetHostName() );
      IPAddress^ ipAddress = ipHostInfo->AddressList[ 0 ];
      IPEndPoint^ remoteEP = gcnew IPEndPoint( ipAddress,11000 );

      // Create a TCP/IP socket.
      Socket^ client = gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp );

      // Connect to the remote endpoint.
      client->BeginConnect( remoteEP, gcnew AsyncCallback( ConnectCallback ), client );

      // Wait for connect.
      connectDone->WaitOne();

      // There is a text file test.txt in the root directory.
      String^ fileName = "C:\\test.txt";

      // Send file fileName to the remote device.
      Console::WriteLine( fileName );
      client->BeginSendFile( fileName, gcnew AsyncCallback( FileSendCallback ), client );

      // Release the socket.
      client->Shutdown( SocketShutdown::Both );
      client->Close();
   }

private:
   static void FileSendCallback( IAsyncResult^ ar )
   {
      // Retrieve the socket from the state object.
      Socket^ client = dynamic_cast<Socket^>(ar->AsyncState);

      // Complete sending the data to the remote device.
      client->EndSendFile( ar );
      sendDone->Set();
   }

public static void AsynchronousFileSend()
{
    // Send a file to a remote device.

    // Establish the remote endpoint for the socket.
    IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

    // Create a TCP/IP socket.
    Socket client = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);

    // Connect to the remote endpoint.
    client.BeginConnect(remoteEP,
        new AsyncCallback(ConnectCallback), client);

    // Wait for connect.
    connectDone.WaitOne();

    // There is a text file test.txt in the root directory.
    string fileName = "C:\\test.txt";

    // Send file fileName to the remote device.
    Console.WriteLine(fileName);
    client.BeginSendFile(fileName, new AsyncCallback(FileSendCallback), client);

    // Release the socket.
    client.Shutdown(SocketShutdown.Both);
    client.Close();
}

private static void FileSendCallback(IAsyncResult ar)
{
    // Retrieve the socket from the state object.
    Socket client = (Socket) ar.AsyncState;

    // Complete sending the data to the remote device.
    client.EndSendFile(ar);
    sendDone.Set();
}

注解

EndSendFile 完成在中启动的异步发送操作 BeginSendFileEndSendFile completes the asynchronous send operation started in BeginSendFile.

在调用之前 BeginSendFile ,必须创建实现委托的回调方法 AsyncCallbackBefore calling BeginSendFile, you must create a callback method that implements the AsyncCallback delegate. 此回调方法在单独的线程中执行,并在返回后由系统调用 BeginSendFileThis callback method executes in a separate thread and is called by the system after BeginSendFile returns. 回调方法必须接受 IAsyncResult 由方法返回的对象 BeginSendFile 作为参数。The callback method must accept the IAsyncResult object returned by the BeginSendFile method as a parameter.

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

如果使用的是无连接协议,则在 EndSendFile 发送数据报之前阻止。If you are using a connectionless protocol, EndSendFile blocks until the datagram is sent. 如果你使用的是面向连接的协议,则会 EndSendFile 阻止,直到发送整个文件。If you are using a connection-oriented protocol, EndSendFile blocks until the entire file is 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. 此方法成功完成 BeginSendFile 意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the BeginSendFile 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.

适用于