Introduction
From. the SendFile method has been added to NET Framework 2.0. This method can easily send files in the specified path. Today, we will show you how to use Socket. the SendFile method is used as an example to send images, hoping to help friends who have just come into contact with Socket programming ,:
SendFile detailed parameters
Socket. SendFile method (String, Byte [], Byte [], TransmitFileOptions)
Use the specified TransmitFileOptions value to send the file fileName and data buffer to the connected Socket object.
Note that it is the Socket object of the connection, so it is only applicable to Socket connections of the TCP protocol.
Parameters
FileName
A String that contains the path and name of the file to be sent. This parameter can be a null reference (Nothing in Visual Basic ).
PreBuffer
A Byte array that contains the data to be sent before the file is sent. This parameter can be a null reference (Nothing in Visual Basic ).
PostBuffer
A Byte array that contains the data to be sent after the file is sent. This parameter can be a null reference (Nothing in Visual Basic ).
Flags
One or more TransmitFileOptions values.
For details, see Socket. SendFile.
Send and Receive
SendFile
/// <Summary>
/// Send the specified file
/// </Summary>
/// <Param name = "filename"> file path </param>
Public void SendFile (string filename)
{
FileInfo fi = new FileInfo (filename );
Byte [] len = BitConverter. GetBytes (fi. Length );
// First, send the file length
_ Client. BeginSendFile (filename,
Len,
Null,
TransmitFileOptions. usedefaworkworkerthread,
New AsyncCallback (SendFileCallback ),
Null );
}
Private void SendFileCallback (IAsyncResult iar)
{
_ Client. EndSendFile (iar );
}
ReceiveFile
Public void BeginReceive ()
{
Byte [] buffer = new byte [8];
// Since long occupies 8 bytes, the first 8 bytes of data are obtained first.
IAsyncResult iar = _ client. BeginReceive (
Buffer,
0,
Buffer. Length,
SocketFlags. None,
Null,
Null );
Int len = _ client. EndReceive (iar );
Int offset = 0;
Int length = BitConverter. ToInt32 (buffer, offset); // obtain the object length first.
ReceiveFile (length );
BeginReceive (); // continue receiving
}
Public void ReceiveFile (long filelen)
{
MemoryStream MS = new MemoryStream ();
Int bytesRead = 0;
Long count = 0;
Byte [] buffer = new byte [8192];
While (count! = Filelen)
{
BytesRead = _ client. Receive (buffer, buffer. Length, 0 );
Ms. Write (buffer, 0, bytesRead );
Count + = bytesRead;
}
ReceivedBitmap (new Bitmap (MS ));
}
Last
Although the SendFile method is very convenient to use, it also has its own advantages and disadvantages.
Advantage: A thread is automatically created when a file is sent. Even when a large file is sent, it does not affect the running of the main thread. You do not have to worry about the status of the sending process. The file will be automatically returned when it is sent.
Disadvantage: only files in the specified path can be sent, and no traffic has been sent during the sending process. Therefore, you cannot observe the status of the files sent.
You can use the SendFile method according to the system environment you designed. Although the disadvantages of SendFile make it difficult for us, it is very convenient to send small files, such as slice and text files.
Attached http://www.bkjia.com/uploadfile/2011/1122/20111122024523982.zip