Socket.BeginSendFile Method (String, AsyncCallback, Object)

Note: This method is new in the .NET Framework version 2.0.

Sends the file fileName to a connected Socket object using the UseDefaultWorkerThread flag.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

public IAsyncResult BeginSendFile (
	string fileName,
	AsyncCallback callback,
	Object state
public IAsyncResult BeginSendFile (
	String fileName, 
	AsyncCallback callback, 
	Object state
public function BeginSendFile (
	fileName : String, 
	callback : AsyncCallback, 
	state : Object
) : IAsyncResult



A string that contains the path and name of the file to send. This parameter can be a null reference (Nothing in Visual Basic).


The AsyncCallback delegate.


An object that contains state information for this request.

Return Value

An IAsyncResult object that represents the asynchronous send.

Exception typeCondition


The Socket object has been closed.


The socket is not connected to a remote host.


The file fileName was not found.


An error occurred when attempting to access the socket. See remarks section below.

This overload sends the file fileName to the connected socket. If fileName is in the local directory, it may be identified with just the name of the file; otherwise, the full path and name of the file must be specified. Wildcards ("..\\myfile.txt") and UNC share names ("\\\\shared directory\\myfile.txt") are supported. If the file is not found, the exception FileNotFoundException is thrown.

This method uses the TransmitFile function found in the Windows Sockets 2 API. For more information about the TransmitFile function and its flags, see the Windows Sockets documentation in the MSDN Library.

The BeginSendFile method starts an asynchronous send operation to the remote host established in the Connect, BeginConnect, Accept, or BeginAccept methods. BeginSendFile throws an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. Calling the BeginSendFile method enables you to send a file within a separate execution thread.

To complete the operation, you can create a callback method that is invoked by the AsyncCallback delegate parameter. To do this, at the very minimum, the state parameter must contain the Socket object being used for communication. If your callback needs more information, you can create a class or structure to hold the Socket and the other required information. Pass an instance of this custom object to the BeginSendFile method through the state parameter.

Your callback method must invoke the EndSendFile method. When your application calls BeginSendFile, the system uses a separate thread to execute the specified callback method, and blocks on EndSendFile until the Socket sends the entire file or throws an exception. For additional information on writing callback methods see Callback Sample.

Although intended for connection-oriented protocols, BeginSendFile also works for connectionless protocols, provided that you first call the Connect or BeginConnect method to establish a default remote host. With connectionless protocols, you must be sure that the size of your file does not exceed the maximum packet size of the underlying service provider. If it does, the datagram is not sent and BeginSendFile throws a SocketException exception.


If you receive a SocketException exception, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.


This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.


The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

The following code example creates and connects a socket for asynchronous communication. First, the file "text.txt" is sent asynchronously to the remote host. The callback delegate calls EndSendFile to complete the transmission.

public static void AsynchronousFileSend()
    // Send a file to a remote device.
    // Establish the remote endpoint for the socket.
    IPHostEntry ipHostInfo = Dns.Resolve(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.
        new AsyncCallback(ConnectCallback), client);
    // Wait for connect.

    // There is a text file test.txt in the root directory.
    string fileName = "C:\\test.txt";
    // Send file fileName to the remote device.
    client.BeginSendFile(fileName, new AsyncCallback(FileSendCallback), client);

    // Release the socket.

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.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0