Lec 10-12 P2P Applications and Sockets
Lec 10-12 P2P Applications and Sockets
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
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
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
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 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
8
Computer Networks (CS F303) BITS Pilani, Pilani Campus
CS vs P2P: Example
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
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)
13
Computer Networks CS F303 BITS Pilani, Pilani Campus
DHT Implementation [.1]
• Randomly scatter the (key, value) pairs across all the 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
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
• 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]
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
23
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket Programming with 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
}
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
• 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
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]
31
Computer Networks (CS F303) BITS Pilani, Pilani Campus
Socket System Calls [..2]
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
#include <sys/types.h>
#include <netinet/in.h>
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”);
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);
#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); }
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