0% found this document useful (0 votes)
29 views

Lec 10-12 P2P Applications and Sockets

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lec 10-12 P2P Applications and Sockets

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Computer Networks (CS F303)

BITS Pilani Virendra Singh Shekhawat


Department of Computer Science and Information Systems
Pilani Campus
Today’s Agenda

• Peer to Peer Applications and Protocols


– P2P File Distribution, Bit Torrent Protocol

• Database Implementation Protocol in P2P Networks


– Distributed Hash Tables (DHTs)
– Chord Protocol

2
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Peer to Peer (P2P) Architecture

• No always-on server
• Arbitrary end systems directly communicate
• Peers are intermittently connected
• Examples
– File distribution (BitTorrent)
– Streaming (KanKan)
– VoIP (Skype)

3
Computer Networks CS F303 BITS Pilani, Pilani Campus
File Distribution: P2P vs CS
How much time required to distribute file (size F) from one server to N peers?
– Peer upload/download capacity is limited resource

us: server upload


capacity

di: peer i download


file, size F u1 d1 capacity
us u2 d2
server
di
uN network (with abundant
bandwidth) ui
dN
ui: peer i upload
capacity

4
Computer Networks CS F303 BITS Pilani, Pilani Campus
File Distribution Time – Client Server
• Server transmission: must F
sequentially send (upload) N us

file copies: di
network
– Time to send N copies: NF/us ui

 Client: each client must download file copy


 dmin = min client download rate
 Slowest client download time: F/dmin

time to distribute F
to N clients using
client-server approach Dc-s > max{NF/us,,F/dmin}

5
Computer Networks CS F303 BITS Pilani, Pilani Campus
File Distribution Time - Peer to Peer
• Server transmission: must upload at least one copy
F
– time to send one copy: F/us us
di
 Client: each client must download file copy network
 Slowest client download time: F/dmin ui

 Clients: as aggregate must download NF bits


 max upload rate (limiting max download rate) is us + Sui

Time to distribute F
to N clients using
P2P approach
DP2P > max{F/us,,F/dmin,,NF/(us + Sui)}

6
Computer Networks CS F303 BITS Pilani, Pilani Campus
Examples

• Consider distributing a file of F bits to N peers using C-S. Assume a fluid


model where the server can simultaneously transmit to multiple peers,
transmitting to each peer at different rates, as long as combined rate dose
not exceed us
• Distribution schemes
– Consider a distribution scheme in which the server sends the file to each client, in
parallel, at a rate of a rate of us/N
• Assume us/N <= dmin

– Consider a distribution scheme in which the server sends the file to each client, in
parallel, at a rate of dmin
• Assume us/N >= dmin
7
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Exercise

• Distributing a File F = 15 Gbits to 100 peers


• Server upload rate is us = 30 Mbps
• Each peer download rate is di = 2 Mbps
• Each peer upload rate is u = 0.5 Mbps
• Question
– Calculate minimum distribution time for F in both CS and P2P scenarios.

8
Computer Networks (CS F303) BITS Pilani, Pilani Campus
CS vs P2P: Example

client upload rate = u, F/u = 1 hour, us = 10u, dmin ≥ us


3.5
P2P

Minimum Distribution Time


3
Client-Server
2.5

1.5

0.5

0
0 5 10 15 20 25 30 35

N
9
Computer Networks CS F303 BITS Pilani, Pilani Campus
P2P File Distribution: BitTorrent

• File divided into 64 KB to 1 MB size (typically


256 KB) chunks

• Peers in torrent send/receive file chunks


– At any given time, each peer will have a subset of
chunks from the file.
– A peer asks its neighbors for the list of chunks they
have and gets list from each.
– A peer needs to take a call on-
• Which chunks should it request first from its neighbor?
• To which of its neighbors it should send requested
chunks?
– Tit-for-tat trading algorithm

10
Computer Networks CS F303 BITS Pilani, Pilani Campus
The lookup problem

N2 N3
N1

Internet
Key = “data item”
Value = video lecture ?
Client
Publisher
N4 N6 Lookup(“data item”)

N5
Decentralized network with several peers (servers/clients)
How to find specific peer that hosts desired data within this network?
11
Computer Networks CS F303 BITS Pilani, Pilani Campus
P2P Protocols

Napster

Gnutella
Kazaa (Skype is based on Kazaa)
12
Computer Networks CS F303 BITS Pilani, Pilani Campus
Distributed Hash Table (DHT)

