日期:2008-08-23  浏览次数:20461 次

NET P2P: Writing Peer-to-Peer Networked Apps with the Microsoft .NET Framework
Lance Olson

This article assumes you’re familiar with HTTP and .NET
Level of Difficulty 1 2 3
Download the code for this article: NetPeers.exe(66KB)
Browse the code for this article at Code Center: ShareBaby
SUMMARY Peer-to-peer applications such as Napster, Gnutella, and Scour that communicate as peers sharing and receiving information are becoming commonplace as a means for users connected on large networks to take advantage of the vast resources available to them. The Microsoft .NET Framework provides a rich platform for building P2P apps.
This article explains the concepts that make up peer-to-peer applications. The peer-to-peer application model, discovering other peers, and querying peers for information are discussed. The article goes on to cover the System.Net namespace for the use of Internet protocols, the System.Web.Services namespace for exposing Web Services, and firewall and port issues. Finally, the role of the .NET Framework in simplifying the design of powerful peer-to-peer applications is outlined.
--------------------------------------------------------------------------------

he Microsoft® .NET Framework SDK is a common framework of classes designed to provide a rich platform for building applications. In this article, I'll explain how this framework can be used to create peer-to-peer (P2P) applications and discuss the design decisions that are important to consider when writing a peer-to-peer application. I'll look at the .NET Framework support for the particular features listed in Figure 1, and discuss how they can help you.

What is a Peer-to-peer Application?
Communication is a key element when writing nearly any type of application. An application gains value when it becomes distributed and interacts with other resources available to it on the Internet or intranet. The most common model for communication over the Internet today is client/server, where there is a client that knows how to request information and post information to a server, and the server knows how to respond to requests from the client. A browser talking to a Web server is a common example of this model. Many browsers can send requests to the Web server, and the server does its job by listening for those requests and responding back to each of the browsers requesting or sending information (usually in the form of Web pages). In this model, the Web server cannot arbitrarily contact the browser. The "conversation" is always initiated by the client.
A peer-to-peer application is different from the traditional client/server model because the applications involved act as both clients and servers. That is to say, while they are able to request information from other servers, they also have the ability to act as a server and respond to requests for information from other clients at the same time. This approach increases the amount of value that each node on the network can add because it not only takes information from a source, but it also has the ability to share that information with other sources. A typical peer-to-peer application has the following key features that help define it:
Discovering other peers The application must be able to find other applications that are willing to share information. Historically, the application finds these peers by registering with a central server that maintains a list of all applications currently willing to share and giving that list to any new applications as they connect to the network. However, there are other means available, such as network broadcasting or discovery algorithms.
Querying peers for content Once these peers have been discovered, the application can ask them for the content that is desired by the application. Content requests often come from users, but it is highly conceivable that the peer-t