Socket.BeginSendFile Method (String, AsyncCallback, Object)
Assembly: System (in System.dll)
[<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)>] member 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 null.
An object that contains state information for this request.
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 Connect,BeginConnect, Accept, or BeginAccept methods. throws an exception if you do not first call Accept, BeginAccept, Connect, or BeginConnect. Calling the method enables you to send a file within a separate execution thread.method starts an asynchronous send operation to the remote host established in the
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 method through the state parameter.
Your callback method must invoke the EndSendFile method. When your application calls , 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 Marshaling a Delegate as a Callback Method.
Although intended for connection-oriented protocols, 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 throws a SocketException exception.also works for connectionless protocols, provided that you first call the
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 in the .NET Framework.
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.
Available since 2.0