• Key-value pairs are distributed across peers.


• Any Peer can query the distributed database with a particular key.
• Distributed DB locate the Peers that have the corresponding (key,
value) pairs and return to the querying Peer.
• Each peer only hold a small subset of the total key-value pairs.
• Any Peer can insert new (key, value) pairs into the DB.

13
Computer Networks CS F303 BITS Pilani, Pilani Campus
DHT Implementation [.1]

• Randomly scatter the (key, value) pairs across all the peers.

• Each peer maintain a list of the IP addresses of all peers.

• The querying peer sends its query to all other peers.

• The peers containing the (key, value) pairs that match the key can respond
with matching pairs.

14
Computer Networks CS F303 BITS Pilani, Pilani Campus
DHT Implementation [..2]: Circular DHT

• Hash function assigns each “node” and “key” an m-bit identifier using a
base hash function such as SHA-1
– Node_ID = hash(IP, Port)
N63
– Key_ID = hash(original key) N60 N2
k7
ID Space: 0 to 2m-1 k58
N10
Here: m = 6 k11

N50 k16
Range = 64
k46 N20

Assign (key-value) pair to the peer that has N40 k39 k25

the closest ID.


15
Computer Networks CS F303 BITS Pilani, Pilani Campus
Chord Protocol:Lookup Operation Example

Predecessor: pointer to the previous node on the id


circle
Successor: pointer to the succeeding node on the
id circle

 ask node n to find the successor of id


 If id between n and its successor
return successor
 else forward query to n´s successor and
so on

Algorithm complexity??? 16
Computer Networks CS F303 BITS Pilani, Pilani Campus
Scalable node localization
• Each node n contains a routing table with up-to m entries (m: number of bits
of the identifier) => finger table
• ith entry in the table at node n contains the first node s that succeds n by at
least 2i-1
– s = successor (n + 2i-1)
– s is called the ith finger of node n

17
Computer Networks CS F303 BITS Pilani, Pilani Campus
Scalable node localization: Algorithm

• Search in finger table for the node which


is most immediatly precedes the key
• Invoke find_successor from that node

//ask node n to find the successor of id


n.find_successor(id)
if (id in (n,successor))
return successor
else //search the local table for the
//highest predecessor of id
n‘ = closest_preceding_node(id); n.closest_preceding_node(id)
return n‘.find_successor(id); for i=m down to 1 do
if (finger[i] in (n,id))
return finger[i];
Complexity??? return n; 18

Computer Networks CS F303 BITS Pilani, Pilani Campus


Failure Recovery (Peer Churn)

• Key step in failure recovery is maintaining correct successor pointers


• To achieve this, each node maintains a successor-list of its r nearest
successors on the ring
• If node n notices that its successor has failed, it replaces it with the first
live entry in the successor list
• The stabilize will correct finger table entries and successor-list entries
pointing to failed node
• Stabilization protocol should be invoked based on the frequency of
nodes leaving and joining
19
Computer Networks CS F303 BITS Pilani, Pilani Campus
Next… Transport Layer

• Creating network Applications


– Socket Programming
• TCP vs. UDP Sockets
• Transport Layer
– Transport Layer Services
• Multiplexing/Demultiplexing
– Connectionless and Connection Oriented
» TCP and UDP
• Reliable data transfer (Protocol design)
• Flow control
• Congestion control
20
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket Programming [.1]
• What is a socket?
– To the kernel, a socket is an endpoint of communication.
– To an application, a socket is a file descriptor that lets the application read/write from/to the network.
• Remember: All Unix I/O devices, including networks, are modeled as files.

• Clients and servers communicate with each other by reading from and writing to socket
descriptors.

application application
socket controlled by
process process app developer

transport transport
network network controlled
link by OS
link Internet
physical physical

21
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket Programming [..2]

Two socket types for two different transport services:


– UDP: unreliable datagram
– TCP: reliable, byte stream-oriented

Application Example:
1. 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 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.

22
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket Programming with UDP

UDP: no “connection” between client & server


• No handshaking before sending data
• Sender explicitly attaches destination IP address and port # to each packet
• Receiver extracts sender IP address and port# from received packet

Note: Transmitted data may be lost or received out-of-order

23
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket Programming with TCP

Client contacts server by: • When contacted by client, server TCP


