CN Module1 PDF
CN Module1 PDF
Module – 1
APPLICATION LAYER
P2P Architecture
Future P2P applications face three major challenges:
1. ISP Friendly. Most residential ISPs have been dimensioned for “asymmetrical” bandwidth
usage, that is, for much more downstream than upstream traffic. But P2P video streaming
and file distribution applications shift upstream traffic from servers to residential ISPs,
thereby putting significant stress on the ISPs. Future P2P applications need to be designed so
that they are friendly to ISPs
2. Security. Because of their highly distributed and open nature, P2P applications can be a
challenge to secure
3. Incentives. The success of future P2P applications also depends on convincing users to
volunteer bandwidth, storage, and computation resources to the applications, which is the
challenge of incentive design.
Addressing Processes
For a process running on one host to send packets to a process running on another host, the
receiving process needs to have an address.
To identify the receiving process, two pieces of information need to be specified:
(1) The address of the host
(2) An identifier that specifies the receiving process in the destination host.
In the Internet, the host is identified by its IP address.
In addition to knowing the address of the host to which a message is destined, the sending
process must also identify the receiving process running in the host. A destination port
number serves this purpose. Popular applications have been assigned specific port numbers.
For example, a Web server is identified by port number 80. A mail server process (using the
SMTP protocol) is identified by port number 25.
2) Throughput
Transport-layer protocol could provide guaranteed available throughput at some specified
rate.
With such a service, the application could request a guaranteed throughput of r bits/sec, and
the transport protocol would then ensure that the available throughput is always at least r
bits/sec. Such a guaranteed throughput service would appeal to many applications.
For example, if an Internet telephony application encodes voice at 32 kbps, it needs to send
data into the network and have data delivered to the receiving application at this rate.
If the transport protocol cannot provide this throughput, the application would need to encode
at a lower rate or may have to give up.
Applications that have throughput requirements are said to be bandwidth-sensitive
applications. Many current multimedia applications are bandwidth sensitive
Elastic applications can make use of as much, or as little, throughput as happens to be
available. Electronic mail, file transfer, and Web transfers are all elastic applications.
3) Timing
A transport-layer protocol can also provide timing guarantees.
Interactive real-time applications, such as Internet telephony, virtual environments,
teleconferencing, and multiplayer games require tight timing constraints on data delivery in
order to be effective.
4) Security
Transport protocol can provide an application with one or more security services.
For example, in the sending host, a transport protocol can encrypt all data transmitted by the
sending process, and in the receiving host, the transport-layer protocol can decrypt the data
before delivering the data to the receiving process.
A transport protocol can provide security services like confidentiality, data integrity and end-
point authentication.
TCP Services
The TCP service model includes a connection-oriented service and a reliable data transfer
service.
1) Connection-oriented service:
In TCP the client and server exchange transport layer control information with each other
before the application-level messages begin to flow.
This handshaking procedure alerts the client and server, allowing them to prepare for an
onslaught of packets.
After the handshaking phase, a TCP connection is said to exist between the sockets of the
two processes.
The connection is a full-duplex connection in that the two processes can send messages to
each other over the connection at the same time.
When the application finishes sending messages, it must tear down the connection.
When one side of the application passes a stream of bytes into a socket, it can count on TCP
to deliver the same stream of bytes to the receiving socket, with no missing or duplicate
bytes.
TCP also includes a congestion-control mechanism.
UDP Services
UDP is connectionless, so there is no handshaking before the two processes start to
communicate.
UDP provides an unreliable data transfer service—that is, when a process sends a message
into a UDP socket, UDP provides no guarantee that the message will ever reach the receiving
process.
UDP does not include a congestion-control mechanism, so the sending side of UDP can
pump data into the layer below (the network layer) at any rate it pleases.
Popular Internet applications, their application-layer protocols, and their underlying transport
protocols
The semantics of the fields, that is, the meaning of the information in the fields
Rules for determining when and how a process sends messages and responds to messages.
It is important to note that the server sends requested files to clients without storing any state
information about the client. If a particular client asks for the same object twice in a period of
a few seconds, the server does not respond by saying that it just served the object to the
client; instead, the server resends the object, as it has completely forgotten what it did earlier.
Because an HTTP server maintains no information about the clients, HTTP is said to be a
stateless protocol.
2. The HTTP client sends an HTTP request message to the server via its socket. The request
message includes the path name /someDepartment/home.index.
3. The HTTP server process receives the request message via its socket, retrieves the object
/someDepartment/home.index from its storage (RAM or disk), encapsulates the object in an
HTTP response message, and sends the response message to the client via its socket.
4. The HTTP server process tells TCP to close the TCP connection.
5. The HTTP client receives the response message. The TCP connection terminates. The
message indicates that the encapsulated object is an HTML file. The client extracts the file
from the response message, examines the HTML file, and finds references to the 10 JPEG
objects.
6. The first four steps are then repeated for each of the referenced JPEG objects.
Round-trip time (RTT) is the time it takes for a small packet to travel from client to server
and then back to the client.
The RTT includes packet-propagation delays, packet queuing delays in intermediate routers
and switches, and packet-processing delays.
When a user clicks on a hyperlink, the browser initiate a TCP connection between the
browser and the Web server; this involves a “three-way handshake”—the client sends a small
TCP segment to the server, the server acknowledges and responds with a small TCP segment,
and, finally, the client acknowledges back to the server.
The first two parts of the three way handshake take one RTT.
After completing the first two parts of the handshake, the client sends the HTTP request
message combined with the third part of the three-way handshake (the acknowledgment) into
the TCP connection.
Once the request message arrives at the server, the server sends the HTML file into the TCP
connection. This HTTP request/response eats up another RTT. Thus, roughly, the total
response time is two RTTs plus the transmission time at the server of the HTML file.
The header line Host:www.someschool.edu specifies the host on which the object resides.
By including the Connection:close header line, the browser is telling the server that it doesn’t
want to bother with persistent connections; it wants the server to close the connection after
sending the requested object.
The User-agent: header line specifies the user agent, that is, the browser type that is making the
request to the server. Here the user agent is Mozilla/5.0, a Firefox browser.
The Accept-language: header indicates that the user prefers to receive a French version of the
object, if such an object exists on the server; otherwise, the server should send its default version.
Ex:
HTTP/1.1 200 OK
Connection: close
Date: Tue, 09 Aug 2011 15:44:04 GMT
Server: Apache/2.2.3 (CentOS)
Last-Modified: Tue, 09 Aug 2011 15:11:03 GMT
Content-Length: 6821
Content-Type: text/html
The status line has three fields: the protocol version field, a status code, and a corresponding
status message.
Version is HTTP/1.1
The status code and associated phrase indicate the result of the request. Some common status
codes and associated phrases include:
• 200 OK: Request succeeded and the information is returned in the response.
• 301 Moved Permanently: Requested object has been permanently moved; the new URL is
specified in Location: header of the response message. The client software will automatically
retrieve the new URL.
• 400 Bad Request: This is a generic error code indicating that the request could not be
understood by the server.
• 404 Not Found: The requested document does not exist on this server.
• 505 HTTP Version Not Supported: The requested HTTP protocol version is not supported by
the server.
Header fields:
The server uses the Connection: close header line to tell the client that it is going to close
the TCP connection after sending the message.
The Date: header line indicates the time and date when the HTTP response was created
and sent by the server.
The Server: header line indicates that the message was generated by an Apache Web
server; it is analogous to the User-agent: header line in the HTTP request message.
The Last-Modified: header line indicates the time and date when the object was created
or last modified.
The Content-Length: header line indicates the number of bytes in the object being sent.
The Content-Type: header line indicates that the object in the entity body is HTML text.
Ex:
Suppose a user, who always accesses the Web using Internet Explorer from her home PC,
contacts Amazon.com for the first time. Let us suppose that in the past he has already visited the
eBay site. When the request comes into the Amazon Web server, the server creates a unique
identification number and creates an entry in its back-end database that is indexed by the
identification number. The Amazon Web server then responds to Susan’s browser, including in
the HTTP response a Set-cookie: header, which contains the identification number.
For example, the header line might be:
Set-cookie: 1678
When users browser receives the HTTP response message, it sees the Set-cookie: header. The
browser then appends a line to the special cookie file that it manages. This line includes the
hostname of the server and the identification number in the Set-cookie: header.
As user continues to browse the Amazon site, each time he requests a Web page, his browser
consults his cookie file, extracts his identification number for this site, and puts a cookie header
line that includes the identification number in the HTTP request. Specifically, each of his HTTP
requests to the Amazon server includes the header line:
Cookie: 1678
When web cache receives requests from and sends responses to a browser, it is a server.
When it sends requests to and receives responses from an origin server, it is a client.
Typically a Web cache is purchased and installed by an ISP. For example, a university might
install a cache on its campus network and configure all of the campus browsers to point to
the cache. Or a major residential ISP (such as AOL) might install one or more caches in its
network and pre configure its shipped browsers to point to the installed caches.
Web caching has seen deployment in the Internet for two reasons. First, a Web cache can
substantially reduce the response time for a client request. Second, Web caches can
substantially reduce traffic on an institution’s access link to the Internet.
The cache forwards the object to the requesting browser but also caches the object locally.
Importantly, the cache also stores the last-modified date along with the object.
Third, one week later, another browser requests the same object via the cache, and the object is
still in the cache. Since this object may have been modified at the Web server in the past week,
the cache performs an up-to-date check by issuing a conditional GET. Specifically, the cache
sends:
GET /fruit/kiwi.gif HTTP/1.1
Host: www.exotiquecuisine.com
If-modified-since: Wed, 7 Sep 2011 09:23:24
This conditional GET is telling the server to send the object only if the object has been modified
since the specified date.
Suppose the object has not been modified since 7 Sep 2011 09:23:24. Then, fourth, the Web
server sends a response message to the cache:
HTTP/1.1 304 Not Modified
Date: Sat, 15 Oct 2011 15:39:29
Server: Apache/1.3.0 (Unix)
(empty entity body)
We see that in response to the conditional GET, the Web server still sends a response message
but does not include the requested object in the response message.
Once the server has authorized the user, the user copies one or more files stored in the local
file system into the remote file system (or vice versa).
FTP uses two parallel TCP connections to transfer a file, a control connection and a data
connection.
The control connection is used for sending control information between the two hosts—
information such as user identification, password, commands to change remote directory, and
commands to “put” and “get” files.
The data connection is used to actually send a file.
When a user starts an FTP session with a remote host, the client side of FTP (user) first
initiates a control TCP connection with the server side (remote host) on server port number
21.
The client side of FTP sends the user identification and password over this control
connection. The client side of FTP also sends, over the control connection, commands to
change the remote directory.
When the server side receives a command for a file transfer over the control connection
(either to, or from, the remote host), the server side initiates a TCP data connection to the
client side.
FTP sends exactly one file over the data connection and then closes the data connection. If,
during the same session, the user wants to transfer another file, FTP opens another data
connection.
Thus, with FTP, the control connection remains open throughout the duration of the user
session, but a new data connection is created for each file transferred within a session (that is,
the data connections are non-persistent).
Throughout a session, the FTP server must maintain state about the user. In particular, the
server must associate the control connection with a specific user account, and the server must
keep track of the user’s current directory as the user wanders about the remote directory tree.
User agents allow users to read, reply to, forward, save, and compose messages.
Mail servers form the core of the e-mail infrastructure. Each recipient has a mailbox located
in one of the mail servers. A typical message starts its journey in the sender’s user agent,
travels to the sender’s mail server, and travels to the recipient’s mail server, where it is
deposited in the recipient’s mailbox.
SMTP is the principal application-layer protocol for Internet electronic mail. It uses the
reliable data transfer service of TCP to transfer mail from the sender’s mail server to the
recipient’s mail server. As with most application-layer protocols, SMTP has two sides: a
client side, which executes on the sender’s mail server, and a server side, which executes on
the recipient’s mail server.
1.4.1 SMTP
SMTP transfers messages from senders’ mail servers to the recipients’ mail servers. It restricts
the body (not just the headers) of all mail messages to simple 7-bit ASCII.
Suppose Alice wants to send Bob a simple ASCII message.
1. Alice invokes her user agent for e-mail, provides Bob’s e-mail address (for example,
[email protected]), composes a message, and instructs the user agent to send the message.
2. Alice’s user agent sends the message to her mail server, where it is placed in a message queue.
3. The client side of SMTP, running on Alice’s mail server, sees the message in the message
queue. It opens a TCP connection to an SMTP server, running on Bob’s mail server.
4. After some initial SMTP handshaking, the SMTP client sends Alice’s message into the TCP
connection.
5. At Bob’s mail server, the server side of SMTP receives the message. Bob’s mail server then
places the message in Bob’s mailbox.
6. Bob invokes his user agent to read the message at his convenience.
An example transcript of messages exchanged between an SMTP client (C) and an SMTP server
(S).
S: 220 hamburger.edu
C: HELO crepes.fr
S: 250 Hello crepes.fr, pleased to meet you
C: MAIL FROM: <[email protected]>
S: 250 [email protected] ... Sender ok
C: RCPT TO: <[email protected]>
S: 250 [email protected] ... Recipient ok
C: DATA
S: 354 Enter mail, end with “.” on a line by itself
C: Do you like ketchup?
C: How about pickles?
C: .
S: 250 Message accepted for delivery
C: QUIT
S: 221 hamburger.edu closing connection
POP3
POP3 is an extremely simple mail access protocol.
POP3 begins when the user agent (the client) opens a TCP connection to the mail server (the
server) on port 110.
With the TCP connection established, POP3 progresses through three phases: authorization,
transaction, and update.
During the authorization phase, the user agent sends a username and a password to
authenticate the user.
During the transaction phase, the user agent retrieves messages; also during this phase, the
user agent can mark messages for deletion, remove deletion marks, and obtain mail statistics.
The update phase occurs after the client has issued the quit command, ending the POP3
session; at this time, the mail server deletes the messages that were marked for deletion.
In a POP3 transaction, the user agent issues commands, and the server responds to each
command with a reply. There are two possible responses: +OK used by the server to indicate
that the previous command was fine; and -ERR, used by the server to indicate that something
was wrong with the previous command.
The authorization phase has two principal commands: user <username> and pass
<password>.
user bob
+OK
pass hungry
+OK user successfully logged on
A user agent using POP3 can often be configured (by the user) to “download and delete” or
to “download and keep.”
In the download-and-delete mode, the user agent will issue the list, retr, and dele commands.
Ex:
C: list
S: 1 498
S: 2 912
S: .
C: retr 1
S: (blah blah ...
S: .................
S: ..........blah)
S: .
C: dele 1
C: retr 2
S: (blah blah ...
S: .................
S: ..........blah)
S: .
C: dele 2
C: quit
S: +OK POP3 server signing off
A problem with this download-and-delete mode is that the recipient cannot access mail
messages from multiple machines.
In the download-and keep mode, the user agent leaves the messages on the mail server after
downloading them. In this case, user can reread messages from different machines.
IMAP
With POP3 access, once user has downloaded his messages to the local machine, he can
create mail folders and move the downloaded messages into the folders. User can then delete
messages, move messages across folders, and search for messages (by sender name or
subject).
But this paradigm—namely, folders and messages in the local machine—poses a problem for
the nomadic user, who would prefer to maintain a folder hierarchy on a remote server that
can be accessed from any computer. This is not possible with POP3—the POP3 protocol
does not provide any means for a user to create remote folders and assign messages to
folders.
To solve this and other problems, the IMAP protocol was invented. Like POP3, IMAP is a
mail access protocol. It has many more features than POP3, but it is also significantly more
complex.
An IMAP server will associate each message with a folder; when a message first arrives at
the server, it is associated with the recipient’s INBOX folder.
The recipient can then move the message into a new, user-created folder, read the message,
delete the message, and so on.
The IMAP protocol provides commands to allow users to create folders and move messages
from one folder to another.
IMAP also provides commands that allow users to search remote folders for messages
matching specific criteria.
Another important feature of IMAP is that it has commands that permit a user agent to obtain
components of messages. For example, a user agent can obtain just the message header of a
message or just one part of a multipart MIME message. This feature is useful when there is a
low-bandwidth connection (for example, a slow-speed modem link) between the user agent
and its mail server. With a low bandwidth connection, the user may not want to download all
of the messages in its mailbox, particularly avoiding long messages that might contain, for
example, an audio or video clip.
Web-Based E-Mail
More and more users today are sending and accessing their e-mail through their Web browsers.
In this case user communicates with its remote mailbox via HTTP.
All the hosts connected to network is identified by IP address. But it is difficult for human
beings to remember these IP address to access a particular host. Hence hosts are identified by
hostnames. Ex: google.com
But the routers require IP address to forward the packet.
In order to map hostname with the IP address DNS is used.
The DNS is (1) a distributed database implemented in a hierarchy of DNS servers, and (2) an
application-layer protocol that allows hosts to query the distributed database.
Example:
Consider what happens when a browser running on some user’s host, requests the URL
www.someschool.edu/index.html.
In order for the user’s host to be able to send an HTTP request message to the Web server
www.someschool.edu, the user’s host must first obtain the IP address of www.someschool.edu.
This is done as follows.
1. The same user machine runs the client side of the DNS application.
2. The browser extracts the hostname, www.someschool.edu, from the URL and passes the
hostname to the client side of the DNS application.
3. The DNS client sends a query containing the hostname to a DNS server.
4. The DNS client eventually receives a reply, which includes the IP address for the hostname.
5. Once the browser receives the IP address from DNS, it can initiate a TCP connection to the
HTTP server process located at port 80 at that IP address.
Host aliasing: A host with a complicated hostname can have one or more alias names. For
example, a hostname such as relay1.west-coast.enterprise.com could have, say, two aliases
such as enterprise.com and www.enterprise.com. In this case, the hostname relay1.westcoast.
enterprise.com is said to be a canonical hostname. Alias hostnames, when present, are
typically more mnemonic than canonical hostnames. DNS can be invoked by an application
to obtain the canonical hostname for a supplied alias hostname as well as the IP address of
the host.
Mail server aliasing: For obvious reasons, it is highly desirable that e-mail addresses be
mnemonic. For example, if Bob has an account with Hotmail, Bob’s e-mail address might be
as simple as [email protected]. However, the hostname of the Hotmail mail server is more
complicated and much less mnemonic than simply hotmail.com (for example, the canonical
Suppose that some application running in a user’s host needs to translate a hostname to an IP
address. The application will invoke the client side of DNS, specifying the hostname that
needs to be translated.
DNS in the user’s host then takes over, sending a query message into the network.
All DNS query and reply messages are sent within UDP datagrams to port 53. After a delay,
ranging from milliseconds to seconds, DNS in the user’s host receives a DNS reply message
that provides the desired mapping. This mapping is then passed to the invoking application.
In this centralized design, clients simply direct all queries to the single DNS server, and the DNS
server responds directly to the querying clients. Although the simplicity of this design is
attractive, it is inappropriate for today’s Internet, with its vast (and growing) number of hosts.
The problems with a centralized design include:
A single point of failure. If the DNS server crashes, so does the entire Internet!
Traffic volume. A single DNS server would have to handle all DNS queries.
Distant centralized database. A single DNS server cannot be “close to” all the querying
clients. If we put the single DNS server in New York City, then all queries from Australia
must travel to the other side of the globe, perhaps over slow and congested links. This can
lead to significant delays.
Maintenance. The single DNS server would have to keep records for all Internet hosts. Not
only would this centralized database be huge, but it would have to be updated frequently to
account for every new host.
In order to deal with the issue of scale, the DNS uses a large number of servers, organized in
a hierarchical fashion and distributed around the world.
There are three classes of DNS servers—root DNS servers, top-level domain (TLD) DNS
servers, and authoritative DNS servers—organized in a hierarchy.
Root DNS servers. In the Internet there are 13 root DNS servers (labeled A through M),
most of which are located in North America.
Although we have referred to each of the 13 root DNS servers as if it were a single server,
each “server” is actually a network of replicated servers, for both security and reliability
purposes. All together, there are 247 root servers.
Top-level domain (TLD) servers: These servers are responsible for top-level domains such
as com, org, net, edu, and gov, and all of the country top-level domains such as in,uk, fr, ca.
Authoritative DNS servers: Every organization with publicly accessible hosts on the
Internet must provide publicly accessible DNS records that map the names of those hosts to
IP addresses. An organization’s authoritative DNS server houses these DNS records.
There is another important type of DNS server called the local DNS server. A local DNS
server does not strictly belong to the hierarchy of servers but is nevertheless central to the
Here DNS query is sent to local DNS server then to root server, then to TLD server and finally
to authoritative DNS server. DNS response arrives in the reverse order.
2) Iterative Queries:
Here DNS query will be sent to Local DNS server, then to root server. Root server sends the IP
address of TLD server. Now local DNS server sends query to TLD DNS server. TLD DNS
server sends the IP address of authoritative DNS server to local DNS server. Now Local DNS
server sends query to authoritative DNS server. Authoritative DNS server sends the IP address of
host to local DNS server. Local DNS server sends it to the host.
DNS Caching
In a query chain, when a DNS server receives a DNS reply it can cache the mapping in its local
memory.
If a hostname/IP address pair is cached in a DNS server and another query arrives to the
DNS server for the same hostname, the DNS server can provide the desired IP address, even if it
is not authoritative for the hostname. Because hosts and mappings between hostnames and IP
addresses are by no means permanent, DNS servers discard cached information after a period of
time (often set to two days).
DNS Messages
The first 12 bytes is the header section, which has a number of fields.
The first field is a 16-bit number that identifies the query. This identifier is copied into the
reply message to a query, allowing the client to match received replies with sent queries.
There are a number of flags in the flag field.
A 1-bit query/reply flag indicates whether the message is a query (0) or a reply (1). A1-bit
authoritative flag is set in a reply message when a DNS server is an authoritative server for a
queried name.
A 1-bit recursion-desired flag is set when a client (host or DNS server) desires that the DNS
server perform recursion when it doesn’t have the record.
A 1-bit recursion available field is set in a reply if the DNS server supports recursion.
In the header, there are also four number-of fields. These fields indicate the number of
occurrences of the four types of data sections that follow the header.
The question section contains information about the query that is being made. This section
includes (1) a name field that contains the name that is being queried, and (2) a type field that
indicates the type of question being asked about the name
In a reply from a DNS server, the answer section contains the resource records for the name
that was originally queried.
Suppose you have just created an exciting new startup company called Network Utopia. The first
thing you’ll surely want to do is register the domain name networkutopia.com at a registrar. A
registrar is a commercial entity that verifies the uniqueness of the domain name, enters the
domain name into the DNS database (as discussed below), and collects a small fee from you for
its services.
For the primary authoritative server for networkutopia.com, the registrar would insert the
following two resource records into the DNS system:
(dns1.networkutopia.com, 212.212.212.1, A)
In P2P architecture, there is minimal (or no) reliance on always-on infrastructure servers.
Instead, pairs of intermittently connected hosts, called peers, communicate directly with each
other.
In P2P file distribution, each peer can redistribute any portion of the file it has received to
any other peers, thereby assisting the server in the distribution process.
The most popular P2P file distribution protocol is BitTorrent.
The distribution time is the time it takes to get a copy of the file to all N peers.
In the client-server architecture, none of the peers aids in distributing the file. We make the
following observations:
The server must transmit one copy of the file to each of the N peers. Thus the server must
transmit NF bits. Since the server’s upload rate is us, the time to distribute the file must be at
least NF/us.
Let dmin denote the download rate of the peer with the lowest download rate, that is, dmin =
min{d1,dp,...,dN}. The peer with the lowest download rate cannot obtain all F bits of the file
in less than F/dmin seconds. Thus the minimum distribution time is at least F/dmin.
Putting these two observations together, we obtain
Finally, observe that the total upload capacity of the system as a whole is equal to the upload
rate of the server plus the upload rates of each of the individual peers, that is, utotal = us + u1
+ … + uN. The system must deliver (upload) F bits to each of the N peers, thus delivering a
total of NF bits. This cannot be done at a rate faster than utotal. Thus, the minimum
distribution time is also at least NF/(us + u1 + … + uN).
Putting these three observations together, we obtain the minimum distribution time for P2P,
denoted by DP2P.
Below Figure compares the minimum distribution time for the client-server and P2P
architectures assuming that all peers have the same upload rate u.
BitTorrent
In BitTorrent, the collection of all peers participating in the distribution of a particular file is
called a torrent.
Peers in a torrent download equal-size chunks of the file from one another, with a typical
chunk size of 256 KBytes.
When a peer first joins a torrent, it has no chunks. Over time it accumulates more and more
chunks. While it downloads chunks it also uploads chunks to other peers.
Once a peer has acquired the entire file, it may leave the torrent, or remain in the torrent and
continue to upload chunks to other peers.
Also, any peer may leave the torrent at any time with only a subset of chunks, and later rejoin
the torrent.
To determine which requests peer responds to, BitTorrent uses a clever trading algorithm.
The basic idea is that peer gives priority to the neighbors that are currently supplying data to
it at the highest rate. Specifically, for each of its neighbors, peer continually measures the
rate at which it receives bits and determines the four peers that are feeding bits at the highest
rate. Peer then reciprocates by sending chunks to these same four peers.
Every 10 seconds, peer recalculates the rates and possibly modifies the set of four peers.
In BitTorrent lingo, these four peers are said to be unchoked.
Importantly, every 30 seconds, peer also picks one additional neighbor at random and sends
it chunks. In BitTorrent lingo, this randomly selected peer is said to be optimistically
unchoked.
The random neighbor selection also allows new peers to get chunks, so that they can have
something to trade.
The incentive mechanism for trading just described is often referred to as tit-for-tat.
this design, the querying peer sends its query to all other peers, and the peers containing the
(key, value) pairs that match the key can respond with their matching pairs.
Such an approach is completely unscalable as it would require each peer to know about all
other peers and have each query sent to all peers.
An elegant approach to designing a DHT is to first assign an identifier to each peer, where
each identifier is an integer in the range [0, 2n-1] for some fixed n.
This also require each key to be an integer in the same range.
To create integers out of such keys, we will use a hash function that maps each key (e.g.,
social security number) to an integer in the range [0, 2n-1].
Problem of storing the (key, value) pairs in the DHT:
The central issue here is defining a rule for assigning keys to peers. Given that each peer has
an integer identifier and that each key is also an integer in the same range, a natural approach
is to assign each (key, value) pair to the peer whose identifier is the closest to the key.
To implement such a scheme, let’s define the closest peer as the closest successor of the key.
Now suppose a peer, Alice, wants to insert a (key, value) pair into the DHT. Conceptually,
this is straightforward: She first determines the peer whose identifier is closest to the key; she
then sends a message to that peer, instructing it to store the (key, value) pair.
If Alice were to keep track of all the peers in the system (peer IDs and corresponding IP
addresses), she could locally determine the closest peer. But such an approach requires each
peer to keep track of all other peers in the DHT—which is completely impractical for a large-
scale system with millions of peers.
Circular DHT
To address this problem of scale, let’s now consider organizing the peers into a circle. In this
circular arrangement, each peer only keeps track of its immediate successor and immediate
predecessor (modulo 2n).
Each peer is only aware of its immediate successor and predecessor; for example, peer 5 knows
the IP address and identifier for peers 8 and 4 but does not necessarily know anything about any
other peers that may be in the DHT.
Now suppose that peer 3 wants to determine which peer in the DHT is responsible for key 11.
Using the circular overlay, the origin peer (peer 3) creates a message saying “Who is responsible
for key 11?” and sends this message clockwise around the circle. Whenever a peer receives such
a message, because it knows the identifier of its successor and predecessor, it can determine
whether it is responsible for (that is, closest to) the key in question. If a peer is not responsible
for the key, it simply sends the message to its successor. So, for example, when peer 4 receives
the message asking about key 11, it determines that it is not responsible for the key (because its
successor is closer to the key), so it just passes the message along to peer 5. This process
continues until the message arrives at peer 12, who determines that it is the closest peer to key
11. At this point, peer 12 can send a message back to the querying peer, peer 3, indicating that it
is responsible for key 11.
Although each peer is only aware of two neighboring peers, to find the node responsible for a
key (in the worst case), all N nodes in the DHT will have to forward a message around the circle;
N/2 messages are sent on average.
Shortcuts are used to expedite the routing of query messages. Specifically, when a peer receives
a message that is querying for a key, it forwards the message to the neighbor (successor neighbor
or one of the shortcut neighbors) which is the closet to the key.
When peer 4 receives the message asking about key 11, it determines that the closet peer to the
key (among its neighbors) is its shortcut neighbor 10 and then forwards the message directly to
peer 10. Clearly, shortcuts can significantly reduce the number of messages used to process a
query.
Peer Churn
In P2P systems, a peer can come or go without warning. Thus, when designing a DHT, we also
must be concerned about maintaining the DHT overlay in the presence of such peer churn.
To handle peer churn, we will now require each peer to track its first and second successors; for
example, peer 4 now tracks both peer 5 and peer 8. We also require each peer to periodically
verify that its two successors are alive
Let’s now consider how the DHT is maintained when a peer abruptly leaves. For example,
suppose peer 5 in above figure abruptly leaves. In this case, the two peers preceding the departed
peer (4 and 3) learn that 5 has departed, since it no longer responds to ping messages. Peers 4
and 3 thus need to update their successor state information. Let’s consider how peer 4 updates its
state:
1. Peer 4 replaces its first successor (peer 5) with its second successor (peer 8).
2. Peer 4 then asks its new first successor (peer 8) for the identifier and IP address of its
immediate successor (peer 10). Peer 4 then makes peer 10 its second successor.
Let’s say a peer with identifier 13 wants to join the DHT, and at the time of joining, it only
knows about peer 1’s existence in the DHT. Peer 13 would first send peer 1 a message, saying
“what will be 13’s predecessor and successor?” This message gets forwarded through the DHT
until it reaches peer 12, who realizes that it will be 13’s predecessor and that its current
successor, peer 15, will become 13’s successor. Next, peer 12 sends this predecessor and
successor information to peer 13. Peer 13 can now join the DHT by making peer 15 its successor
and by notifying peer 12 that it should change its immediate successor to 13.
Example application:
1. The client reads a line of characters (data) from its keyboard and sends the data to the server.
2. The server receives the data and converts the characters to uppercase.
3. The server sends the modified data to the client.
4. The client receives the modified data and displays the line on its screen.
UDPClient.py
Here is the code for the client side of the application:
from socket import *
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)
message = raw_input(’Input lowercase sentence:’)
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
UDPServer.py
serverPort = 12000
serverSocket.bind((’’, serverPort))
while 1:
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)
One end of the TCP connection is attached to the client socket and the other end is attached
to a server socket.
When creating the TCP connection, we associate with it the client socket address (IP address
and port number) and the server socket address (IP address and port number). With the TCP
connection established, when one side wants to send data to the other side, it just drops the
data into the TCP connection via its socket. This is different from UDP, for which the server
must attach a destination address to the packet before dropping it into the socket.
During the three-way handshake, the client process knocks on the welcoming door of the
server process. When the server “hears” the knocking, it creates a new door— more
precisely, a new socket that is dedicated to that particular client.
TCPClient.py
TCPServer.py