Peer-to-peer This article discusses a simple way to design peer-to-peer Web applications.
Although there are many peer-to-peer networks that do not require an index server or a central server, each client can communicate directly with each other, but figure 1 below shows the basic workings of peer-to-peer networks, which generally include a central indexing server that does not store any files. It stores only information about all users who are logged on to the network, the IP address of the client, and the files provided by the user for sharing, and the client and the server use simple commands to communicate through the route connection.
When client a wants to find files shared by other clients on Peer-to-peer networks, the system does the following:
• Client A logs on to the index server with its own user name.
• Client a registers to the server the files that it wants to share with other users so that other users can find the files.
• Client A sends a request to the server to find a file that matches a certain input pattern.
• The index server searches its database for a given file name and returns the results of the search to client A:
• A client that provides the file, such as Client B.
• The IP address of the user.
• The name of the file it was searched for.
Once client a chooses the download option, client A connects to client B using the IP address returned by the search.
• Once you have successfully established a connection, you can notify the other party to start sending the file.
• After downloading, you should register your copy of the shared file with the index server.
Such peer-to-peer networks can be used to share any type of file, which can be used both on a local area network or on the Internet.
(Figure 1)
The C # language is very easy to use to develop peer-to-peer applications because of its good network support, especially the built-in support for the two classes TcpListener and TcpClient. Here is an example of a peer-to-peer application developed using C #:
public Mytcplistener (int port): base (port) {
} public void Stopme () { if (this. Server!= null) { This. Server.close (); } } } public class Transfer { Mytcplistener tcpl; Public Transfer () { Optionsloader ol = new Optionsloader (); int port = 8081; if (ol. Port > 0) { Port = ol. Port; } Else { Port = 8081; } This.tcpl = new Mytcplistener (port); }
public void Transfershutdown () { Tcpl. STOPME (); } public void Listenforpeers () { Try {
Encoding ASCII = encoding.ascii;
Tcpl. Start (); while (true) { Accept will be blocked until there is a connection Socket s = tcpl. AcceptSocket (); NetworkStream DataStream = new NetworkStream (s); String filename; byte[] Buffer = new byte[256]; Datastream.read (Buffer, 0, 256); filename = Encoding.ASCII.GetString (Buffer); StringBuilder sbfilename = new StringBuilder (filename); StringBuilder sbFileName2 = sbfilename.replace ("\", "\ \"); FileStream fs = new FileStream (sbfilename2.tostring (), FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader (FS); byte[] bytes = new byte[1024]; int read; while (read = reader. Read (bytes, 0, bytes. Length))!= 0) { Datastream.write (bytes, 0, read); } Reader. Close (); Datastream.flush (); Datastream.close (); } } catch (SocketException ex) { MessageBox.Show (ex. ToString ()); } } public void Downloadtoclient (string server, String remotefilename, String localfilename) { Try { TcpClient TCPC = new TcpClient (); byte[] Read = new byte[1024]; Optionsloader ol = new Optionsloader (); int port = 0; if (ol. Port > 0) { Port = ol. Port; } Else { The default port number, which can be set to the port number used Port = 8081; } Trying to connect to the server Iphostentry iphost = dns.resolve (server); string []aliases = iphost.aliases; ipaddress[] addr = iphost.addresslist;
IPEndPoint EP = new IPEndPoint (addr[0], port); Tcpc. Connect (EP);
Get Stream Object Stream s = tcpc. GetStream (); Byte[] B = Encoding.ASCII.GetBytes (remotefilename. ToCharArray ()); S.write (b, 0, b.length); int bytes; FileStream fs = new FileStream (LocalFilename, FileMode.OpenOrCreate); BinaryWriter w = new BinaryWriter (FS); Reads the stream object and converts it to an ASCII code while (bytes = S.read (read, 0, read. Length))!= 0) { W.write (read, 0, bytes); Read = new byte[1024]; } Tcpc. Close (); W.close (); Fs. Close (); } catch (Exception ex) { throw new Exception (ex. ToString ()); } } } } |
|