• Creating TCP socket, specifying IP creates a new socket for server process
address, port number of server process to communicate with that particular
• Server must have created socket (door) client
that welcomes client’s contact
– Allows server to talk with multiple clients
• Client TCP establishes connection to
server TCP

Application viewpoint:
TCP provides reliable, in-order byte-stream transfer (“pipe”) between client
and server.
24
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket Structure [.1]

struct sockaddr
{
unsigned short int sa_family; // address family, AF_xxx
char sa_data[14] ; // 14 bytes of protocol address
}

• sa_family – this remains AF_INET for stream and datagram sockets


• sa_data - contains destination address and port number for the socket

25
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket Structure [..2]
• Parallel structure to sockaddr
struct sockaddr_in
{
short int sin_family; // Address family (e.g., AF_INET)
unsigned short int sin_port; // Port number (e.g., htons (2240)
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8]; // same size as sockaddr
}
struct in_addr
{ unsigned long s_addr;
}
• sin_zero is used to pad the structure to the length of a structure sockaddr and hence is set to all zeros with
the function memset()
• Important – you can cast sockaddr_in to a pointer of type struct sockaddr and vice versa
• sin_family corresponds to sa_family and should be set to “AF_INET”.
• sin_port and sin_addr must be in NBO
26
Computer Networks (CS F303) BITS Pilani, Pilani Campus
NBO & HBO Conversion Functions

• Two types that can be converted


– short (two bytes)
– long (4-8 bytes)

• Primary conversion functions


– htons() // host to network short
– htonl() // host to network long
– ntohs // network to host short
– ntohl() // network to host long

• Very Important: Even if your machine is Big-Endian m/c, but you put your bytes in NBO
before putting them on to the network for portability

27
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Primary Socket System Calls

• socket() - create a new socket and return its descriptor


• bind() - associate a socket with a port and address
• listen() - establish queue for connection requests
• accept() - accept a connection request
• connect() - initiate a connection to a remote host
• recv() - receive data from a socket descriptor
• send() - send data to a socket descriptor
• close() - “one-way” close of a socket descriptor

28
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket System Calls:
Connectionless (e.g., UDP)

29
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket System Calls: Connection-
Oriented (e.g., TCP)

30
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket System Calls [.1]

• SOCKET: int socket(int domain, int type, int protocol);


– domain := AF_INET (IPv4 protocol)
– type := (SOCK_DGRAM or SOCK_STREAM )
– protocol := 0 (IPPROTO_UDP or IPPROTO_TCP)
– returned: socket descriptor (sockfd), -1 is an error

• BIND: int bind(int sockfd, struct sockaddr *my_addr, int addrlen);


– sockfd - socket descriptor (returned from socket())
– my_addr: socket address, struct sockaddr_in is used
– addrlen := sizeof(struct sockaddr)

31
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket System Calls [..2]

• LISTEN: int listen(int sockfd, int backlog);


– backlog: how many connections we want to queue

• ACCEPT: int accept(int sockfd, void *addr, int *addrlen);


– addr: here the socket-address of the caller will be written
– returned: a new socket descriptor (for the temporal socket)

• CONNECT: int connect(int sockfd, struct sockaddr *serv_addr, int


addrlen); //used by TCP client
– parameters are same as for bind()

32
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket System Calls […3]
• SEND: int send(int sockfd, const void *msg, int len, int flags);
– msg: message you want to send
– len: length of the message
– flags := 0
– returned: the number of bytes actually sent

• RECEIVE: int recv(int sockfd, void *buf, int len, unsigned int flags);
– buf: buffer to receive the message
– len: length of the buffer (“don’t give me more!”)
– flags := 0
– returned: the number of bytes received
33
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket System Calls [….4]
• SEND (DGRAM-style): int sendto(int sockfd, const void *msg, int len, int flags, const struct sockaddr *to, int
tolen);
– msg: message you want to send
– len: length of the message
– flags := 0
– to: socket address of the remote process
– tolen: = sizeof(struct sockaddr)
– returned: the number of bytes actually sent

• RECEIVE (DGRAM-style): int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr
*from, int *fromlen);
– buf: buffer to receive the message
– len: length of the buffer (“don’t give me more!”)
– from: socket address of the process that sent the data
– fromlen:= sizeof(struct sockaddr)
– flags := 0
– returned: the number of bytes received

• CLOSE: close (socketfd);


34
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Byte ordering routines

#include <sys/types.h>
#include <netinet/in.h>

u_long htonl(u_long hostlong); /* host-to-network, long integer */

