Esframework demo-P2P communication demo (with source code)

Source: Internet
Author: User
ArticleDirectory
    • Try to create a P2P Channel
    • Observe P2P communication

Now, based on esframework demo-File Transfer demo, we will use the fourth weapon provided by esplus to add P2P communication functions for it. Before reading this article, you must first master the basic knowledge of P2P and related API usage described in esframework (04)-reliable P2P.

This demo mainly demonstrates the following functions:

(1) create a TCP-based P2P Channel

(2) create a UDP-Based P2P channel (using reliable UDP internally)

(3) send messages and transfer files through P2P Channels

I. Server

 During P2P penetration, the server will participate in the establishment of P2P channels. The entire process is automatically completed by esframework/esplus, which is transparent to framework users. After a P2P channel is created, the communication between the client and the client has nothing to do with the server. Therefore, we can directly use the server of the previous demo without any modifications.

Ii. Client

The client mainly uses p2pcontroller provided by irapidpassiveengine to query and control the status of P2P channels.

Try to create a P2P Channel

As described in esframework development manual (04)-reliable P2P, not all P2P channels between clients can be created successfully. Creating a P2P channel is an attempt, the p2pconnectasyn method of ip2pcontroller is to try to create a P2P channel with the target user.

The timing is important.When did I create a P2P channel? Generally, p2pconnectasyn is called to create an instance before the two clients need high-frequency communication.

In this demo, we try to create a P2P channel every time we open a chat window with the target user. As follows:

Void Listview1_mousedoubleclick ( Object Sender, mouseeventargs E)
{
If (E. Button! = System. Windows. forms. Mousebuttons . Left)
{
Return ;
}

Listviewhittestinfo Info = This . Listview1.hittest (E. Location );
If (Info. item! = Null )
{
// Try to establish a P2P channel with the target user
This. rapidpassiveengine. p2pcontroller. p2pconnectasyn (info. item. Text );

Chatform Form = This . Chatformmanager. getform (info. item. Text );
 
......
 
}
}

 When p2pconnectasyn is used to start drilling, if both parties are located in the same LAN, a TCP-based P2P channel is generally established. If both parties are located in different networks, generally, a UDP-Based P2P channel is established.If there is an available P2P channel between the target user and the target user, p2pconnectasyn will not perform any action and return directly.

to receive notifications that the P2P channel is successfully created or fails, in the demo, we booked p2pchannelopened and p2pchannelclosed events of p2pcontroller in the initialize method of mainform.

  //   scheduled events for successful P2P channel creation   
This . rapidpassiveengine. p2pcontroller. p2pchannelopened + = New cbgeneric p2pchannelstate (p2pcontroller_p2pchannelopened);
// events scheduled for P2P channel shutdown
This . rapidpassiveengine. p2pcontroller. p2pchannelclosed + = New cbgeneric p2pchannelstate (p2pcontroller_p2pchannelclosed);

When the P2P channel is successfully created or closed, this demo displays this status by modifying the title text of the corresponding chat window. For example, when a P2P channel is successfully created, the title in the chat window is displayed as follows:

Generally, if two clients are on the same LAN or one is directly on the Internet, the P2P channel between them is based on TCP; otherwise, the created P2P channel is based on UDP.

Chatform provides the showp2pchannelstate method to display the P2P channel status between chat objects.

 ///  <Summary>  
/// Show P2P connection status
/// </Summary>
Public Void Showp2pchannelstate ( P2pchannelstate State)
{
This . Text = (State! = Null )? String . Format ( " In the conversation with {0}, [{1} direct connection: {2 }]... " , This . Friendid, state. protocoltype, state. destipe ): String . Format ( " Talking to {0... " , This . Friendid );
}

Note that when there is no P2P channel between p2pcontroller and the target user, the getp2pchannelstate method of p2pcontroller returns NULL.

Observe P2P communication

After the P2P channel is successfully created, subsequent communication between the two users will be transmitted through the P2P channel. In this demo, it indicates that subsequent chat messages and file transfers will be carried out through the P2P channel.

So, how can we determine whether a message is transmitted through a server or through a P2P channel? We often use two simple methods.

(1) observe the mainserverform interface of the server. 

If a message passes through the server, the "download times", "Upload Times", and "last download time" of the message recipient are displayed on the interface. In this example, each time you send a chat message to the other party, if it passes through the server, the number of downloads displayed on the Interface increases by 1, the number of uploads increases by 1, and the last upload time changes. If messages are transmitted through P2P channels, the data will not be affected.

(2) view resource monitors

In Windows 7, the "Resource Monitor" is provided in the "performance" display of the task manager to monitor network activities. Using it, we can see the applicationProgramWhich machines are communicating. As shown in:

The main traffic that butler receives data comes from the ZY-PC computer, which means that the data between the two clients is transmitted through the P2P channel, not through the server. The reason why there is a small amount of traffic between the client and the server shown in the figure is that it is generated by a scheduled Heartbeat message.

If not, you can install network monitoring software (such as netlimiter) to view the information.

3. Download source code

Esframework. demos. P2P source code

  

Read more esframework development manual articles.

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

For any questions about esframework, please contact us:

Tel: 027-87638960

Q: 372841921

Email:Esframework@oraycn.com

 

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.