Peer-to-Peer Programming

Source: Internet
Author: User
Document directory
  • The peer Module
  • The peerconnection Module
Introductionwhat is peer-to-peer?

This page walks through a basic introduction to developingPeer-to-peer (P2P)Applications. By the end of it, you shoshould understand the concepts and programming constructs necessary to implement a P2P protocol and/or application program.

The first thing we need is to understand exactly what is meant by "P2P ". this page, what is P2P... and what isn' t, written by Clay Shirky in 2000, gives a nice overview of the fundamental essence of P2P, along with a little history. briefly, P2P applications organize and manage resources at the edges of the Internet in a decentralized manner, with little or no interaction with centralized servers. the resources may be storage or content (e.g. file sharing applications), processor cycles (e.g. the SETI framework or other programs which use your computer to perform part of some large, distributed task when it is idle), or human presence (e.g. instant Messaging ). the 'edges 'of the Internet are machines such as the average home PC which has just a single line (Dial-Up, DSL, cable, etc .) to connect it to the vast Internet 'cloud.'

In a traditional Client/Server Setup, your home PC acts as a client and sends requests to machines -- servers -- somewhere inside the cloud of the Internet to get things done: browsing the Web or getting email. in a P2P paradigm, on the other hand, your home PC (for example) may connect, through the Internet, directly to tens or hundreds of other home PCs (or other machines at the 'edge ') in order to share information and data. because such resources at the edge of the internet tend to be ephemeral (they may connect and disconnect times repeatedly in a day ), p2P protocols have to operate in an "environment of unstable connectivity and unpredictable IP addresses" [Shirky].

P2P vs. Client/Server

So, unlike a server/client architecture where you develop applications in two asyuncrical pieces -- the server, which provides services and is assumed to be reliably available at a known Internet address, and the client which connects to the server in order to request information -- P2P applications seem a bit more tricky to develop. in a P2P system, all machines (nodes) are running the same program (this is somewhat of a generalization: Some systems are organized so groups of P2P nodes are running similar but different programs ). some of the issues that need to be handled in such a situation include:

  • Connectivity: how to find and connect other P2P nodes that are running in the network (unlike traditional servers, they don't have a fixed, known IP address)
  • Instability: nodes may always be joining and leaving the network (unlike servers -- web, email, Etc., which we usually depend on to "be there ")
  • Message routing: How messages shocould be routed to get from one node to another (where the two nodes may not directly know about each other)
  • Searching (somewhat related to routing): How to Find desired information from the nodes connected to the network
  • Security: a whole slew of issues including nodes being able to trust other nodes, preventing malicous nodes from doing bad things to the P2P network or the individual nodes, being able to send and receive data anonymously, etc.


The tutorial in this document will primarily focus on the first four issues -- the basics of getting a P2P system up and running. by the time you reach the end of the tutorial, you will be familiar with a library that will help you implement P2P protocols and applications. the Library provides infrastructure-related routines to help you manage issues of socket handling, threading, and sending messages between peers. to help you understand what is going on, the main part of this tutorial is a walk through the development of the library itself.

^ Top

A P2P frameworkoverview

Before diving into the details of actual code, you shoshould understand what we are trying to implement at a high-level. the figure below within strates how conversations happen between peers in a network. each application running on a node provides an interface to the user (You And I), and is simultaneously running a "Main Loop" that listens for incoming connections from other peers.


In the figure, a scenerio is diagrammed where the user on peer 1 clicks a button, for example, a "Search" button, In the GUI interface. the interface somehow decides to send a "query" message to another peer, in this case, peer 2. the main loop of peer 2 detects the incoming connection request (step 2) and starts up a separate thread to handle the actual data of the request (Step 3 ). (ThreadIs a task that a program runs simultaneously, or pseudo-simultaneously, with other running tasks: see Thread-Wikipedia if you are not familiar with the term. the purpose of using threads here is to allow a peer to handle multiple incoming connections simultaneous .)

Assuming, Message Type "C" refers to a "query" message, peer 1 sends the actual message (step 4) Once it has gotten a connection to peer 2. in step 5, the "handle peer" task (thread) of peer 2 threads es the message, sends an acknowledgment back to peer 1, closes the connection, and then Callan appropriate function/method to handle the message based on its type.

After processing the message, the "msg c handler" function decides that it needs to send a "query response" message back to peer 1, so it attempts to connect (Step 6 ). peer 1's main loop, listening for such connections, accepts the connection and starts its separate handler thread (Step 7) to receive the actual message data from peer 2 (Step 8 ). having received the message, peer 1 does what peer 2 did in step 5, and the process continues...

Components of a peer

The implementation of the P2P protocol that each peer is running in the distriabove is built on a simple framework that you will be familiar with in detail by the end of this document. here, I will give a high-level overview of the various modules (I. e. classes, in object-oriented terminology) that make up the framework. note, that the framework does not include anything to do with the user interface -- that part of the program wocould be implemented seperately and wocould interact in appropriate ways with the underlying framework described here.

The peer Module

The peer module manages the overall operations of a single node in the P2P network. it contains a main loop that listens for incoming connections and creates separate threads to handle them. the programmer, building a P2P protocol on top of this generic framework, wocould registers handlers (I. e. methods or functions) with the peer module for various message types, and the main loop wocould dispatch incoming requests to the appropriate handler. the peer is initialized by providing a port to listen for incoming connections, and Optionally a host address (I. e. IP address, which may be automatically determined) and node identifier.

A list of known peers, which may be accessed and modified by the programmer, is also maintained by the peer module. the size of the list may be limited, and peers may be accessed using their identifiers or their sequential position in the list. besides a list of handlers for various method types, the node also stores a programmer-supplied function for deciding how to route messages, and can be set up to run stabilization operations at specific intervals.

The peerconnection Module

The peerconnection module encapsulates a socket connected to a peer node. the framework currently uses TCP/IP sockets for communication between nodes. A peerconnection object provides Methods The make it easy for the programmer to send and receive messages and acknowledgments In the P2P algorithm. it ensures messages are encoded in the correct format and attempts to detect various error conditions.

Messages exchanged between nodes built on this framework are prefixed by a Header composed of a 4-byte identifier for the type of the message and a 4-byte integer holding the size of the data in message. the 4-byte message code can be viewed as a string, so the programmer may come up with appropriate strings of length 4 to identify the varous types of messages exchanged in the system. when the main loop of a peer receives es a message, it dispatches it to the appropriate handler Based on the message type.

A message handler is simply a function object in Python (or an object supporting the handler interface in Java) that has es a reference to an open peerconnection and the message data. handlers can be registered for any message type identified by a 4-byte string. currently only one handler per type may be used. when the peer module has es an incoming connection request, it sets up a peerconnection object, reads in the message type and remainder of the message, and launches a separate thread to handle the data. the peer connection is automatically closed when the message handler completes its task.

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: 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.