C # Using P2P technology to realize point-to-point chat

xiaoxiao2021-03-06  108

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 the edge. This will bring the following changes: First, the customer no longer needs to upload the file to the server, but only need to share the P2P and other computers; secondly, the computer using P2P technology does not require a fixed IP address and permanent Internet connection, this Make a great proportion of dial-up users can also enjoy the changes brought by P2P. An example introduces the best way to understand P2P technology is 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 C # project (Note: Template for Windows Applications), you may wish to name "p2pchat". The image is shown in: Second, refer to the chat tool such as ICQ, OICQ, we can arrange the main interface of the program into a layout similar to the messages of 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 arranged the main form interface as follows: Now, the main part of the program, ie the code section. 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 = "is Stop listening! "; 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.

Function to achieve the following: 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 Denial Connection Request!"; It is also 5656, and must be 5656, which is the same as the receiver 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 many network programming the object and the input output object, the multi-threaded programming mechanism is used, so the following names are added in the start of the program: use 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 graph of the program is shown below: Now a very basic P2P utility and complete, 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. Summary Finally, the author hopes that you can send you an interest in P2P technology through this call. Because the incomparable creativity behind P2P makes people full of beautiful embarrassment for future Internet, the world's scope P2P application boom is also a wave.

转载请注明原文地址:https://www.9cbs.com/read-104736.html

New Post(0)