Introduction to P2P Technology P2P, English Peer-to-Peer abbreviation, Chinese translation as peer-to-peer interconnected or point-to-point technology. P2P technology allows users to connect directly to other users' computers, perform file sharing and exchange, and P2P is also useful in deep search, distribution calculation, collaborative work.
Simply put, P2P is a technique for exchange data or service directly between different PC users, which allows Internet users to directly use the other party. Each person can connect directly to other users' computers, and exchange for files, without having to connect to the server and browse and download.
At present, the storage mode of the Internet is "content in the center", while the application of P2P technology will cause the contents of the Internet to move from the edges of the INTERNET. This will bring the following changes:
First, customers no longer need to upload files to the server, but only need to share with other computers using P2P;
Second, the computer using P2P technology does not require a fixed IP address and a permanent Internet connection, which makes it a great proportion of dial-up users can also enjoy the changes brought by P2P. Example introduction
It is best to understand P2P technology to carefully observe and understand an actual P2P application. C # As an important chess piece of Microsoft .NET strategy, it provides good support and optimization on network programming. This article introduces you the method and implementation mechanism of P2P programming under C # through a program. This program in this article is not very useful, but it is very intuitive to give P2P (point-to-point) programming and some basic knowledge and concepts programming. It is based on two classes of TCPListener and TCPCLIENT. The principle of program implementation is also relatively simple, but the basic principles of P2P technology will return "unintrovation". In short, this program can be sent, accepting messages in the network, any computer can be used as a server side, but also as a client. System Requirements:
1. WIDOWS 2000 Server Edition operating system or subsequent version of the operating system.
2. Visual Studio.net Framework official version.
specific method:
First, open vs.net, create a new C # project (Note: Template for Windows Applications), you may be named "p2pchat". The illustration is as follows:
Secondly, with reference to the ICQ, OICQ and other chat tools, we can arrange the main interface of the program into a layout similar to the message sent dialog box for common chat tools. However, since the program is only one main interface, there is also some control buttons such as "Start Monitor", "Stop Listening". At the same time, the program is to support nicknames. So, according to this idea, we can start the main interface of the program. First, drag and drop some controls on the main interface: Two label controls, three Button controls, three TextBox controls, a RichTextBox control, and a StatusBar control. The properties of each control are set as shown in the following table:
Form1Text properties P2P chat tool Label1Text properties of the target computer addresses: TestSlign property MiddleCenterLabel2Text property Nickname: TextAlign property MiddleCenterButton1Text property starts listening FlatStyle property FlatButton2Text property to stop listening FlatStyle property FlatEnabled property FalseAnchor property Top, RightButton3Text property to send a message FlatStyle property FlatAnchor property Top, RightTextBox1Text property ( empty) Records property Top, Left, RightTextBox2Text attributes (empty) the Multiline properties TrueAnchor properties Top, Left, RightTextBox3Text attributes (empty) the ForeColor properties FirebrickAnchor properties Top, Left, RightRichTextBox1Text attributes (empty) the ForeColor properties SteelblueReadOnly properties TrueAnchor properties Top, Bottom, Left , Right Note: The Anchor property is to set up the control layout on the form. When the form size changes, the various controls are reset on the form, which is determined by this attribute. The reader can refer to more detailed introduction article.
Other properties are default, and finally the main form interface is as follows:
Now, the main part of the program, ie the code portion. Before giveing the code, I would like to introduce you to the basic principles of implementation and some of the logical relationships. First, after the program is running, the user has to press the "Start Listening" button. After the button is the corresponding event, the program enters the listening state, and the status bar has the corresponding display. In this way, the unit is equivalent to the server in the Server / Client mode, and other computers can connect to the unit and send messages to this machine. Other computers are connected to this machine through the IP address, and C # has good support to network programming, so the programmer's workload is relatively small. Thus, a computer can send a message to another. However, this is a P2P program, so as long as another computer is also pressed, the computer has become the server of this computer. So I realize the message mutual occurrence, however the real server does not exist, each computer is a server, each computer is also a client, which reflects the "unintrovization" principle of P2P technology.
The program mainly uses a listen () function and a send () function. The former realizes the monitoring function of the program, the function is implemented as follows:
Private void listen () {Try {TCPL = New TCPListener (5656); tcpl.start (); statusbar1.text = "Listening ..."; while (listenerrun) {socket s = tcpl.acceptsocket (); byte [ ] stream = new Byte [80]; int i = s.Receive (stream); string message = System.Text.Encoding.UTF8.GetString (stream); richTextBox1.AppendText (message);}} catch (System.Security. SecurityException) {MessageBox.show ("Firewall Security Error!", "Error", MessageBoxButtons.ok, MessageBoxicon.exclamation;} Catch (Exception) {statusbar1.text = "Stop listening!";}}}} A try-catch statement, the TRY section is another while loop, which means that only the user does not press the "Stop listening" button, the program will remain in the listening state. The port of the listener is 5656. This port can be defined by themselves, as long as you don't confuse the commonly used ports. Once the program receives a message of the remote computer, add the message to the message display (the message display is the RichTextBox control). The Catch section of the function is to capture some exceptions. If the firewall is set, it cannot be communicated with each other, or the other party has stopped listening, and it certainly can't send a message to it. Another function Send () is the function of implementing the program sends a message. The function is achieved as follows:
private void Send () {try {string msg = "<" textBox3.Text ">" textBox2.Text; TcpClient tcpc = new TcpClient (textBox1.Text, 5656); NetworkStream tcpStream = tcpc.GetStream (); StreamWriter reqStreamW = new StreamWriter (tcpStream); reqStreamW.Write (msg); reqStreamW.Flush (); tcpStream.Close (); tcpc.Close (); richTextBox1.AppendText (msg); textBox2.Clear ();} catch (Exception) {Statusbar1.text = "Target computer reject connection request!"; Moreover, it must be 5656, which is to be consistent with the receiving port, so that the other party can receive the message sent here. Next, the function sends a message according to the content of the user in the message input box and the user's nickname to the remote computer. In this way, as long as the network is not faulty, the remote computer is already in the listening state, it can receive the message sent here. Of course, here are listening, remote computers can also send messages here. The Catch section of the function is also used to capture some exceptions.
At the same time, it should be noted that since the program uses the objects required for many network programming and input output objects, the multi-threaded programming mechanism is used, so adding some namespaces in the beginning of the program:
Using system.io; using system.net.sockets; using system.threading;
Finally, the event handler of each control in the program and the complete code, please refer to the source code package included after the text. The image run is as follows:
Now a very basic P2P utilization program and completion, through it, we can use the basic characteristics of P2P technology to achieve point-to-point communication. Through this program, I believe that everyone should have a general understanding of P2P programming under C #. For this procedure, the shortcoming is that the function is relatively simple, only can be sent, accepted, and cannot communicate through the firewall, and the reader can try to develop a function of stronger P2P applications.
to sum up