The system. sockes namespace implements the Berkeley socket interface. Through this class, we can implement message transmission and transmission between network computers. in this topic that I will discuss below, we will discuss how to use a set of sub-nodes to transfer files. this method is different from the file transfer method implemented by the FTP protocol. Using the FTP method requires a dedicated server and client, which is undoubtedly complicated for point-to-point file transfer. Here, we implement a lightweight method to achieve point-to-point file transmission, which achieves file sharing between any two computers on intenet.
Before two computers transmit files, you must first have a computer to establish a node connection and bind a fixed port, and listen on connection requests from 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 another computer sends a connection request, the requested computer allocates a thread for each connection request to process file transfer and other services.
While (true)
{
Clientsock = socket. Accept ();
If (clientsock. Connected)
{
Thread Tc = new thread (New threadstart (listenclient ));
TC. Start ();
}
}
The following code demonstrates how the listenclient method processes requests sent from another computer. First, judge the sent request string to see what the request is and then determine the corresponding processing method.
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 );
// Process the message. The request characters and parameters at the resolution are stored 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
Downloadingfile (sock );
Continue;
}
}
}
Catch (exception SE)
{
String S = Se. message;
Console. writeline (s );
}
}
Now, the basic work has been completed. Let's take a look at how to handle file transfer.
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 sent Length
Rdby = rdby + Len;
}
From the code above, we can see that the file is converted to a filestream stream, and then bound to the corresponding node through networkstream, and finally called its write method to send it to the corresponding computer.
Let's look at how the receiver accepts 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 ();
From the above, we can see that the process of receiving and sending is exactly the opposite, which is very simple.
// Obtain the pre-saved file name
String filename = "test.rar ";
// Remote host
String hostname = textboxhost. Text. Trim ();
// Port
Int Port = 80;
// Obtain host information
Iphostentry ipinfo = DNS. gethostbyname (hostname );
// Obtain IPaddress []
IPaddress [] ipaddr = ipinfo. Addresslist;
// Obtain the IP address
IPaddress IP = ipaddr [0];
// Combine remote endpoints
Ipendpoint hostep = new ipendpoint (IP, Port );
// Create a socket instance
Socket socket = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
Try
{
// Try to connect
Socket. Connect (hostep );
}
Catch (exception SE)
{
Leixuncms. Common. MessageBox. Show (this. Page, "connection error" + Se. Message );
}