Http://blog.oraycn.com/ESFramework_Demo_P2P.aspx
Test, fully ok! I like this very much. Can source code is encrypted! I want to achieve a web version of peer-to video viewing, Aehyok proposed with SignalR. There are a lot of things to learn.
Esframework Demo--Peer communication demo (attached source)
we will now use the fourth weapon provided by Esplus to increase the ability to communicate with the Esframework demo, which is based on the file transfer demo. Before reading this article, it is important to master the basics of peer-to esframework development Manual (04)--The basic knowledge of peers and the usage of related APIs.
This demo mainly demonstrates the following features:
(1) Create a TCP-based peer channel
(2) Create a UDP-based peer channel (internal use of reliable UDP)
(3) using peer channel to send messages and transfer files
I. Service-side
In the process of peer to hole, the service side will be involved in assisting the establishment of peer-to channel, the whole process is automatically completed by the Esframework/esplus internal, and this process for the framework users is transparent. When the peer channel is created, communication between the client and the client is not related to the server. So we just put the last demo server to use, do not need to make any changes.
Two. Client
the client mainly uses the P2pcontroller provided by Irapidpassiveengine to query and control the state of the peer channel.
Try to create a peer channel
as described in the Esframework Development Manual (04)-a reliable peer-to-peer article, not all peer-to-peer channels between clients can be created successfully, creating a peer channel is an attempted process, The P2pconnectasyn method of Ip2pcontroller is to try to create a peer-to channel with the target user.
Timing is important. when did you create a peer channel? In general, it is a call to P2pconnectasyn to try to create before two clients need high-frequency communication.
In this demo, we try to create a peer channel each time we open the Chat window with the target user. As shown below:
void Listview1_mousedoubleclick (object sender, MouseEventArgs e)
{
if (E.button! = System.Windows.Forms. mousebuttons. left)
{
return;
}
Listviewhittestinfo this. listview1.hittest (e.location);
if null)
{
// try to establish a peer-to channel with the target user
This.rapidPassiveEngine.P2PController.P2PConnectAsyn (info. Item.text);
Chatform this. Chatformmanager.getform (info. Item.text);
......
}
when using the P2pconnectasyn method to start a hole, if the two sides on the same LAN, will generally establish a TCP-based peer channel, if the two sides in a different network, will generally establish a UDP-based peer-to channel. If there is already an available peer channel with the target user, P2pconnectasyn will no longer do any action and return directly.
In order to obtain the notification that the peer channel was created successfully or failed and the subsequent peer channel is turned off, In the demo we have P2pcontroller p2pchannelopened and p2pchannelclosed events scheduled in the Initialize method of MainForm.
// scheduled peer channel creation of successful events
This New cbgeneric<p2pchannelstate> (p2pcontroller_p2pchannelopened);
// events scheduled when the peer channel is closed
This New cbgeneric<p2pchannelstate> (p2pcontroller_p2pchannelclosed);
When the peer channel creation is successful or closed, this demo displays this status by modifying the title text of the corresponding chat window. For example, when the peer channel is created successfully, the title of the chat window is displayed as follows:
Typically, if two clients reside on the same LAN, or if there is a client directly on the Internet, the peer channel between them is TCP-based, otherwise the peer channel created is UDP-based.
Chatform provides a showp2pchannelstate method to show the peer-to channel status between chat objects.
//<summary>
/// Show the status of the peer-to connection
/// </summary>
Public void Showp2pchannelstate (p2pchannelstate State)
{
This NULL string. Format (" in conversation with {0}" {1} directly connected: {2} "... " This string. Format (" in conversation with {0} ... " this. friendID);
Note that the Getp2pchannelstate method of P2pcontroller returns NULL when there is no peer channel to the target user.
Observing peer-to-peer communication
when the peer channel is created successfully, subsequent communication between the two users will pass through the peer-to channel, and in this demo, the subsequent chat messages and file transfers will be done through the peer channel.
So, how do you know if the message was routed through a server, or was it delivered directly through a peer-to channel? There are two simple methods that we often use.
(1) Observe the Mainserverform interface of the server.
If the message is relayed through the server, then the interface shows the message recipient user corresponding "Download times", "Upload times", "Last download Time" and so on will be changed. In this case, whenever you send a chat message to each other, if it is a server relay, then the interface displayed on the other side of the download will increase by 1, their number of uploads also increased by 1, their last upload time will change. The data is not affected if the message is routed through the peer-to channel.
(2) View Resource Monitor
In the case of a Win7 system, the Task Manager performance display provides resource monitor to monitor network activity. With it, we can see which machines the application is communicating with. As shown in the following:
The above is one of our other peer apps demo, The 59.175.145.163 in the figure is the IP of the server, and we see that the primary traffic that OMCS.ClientDemo.exe receives is from the ZY-PC computer, which means that data between two clients is routed through the peer channel and not relayed through the server. There is also a small amount of traffic between the client and server shown in the figure, which is generated by similar timed heartbeat messages.
If it is not a Win7 system, you can also view this information by installing network monitoring software such as NetLimiter.
Three. Source code download
ESFramework.Demos.P2P Source
--------------------------------------------------------------------------------------------------------------- --------------------------------
Download the free version of Esframework and demo source
Read more articles on the Esframework Development Handbook series.
Q q:168757008
Official website: www.oraycn.com
C # Peer Penetration