Beijing siyuan training center-C # P2P Technology for point-to-point chat (2)

Source: Internet
Author: User
First, Program After running, you must first press the start listener button. After the corresponding event is clicked, the program enters the listener status and the status bar is displayed accordingly. In this way, the local machine is equivalent to a server in "server/client" mode. Other computers can connect to the local machine and send messages to the local machine. Other computers connect to the local machine through this program through IP addresses. C # has good support for network programming, so the workload of programmers is relatively small. In this way, one computer can send messages to the other. However, this is a P2P program, so as long as the user of another computer presses the "Start listening" button, the computer becomes the server of this computer. Therefore, the message sending function is implemented. However, the real server does not exist. Each computer is a server, and each computer is also a client, this reflects the "non-central" principle of P2P technology.

The program mainly uses a listen () function and a send () function. The former implements the program listening function, and the function implementation is as follows:

private void listen () {try
{< br> tcpl = new tcplistener (5656 );
tcpl. start ();
statusbar1.text = "listening... ";
while (listenerrun)
{< br> 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);
}< br>}
catch (system. Security. securityexception)
{< br> MessageBox. Show ("firewall security error! "," Error ",
messageboxbuttons. OK, messageboxicon. exclamation);
}< br> catch (exception)
{< br> statusbar1.text = "stopped listening! ";
}< BR >}

The main body of the function is a try-catch statement, and the try part is a while loop, which means that the program will remain in the listening State as long as the user does not press the "Stop listening" button. The listening port is 5656, which can be customized as long as it is not confused with common ports. Once the program receives a message from the remote computer, it adds the message to the message display box (the RichTextBox Control is the message display box ). The catch part of the function is used to catch exceptions. For example, if a firewall is set between users, users cannot communicate with each other, or the other party has stopped listening, of course, you cannot send messages to it. Another function, send (), implements the program's message sending function. Function implementation:

Private void send ()
{
Try
{
String MSG = "<" + textbox3.text + ">" + textbox2.text;
Tcpclient TCPC = new tcpclient (textbox 1.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 rejects the connection request! ";
}
}

The main part of the function is also a try-catch statement. It first establishes a connection with the remote computer based on the user input. Note that the port is also 5656 and must be 5656, this is to be consistent with the receiver port so that the recipient can receive the message sent here. Then, the function sends messages to the remote computer based on the user's content in the message input box and the user's nickname. In this way, as long as the network is not faulty and the remote computer is already listening, it can receive the message sent here. Of course, this is a listener, and remote computers can send messages to it freely. The catch part of the function is also used to catch exceptions.

At the same time, it should be noted that because the program uses many objects required for network programming and input and output objects, it also uses the multi-thread programming mechanism, therefore, the following namespaces must be added at the beginning of the program:

Using system. IO;
Using system. net. Sockets;
Using system. Threading;

Finally, the event processing functions of various controls in the program and the completeCodeSeeSource codePackage. The program runs as follows:

Now we have a basic P2P application and complete it. Through it, we can use the basic features 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. The disadvantage of this program is that the function is relatively simple. It can only send and accept information and cannot communicate through the firewall. Readers can try to develop P2P applications with better functionality.

Summary

Finally, I hope to use this article to arouse everyone's interest in P2P technology. The incomparable creativity behind P2P makes people have a good vision for the future of the Internet. The P2P application boom in the world is also a wave of waves. In the foreseeable future, with the further development of P2P research and the increasing number of P2P groups, P2P will inevitably enter a new era of rapid development. However, P2P in China started late, so a large amount of technical research investment and enough attention are required to win better development. Here, I hope that P2P in China can achieve brilliant development.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.