System.Net.Sockets.Socket.BeginSendFile Method

Sends the file fileName to a connected System.Net.Sockets.Socket object using the TransmitFileOptions.UseDefaultWorkerThread flag.

Syntax

public IAsyncResult BeginSendFile (string fileName, AsyncCallback callback, object state)

Parameters

fileName
A string that contains the path and name of the file to send. This parameter can be null.
callback
The AsyncCallback delegate.
state
An object that contains state information for this request.

Returns

An IAsyncResult object that represents the asynchronous send.

Remarks

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 System.IO.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 Socket.BeginSendFile(string, AsyncCallback, object) method starts an asynchronous send operation to the remote host established in the Socket.Connect(System.Net.EndPoint), Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object), Socket.Accept, or Socket.BeginAccept(AsyncCallback, object) methods. Socket.BeginSendFile(string, AsyncCallback, object) throws an exception if you do not first call Socket.Accept, Socket.BeginAccept(AsyncCallback, object), Socket.Connect(System.Net.EndPoint), or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object). Calling the Socket.BeginSendFile(string, AsyncCallback, object) 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 System.Net.Sockets.Socket object being used for communication. If your callback needs more information, you can create a class or structure to hold the System.Net.Sockets.Socket and the other required information. Pass an instance of this custom object to the Socket.BeginSendFile(string, AsyncCallback, object) method through the state parameter.

Your callback method must invoke the Socket.EndSendFile(IAsyncResult) method. When your application calls Socket.BeginSendFile(string, AsyncCallback, object), the system uses a separate thread to execute the specified callback method, and blocks on Socket.EndSendFile(IAsyncResult) until the System.Net.Sockets.Socket sends the entire file or throws an exception. For additional information on writing callback methods see [<topic://cpconcallbacksample>].

Although intended for connection-oriented protocols, Socket.BeginSendFile(string, AsyncCallback, object) also works for connectionless protocols, provided that you first call the Socket.Connect(System.Net.EndPoint) or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) 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 Socket.BeginSendFile(string, AsyncCallback, object) throws a System.Net.Sockets.SocketException exception.

Note:

If you receive a System.Net.Sockets.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.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Note:

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

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0