peer - to - peer networking

Download Peer - to - Peer Networking

Post on 14-Dec-2014




7 download

Embed Size (px)




  • 1. 45Peer-to-Peer NetworkingWHAT S IN THIS CHAPTER?An overview of P2P The Microsoft Windows Peer-to-Peer Networking platform, including PNRPand PNM Building P2P applications with the .NET Framework Peer-to -peer networking, often referred to as P2P, is perhaps one of the most useful and yetmisunderstood technologies to emerge in recent years. When people think of P2P they usually thinkof one thing: sharing music les, often illegally. This is because le - sharing applications such asBitTorrent have risen in popularity at a staggering rate, and these applications use P2P technologyto work.Although P2P is used in le-sharing applications, that doesnt mean it doesnt have other applications.Indeed, as you see in this chapter, P2P can be used for a vast array of applications, and is becomingmore and more important in the interconnected world in which we live. You learn about this in the rst part of this chapter, when you look at an overview of P2P technologies.Microsoft has not been oblivious to the emergence of P2P, and has been developing its own toolsand technologies to use it. You can use the Microsoft Windows Peer- to - Peer Networking platformas a communication framework for P2P applications. This platform includes the important compo-nents Peer Name Resolution Protocol (PNRP) and People Near Me (PNM). Also, version 3.5 of the.NET Framework introduced a new namespace, System.Net.PeerToPeer, and several new typesand features that you can use to build P2P applications yourself with minimal effort. PEER-TO -PEER NETWORKING OVERVIEWPeer-to -peer networking is an alternative approach to network communication. To understand howP2P differs from the standard approach to network communication it is helpful to take a stepbackward and look at client-server communications. Client-server communications are ubiquitous innetworked applications today. c45.indd 111/11/09 5:35:13 PM

2. 2 CHAPTER 45 PEER-TO -PEER NETWORKINGClient-Server Architecture Traditionally, you interact with applications over a network (including the Internet) using a client-server architecture. Web sites are a great example of this. When you look at a web site you send a request over the Internet to a web server, which then returns the infor- mation that you require. If you want to download a le, you do so directly fromServer the web server. Similarly, desktop applications that include local or wide area network connectiv- ity will typically connect to a single server, for example, a database server or a Request Response server that hosts other services. This simple form of client-server architecture is illustrated in Figure 45-1. There is nothing inherently wrong with the client-server architecture, and indeed Client in many cases it will be exactly what you want. However, there is a scalability problem. Figure 45-2 shows how the client-server architecture scales with addi-FIGURE 45-1 tional clients. ServerClients FIGURE 45-2With every client that is added an increased load is placed on the server, which must communicate with each client. To return to the web site example, this increased communication load is how web sites collapse. When there is too much trafc the server simply becomes unresponsive. There are of course scaling options that you can implement to mitigate this situation. You can scale up by increasing the power and resources available to the server, or you can scale out by adding additional servers. Scaling up is of course limited by the technology available and the cost of better hardware. Scaling out is potentially more exible, but requires an additional infrastructure layer to ensure that clients either communicate with individual servers or maintain session state independent of the server with which they are communicating. Plenty of solutions are available for this, such as web or server farm products. P2P Architecture The peer-to -peer approach is completely different from either the scaling up or scaling out approach. With P2P, instead of focusing on and attempting to streamline the communication between the server and its cli- ents, you instead look at ways in which clients can communicate with each other. Say, for example, that the web site that clients are communicating with is In our imaginary scenario, Wrox has announced that a new version of this book is to be released on the web site and will be free to download to anyone who wants it; however, it will be removed after one day. Before the book becomes available on the web site you might imagine that an awful lot of people will be looking at the web site and refreshing their browsers, waiting for the le to appear. When the le is available, everyone will try to download it at the same time, and more than likely the web server will collapse under the strain. c45.indd 2 11/11/09 5:35:15 PM 3. Peer-to - Peer Networking Overview 3You could use P2P technology to prevent this web server collapse. Instead of sending the le directly fromthe server to all the clients, you send the le to just a few clients. A few of the remaining clients then down-load the le from the clients that already have it, a few more clients download it from those second-levelclients, and so on. In fact, this process is made even faster by splitting the le into chunks and dividingthese chunks among clients, some of whom download it directly from the server, and some whom downloadchunks from other clients. This is how le-sharing technologies such as BitTorrent work, and is illustrated inFigure 45-3. Server ClientsFIGURE 45-3P2P Architectural ChallengesThere are still problems to solve in the le - sharing architecture discussed here. For a start, how doclients detect that other clients exist, and how do they locate chunks of the le that other clients mighthave? Also, how can you ensure optimal communication between clients that may be separated by entirecontinents?Every client participating in a P2P network application must be able to perform the following operations toovercome these problems: It must be able to discover other clients. It must be able to connect to other clients. It must be able to communicate with other clients.The discovery problem has two obvious solutions. You can either keep a list of the clients on the server soclients can obtain this list and contact other clients (known as peers), or you can use an infrastructure (forexample PNRP, covered in the next section) that enables clients to nd each other directly. Most le-sharing c45.indd 3 11/11/09 5:35:16 PM 4. 4 CHAPTER 45 PEER-TO -PEER NETWORKINGsystems use the list on a server solution by using servers known as trackers. Also, in le-sharing systems any client may act as a server as shown in Figure 45-3, by declaring that it has a le available and registering it with a tracker. In fact, a pure P2P network needs no servers at all, just peers. The connection problem is a more subtle one, and concerns the overall structure of the networks used by a P2P application. If you have one group of clients, all of which can communicate with one another, the topol- ogy of the connections between these clients can become extremely complex. You can often improve per- formance by having more than one group of clients, each of which consists of connections between clients in that group, but not to clients in other groups. If you can make these groups locale-based you will get an additional performance boost, because clients can communicate with each other with fewer hops between networked computers. Communication is perhaps a problem of lesser importance, because communication protocols such as TCP/IP are well established and can be reused here. There is, however, scope for improvement in both high-level technologies (for example, you can use WCF services and therefore all the functionality that WCF offers) and low-level protocols (such as multicast protocols to send data to multiple endpoints simultaneously). Discovery, connection, and communication are central to any P2P implementation. The implementa- tion you look at in this chapter is to use the System.Net.PeerToPeer types with PNM for discovery and PNRP for connection. As you see in subsequent sections, these technologies cover all three of these operations. P2P Terminology In the previous sections you were introduced to the concept of a peer, which is how clients are referred to in a P2P network. The word client makes no sense in a P2P network because there is not necessarily a server to be a client of. Groups of peers that are connected to each other are known by the interchangeable terms meshes, clouds, or graphs. A given group can be said to be well - connected if at least one of the following statements applies: There is a connection path between every pair of peers, so that every peer can connect to any otherpeer as required. There are a relatively small number of connections to traverse between any pair of peers. Removing a peer will not prevent other peers from connecting to each other. Note that this does not mean that every peer must be able to connect to every other peer directly. In fact, if you analyze a network mathematically you will nd that peers need to connect only to a relatively small number of other peers for these conditions to be met. Another P2P concept to be aware of is ooding. Flooding is the way in which a single piece of data may be propagated through a network to all peers, or of querying other nodes in a network to locate a specic piece of data. In unstructured P2P networks this is a fairly random process of contacting nearest neighbor peers, which in turn contact their nearest neighbors, and so on until every peer in the network is contacted. It is also possible to create structured P2P networks such that there are well- de ned pathways for queries and data ow among peers. P2P Solutions When you have an infrastructure for P2P you can start to develop not just improved versions of client-se