Socket.EndSendFile(IAsyncResult) Socket.EndSendFile(IAsyncResult) Socket.EndSendFile(IAsyncResult) Socket.EndSendFile(IAsyncResult) Method

定义

结束文件的挂起异步发送。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 IAsyncResult IAsyncResult

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

异常

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

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,必须创建实现一个回调方法AsyncCallback委托。Before calling BeginSendFile, you must create a callback method that implements the AsyncCallback delegate. 该回调方法在一个单独的线程中执行,并由系统后调用BeginSendFile返回。This 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.

适用于