The System. Sockes namespace completes the Berkeley socket interface. After this class, we can complete audio transmission and sending between network computers. In the topic I will discuss below, we will discuss how to use sockets to transfer files. this method is different from the file transmission method completed by the FTP protocol. The ftp method requires a special server and client, which is undoubtedly complicated for point-to-point file transmission. Here, we complete a lightweight method to complete point-to-point file transfer, so that the file is shared by any two computers on intenet.
Before two computers transmit files, a computer must establish a socket connection and bind a fixed port, and listen on the connection request of another computer.
Socket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
Socket. Blocking = true;
IPEndPoint computernode1 = new IPEndPoint (serverIpadress, 8080 );
Socket. Bind (computernode1 );
Socket. Listen (-1 );
When other computers send a connection request, the computer that is begged will allocate a thread for each connection request for processing file transfers and other services.
While (true)
{
Clientsock = socket. Accept ();
If (clientsock. Connected)
{
Thread tc = new Thread (new ThreadStart (listenclient ));
Tc. Start ();
}
}
The following code shows how the listenclient method handles the request sent by another computer. First, identify the sent request string to see what kind of request, and then determine the corresponding solution.
Void listenclient ()
{
Socket sock = clientsock;
Try
{
While (sock! = Null)
{
Byte [] recs = new byte [1, 32767];
Int rcount = sock. Receive (recs, recs. Length, 0 );
String message = System. Text. Encoding. ASCII. GetString (recs );
// Handle the message, and store the request characters and parameters in cmdList.
Execmd = cmdList [0];
Sender = null;
Sender = new Byte [32767];
String parm1 = "\";
// List Directories
If (execmd = "LISTING ")
{
ListFiles (message );
Continue;
}
// File Transfer
If (execmd = "GETOK ")
{
Cmd = "BEGINSEND" + filepath + "+ filesize;
Sender = new Byte [1024];
Sender = Encoding. ASCII. GetBytes (cmd );
Sock. Send (sender, sender. Length, 0 );
// Transfer to File Download disposal
DownloadingFile (sock );
Continue;
}
}
}
Catch (Exception Se)
{
String s = Se. Message;
Console. WriteLine (s );
}
}
Now, the fundamental work has been completed. Let's take a look at how to handle file transfers.
While (rdby <total & nfs. CanWrite)
{
// Read data of the specified length from the file to be transmitted
Len = fin. Read (buffed, 0, buffed. Length );
// Send the read data to the corresponding computer
Nfs. Write (buffed, 0, len );
// Increase the length of the sent message
Rdby = rdby + len;
}
From the code above, we can see that the file is converted into a FileStream stream, and then bound to the corresponding node through NetworkStream. Finally, we call its write method to send it to the corresponding computer.
Let's take a look at how the receiver receives the transmitted stream and converts it to a file:
NetworkStream nfs = new NetworkStream (sock );
Try
{
// Keep repeating until the specified file length
While (rby <size)
{
Byte [] buffer = new byte [1024];
// Read the sent file stream
Int I = nfs. Read (buffer, 0, buffer. Length );
Fout. Write (buffer, 0, (int) I );
Rby = rby + I;
}
Fout. Close ();
It can be seen from the above that the process of bearing and sending is exactly the opposite, which is very simple.
Now, dual-direction file transmission is complete. You only need to complete the above sending and processing code on each peer node at the same time to transfer files to each other.