Author: Liu Yanqing
This article discusses a simple way to design a P2P network application. Although there are many P2P networks that do not require an index server or a central server, each client can communicate with each other, but the following figure 1 still shows the basic working principle of the P2P network, in general, the P2P concept includes a central index Server, this server does not store any files, which stores information on all users logged in to the network, the client's IP address, and the user-provided files, clients, and servers use simple commands. The road connection communicates. When the client A wants to find a shared file on the P2P network, the system will perform the following: • The client A is logged on the index server with its own username. • Client A registering the server to the server to provide files shared by other users so that other users can find these files. • Client A issues an application to the server to find files matching a certain input pattern. • The index server searches for a given file name in its database, and returns the following result to the client A: • Provides the client of the file, such as the client B. · The user's IP address. · It searches for the file name. Once the client A selects the download option, the client A uses the IP address returned by the search to the client B to establish a connection. · Once a connection is successfully established, you can notify the other party to start sending files. · After the download is complete, you should register your copy of the shared file to the index server. Such P2P networks can be used to share any type of file, which can be used on a local area, or on the Internet.
C # Language Because of its good support for network features, especially the two classes of TCPListener and TCPClient, it is very easy to use it to develop P2P applications.
Here is an example of a P2P application using C # development: 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 (il.port> 0) {port =}} else = 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) { // Before you have a connection, Accept will be in block state 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); 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 =