Introduction to Peer-to-peer Technology
Peer-to-peer, the abbreviation for English peer-to-peer, is translated into peer-to-peer or point-to-point technology. Peer-to-peer technology allows users to directly connect to other users of the computer, file sharing and exchange, at the same time Peer-to-peer in depth search, distribution calculation, collaborative work, etc. are also useful.
Simply put, Peer-to-peer is a technique for exchanging data or services directly between different PC users without relaying, allowing Internet users to directly use each other's files. Everyone can connect directly to other users ' computers and Exchange files without having to connect to the server for browsing and downloading.
The current storage mode of the Internet is "content in the center", and Peer-to-peer technology will make content on the internet move to the edge. This will bring the following changes:
First, customers no longer need to upload files to the server, but only use Peer-to-peer to share with other computers;
Second, computers that use Peer-to-peer technology do not need a fixed IP address and a permanent Internet connection, which makes it possible for a large proportion of dial-up users to enjoy peer-to-peer change.
Example Introduction
The best way to understand Peer-to-peer technology is to carefully observe and understand an actual peer-to-peer application. As an important piece of Microsoft. NET Strategy, C # provides a good support and optimization for network programming. This article through a program, to introduce you to the C # Peer-to-peer programming methods and implementation mechanism. This program is not very useful, but it is very intuitive to give Peer-to-peer (point-to-point) programming and set interface programming some of the basic knowledge and concepts. It is based on the two classes of TcpListener and TcpClient. The principle of program implementation is relatively simple, but the use of Peer-to-peer technology to return to the "non-central" principle. In short, this program can send and receive messages on the network, and any computer can serve as both a server and a client.
System Requirements:
1. Widows 2000 Server version of the operating system or subsequent versions of the operating system.
2. Official version of the Visual Studio.NET framework.
Specific methods:
First, open vs.net, create a new C # project (Note: The template is a Windows application), and name it "P2pchat." This is illustrated below:
Second, referring to ICQ, Oicq and other chat tools, we can arrange the main interface of the program and the common Chat Tool message Send dialog box similar layout. However, because the program is only a main interface, you also need to add some such as "Start Listening", "Stop Listening" and other control buttons. At the same time, the program supports nickname display. So, according to this idea, we can start to lay out the main interface of the program. First, drag the following controls to the main interface: Two label controls, three button controls, three TextBox controls, one RichTextBox control, and one StatusBar control.
The properties of each control are set as shown in the following table:
Form1 |
Text Property |
Peer-to-peer Chat Tool |
Label1 |
Text Property |
Destination computer Address: |
Testslign Property |
MiddleCenter |
Label2 |
Text Property |
Nickname: |
TextAlign Property |
MiddleCenter |
Button1 |
Text Property |
Start listening. |
FlatStyle Property |
Flat |
Button2 |
Text Property |
Stop listening |
FlatStyle Property |
Flat |
Enabled Property |
False |
Anchor Property |
Top, right |
Button3 |
Text Property |
Send a message |
FlatStyle Property |
Flat |
Anchor Property |
Top, right |
TextBox1 |
Text Property |
Empty |
Anchor Property |
Top, left, right |
TextBox2 |
Text Property |
Empty |
Multiline property |
True |
Anchor Property |
Top, left, right |
TextBox3 |
Text Property |
Empty |
ForeColor Property |
Firebrick |
Anchor Property |
Top, left, right |
RichTextBox1 |
Text Property |
Empty |
ForeColor Property |
Steelblue |
ReadOnly Property |
True |
Anchor Property |
Top, Bottom, left, right |
Note: The Anchor property is used to set the layout of controls on a form, and when the form size changes how each control is distributed on the form, the reader can refer to the more detailed introductory article to understand.
The other properties are the default, and the final layout of the main form interface looks like this:
Now to the main part of the program, the Code section. Before giving the code, I'd like to introduce you to the basics of implementation and some of the logical relationships.
First, after the program is run, the user must first press the "Start listening" button, the button corresponding events, the program entered the listening state, the status bar has a corresponding display. In this way, the machine is equivalent to a server in server/client mode, and other computers can connect to the local computer and send messages to the local machine. Other computers through the program connected to the computer through the IP address to achieve, C # for network programming has a good support, so the programmer's workload is relatively small. This way, a computer can send a message to another station. However, this is a peer-to-peer program, so as long as the other computer users also press the "Start listening" button, the computer has become the server of this computer. Therefore, the realization of the messaging function, but the real server is non-existent, each computer is a server, each computer is also a client, which embodies the peer-to-peer technology of "non-central" principle.
The program mainly uses a listen () function and a Send () function. The former realizes the monitoring function of the program, the function realizes as follows:
private void Listen () {try
{
Tcpl = new TcpListener (5656);
Tcpl. Start ();
Statusbar1.text = "is 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 = "has stopped listening. ";
}
}
The body of the function is a try-catch statement, and the try part is a while loop, which means that the program is listening as long as the user does not press the "Stop listening" button. The port being monitored is 5656, and the port can be defined by itself, as long as it is not confused with the commonly used ports. Once the program receives a message from the remote computer, the message is added to the Message display box (the Message display box is the RichTextBox control). The catch part of the function catches some exceptions, such as setting up a firewall between the users, cannot communicate with each other, or the other party has stopped listening, of course, can not send a message to it. Another function send () is the ability to implement a message from a program. The function implementation is 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 = "The target computer rejected the connection request. ";
}
}
The body part of the function is also a try-catch statement that first establishes a connection to the remote computer based on the user's input, notices that its port is 5656, and must be 5656, in order to be consistent with the receiver port so that the other party receives the message sent here. The function then sends a message to the remote computer based on what the user has in the Message entry box and the user's nickname. In this way, as long as the network is not faulted, the remote computer is already listening, it can receive the message sent here. Of course, this is a listening state, and remote computers can also send messages here freely. The catch part of the function is also used to catch some exceptions.
At the same time also note that, because the program used a lot of network programming needs of the object as well as input and output objects, and the use of multithreaded programming mechanism, so at the beginning of the program to add some of the following namespaces:
Using System.IO;
Using System.Net.Sockets;
Using System.Threading;
Finally, the event handler functions for each control in the program and the complete code refer to the source code package included with the article. The program runs with the following diagram:
Now a very basic Peer-to-peer application and completion, through which we can use the basic characteristics of peer-to-peer technology to achieve point-to-point communication. Through this program, I believe that you should have a general understanding of the Peer-to-peer programming under C #. For this program, the disadvantage is that the function is relatively simple, can only send, receive information, and not through the firewall to communicate, readers can try to develop a more powerful peer-to-peer applications.
Summary
Finally, I hope that this article can arouse everyone's interest in Peer-to-peer technology. Because the hidden behind the incomparable creativity of the future of the Internet is full of good vision, now the world's Peer-to-peer application boom is also a wave high. In the foreseeable future, with the further deepening of peer-to-peer research and attention to peer-to-peer groups gradually increased, peer-to-peer will enter a new era of rapid development. However, the domestic peer-to-peer start relatively late, so more need to have a lot of technical research input and enough attention to win better development. In this, I hope that the domestic peer-to-peer can achieve brilliant development.