u_short htons(u_short hostshort); /* host-to-network, short integer */

u_long ntohl(u_long netlong); /* network-to-host, long integer */

u_short ntohs(u_short netshort); /* network-to-host, short integer */

Address conversion routines


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

unsigned long inet_addr(char *ptr);


accepts a char string of IP address and returns a 32-bit network byte-order integer equivalent.
char *inet_ntoa(struct in_addr inaddr);
accepts an IP address expressed as a 32-bit quantity in network byte order and returns a string
expressed in dotted-decimal notation 35
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Simple TCP Server
#include <sys/types.h> listen(sockfd, 5);
#include <sys/socket.h>
#include <netinet/in.h> for(; ; ) {
#define SERVER_PORT 5888 clilen= sizeof(cliaddr);
connfd = accept(sockfd, (struct sockaddr *)
int main() &cliaddr, &clilen);
{ int sockfd,connfd,clilen,n;
if(connfd < 0)
char buf[256]; { printf(“Server Accept error \n”); exit(1); }
struct sockaddr_in servaddr, cliaddr;
printf("Client IP: %s\n",
sockfd = socket( AF_INET, SOCK_STREAM, 0); inet_ntoa(cliaddr.sin_addr));
if (sockfd < 0) printf("Client Port: %hu\n",
{ printf(“ Server socket error"); ntohs(cliaddr.sin_port));
exit(1); }
servaddr.sin_family = AF_INET; n = read(connfd, buf,256);
servaddr.sin_port = htons(SERVER_PORT); printf("Server read: \"%s\" [%d chars]\n", buf,
n);
servaddr.sin_addr.s_addr =
htonl(INADDR_ANY);
write(connfd, “Server Got Message”,n);
close(connfd);
if(bind(sockfd,(struct }
sockaddr*)&servaddr,sizeof(servaddr) <0 )
{ printf(“Server Bind Error”); exit(1); } }

36
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Simple TCP Client
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SERVER_PORT 5888
int main()
{ int sockfd, clifd,len;
char buf[256];
struct sockaddr_in servaddr;
sockfd = socket( AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) { printf(“ Server socket error"); exit(1); }

servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
servaddr.sin_addr.s_addr = inet_addr(“172.24.2.4”);

connect(sockfd,(struct sockaddr*)&servaddr, sizeof(servaddr));

print(“Enter Message \n”);


fgets(buf,256,stdin);
write(sockfd, buf, strlen(buf));

read(sockfd, buf,256);
printf(“Client Received%s\n",buf);
close(sockfd);
}
37
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Simple UDP Server
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SERVER_PORT 9988
int main()
{ int sockfd, clilen;
char buf[256];
struct sockaddr_in servaddr, cliaddr;
sockfd = socket( AF_INET, SOCK_DGRAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
servaddr.sin_addr.s_addr =htonl(INADDR_ANY);
if (bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) <0 )
{ printf(“Server Bind Error”); exit(1); }
for(; ; )
{ clilen= sizeof(cliaddr);
recvfrom(sockfd,buf,256,0,(struct sockaddr*)&cliaddr,&clilen);

printf(“Server Received:%s\n”,buf);

sendto(sockfd,“Server Got Message",18, 0,(struct sockaddr*)&cliaddr,clilen);


}
}
38
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Simple UDP Client

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
printf(“Enter Message\n”);
#define SERVER_PORT 9988 fgets(buf,255,stdin);
#define SERVER_IPADDR “172.24.2.4” len= sizeof(server);
int main()
{ int sockfd,len; sendto(sockfd,buf,strlen(buf), 0,(struct
char buf[256]; sockaddr*)&servaddr,len);
struct sockaddr_in ,cliaddr,servaddr;
recvfrom(sockfd,buf,256,0,NULL,NULL);
servaddr.sin_family = AF_INET; printf(“Clinet Received: %s \n”,buf);
servaddr.sin_port = htons(SERVER_PORT); close(sockfd);
servaddr.sin_addr.s_addr = inet_addr(SERVER_IPADDR); }

sockfd = socket( AF_INET, SOCK_DGRAM, 0);

cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(0);
cliaddr.sin_addr.s_addr =htonl(INADDR_ANY);
bind(sockfd,(struct sockaddr*)&cliaddr,sizeof(cliaddr));

39
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Thank You!

40
Computer Networks (CS F303) BITS Pilani, Pilani Campus

You might also like