Realize file transfer with Socket
System.sockes namespace implements the Berkeley socket interface. Through this class, we can implement messaging and sending between network computers. In this topic I have to discuss, we will discuss the transmission of files to implement files with a set of junctions. This method is different from the FTP protocol The implementation of the file transmission method, using FTP methods require a special server and client, undoubtedly the file transfer we want to achieve is too complicated. Here, we implements a lightweight approach to realize the file transfer of point-to-point points, which reaches the file sharing of two computers on Intenet.
Before two computers transmit files, you must have a computer to establish a set of set of junction and bind a fixed port, and listen to another computer connection request at this port.
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 there are other computers issued a connection request, the requested computer assigns a thread for each connection request to handle file transfer and other services.
While (True)
{
Clientsock = Socket.accept ();
clientsock.connected
{
Thread TC = New Thread (New ThreadStart (ListenClient));
Tc.start ();
}
}
The following code shows how the ListenClient method handles the request from another computer. First, the sending request string makes judgments to see if the request is requested, and then determine the corresponding processing method.
void listenclient ()
{
Socket Sock = Clientsock;
Try
{
While (Sock! = NULL)
{
Byte [] recs = new byte [32767];
Int rount = sock.receive (Recs, Recs.length, 0);
String message = system.text.encoding.ascii.getstring (recs);
// Processing the message, the resolution request character and parameters are stored in CMDLIST
Execmd = cmdlist [0];
Sender = NULL;
Sender = new byte [32767];
String parm1 = "";
// directory list
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);
// Go to the file download processing
Downloadfile (SOCK);
CONTINUE;
}
}
}
Catch (Exception SE)
{
String s = se.Message; console.writeLine (s);
}
}
At this point, the basic work has been completed, let's see how to handle file transfer.
While (RDBY { / / Read the data of the specified length from the file to be transmitted Len = fin.read (buffed, 0, buffed.length); // Send the data read to the corresponding computer NFS.WRITE (Buffed, 0, Len); / / Increase the length that has been sent RDBY = RDBY LEN; } As can be seen from the code, the completion file is converted to the FILESTREAM stream, and then the corresponding set of son is bound through the NetworkStream, and finally calls his WRITE method to the corresponding computer. Let's take a look at how the accepted stream is transmitted and converted into documents: NetworkStream NFS = New NetworkStream (SOCK); Try { // Continue until the specified file length While (RBY { Byte [] buffer = new byte [1024]; // Read the sending 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, it is very simple to accept and send it. At this point, the single-directional file transmission is completed, and only the above-mentioned transmission and acceptance processing code can be used to transmit the files at the same time on each peer node.