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

Lecture 13 P2P File Sharing and Socket Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 13 P2P File Sharing and Socket Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Peer-to-peer (P2P) architecture

 no always-on server mobile network


 arbitrary end systems directly national or global ISP

communicate
 peers request service from other
peers, provide service in return to
other peers local or
regional ISP
• self scalability – new peers bring new
service capacity, and new service demands home network content
 peers are intermittently connected provider
network datacenter
network
and change IP addresses
• complex management
 examples: P2P file sharing (BitTorrent), enterprise
network
streaming (KanKan), VoIP (Skype)
Application Layer: 2-1
File distribution: client-server vs P2P
Q: how much time 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 u2 capacity
us d2
server
di
uN network (with abundant
bandwidth) ui
dN
ui: peer i upload
capacity
Introduction: 1-2
File distribution time: client-server
 server transmission: must sequentially
send (upload) N file copies:
F
• time to send one copy: F/us us

• time to send N copies: NF/us di


network
ui
 client: each client must download
file copy
• dmin = min client download rate
• min client download time: F/dmin
time to distribute F
to N clients using
Dc-s > max{NF/us,,F/dmin}
client-server approach

increases linearly in N Introduction: 1-3


File distribution time: P2P
 server transmission: must upload at
least one copy:
F
• time to send one copy: F/us us
 client: each client must download di
network
file copy ui
• min client download time: F/dmin
 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)}
increases linearly in N …
… but so does this, as each peer brings service capacity Application Layer: 2-4
Client-server 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
Application Layer: 2-5
P2P file distribution: BitTorrent
 file divided into 256Kb chunks
 peers in torrent send/receive file chunks
tracker: tracks peers torrent: group of peers
participating in torrent exchanging chunks of a file

Alice arrives …
… obtains list
of peers from tracker
… and begins exchanging
file chunks with peers in torrent

Application Layer: 2-6


P2P file distribution: BitTorrent
 peer joining torrent:
• has no chunks, but will accumulate them
over time from other peers
• registers with tracker to get list of peers,
connects to subset of peers (“neighbors”)

 while downloading, peer uploads chunks to other peers


 peer may change peers with whom it exchanges chunks
 churn: peers may come and go
 once peer has entire file, it may (selfishly) leave or (altruistically) remain
in torrent

Application Layer: 2-7


BitTorrent: requesting, sending file chunks

Requesting chunks: Sending chunks: tit-for-tat


 at any given time, different  Alice sends chunks to those four
peers have different subsets peers currently sending her chunks
of file chunks at highest rate
 periodically, Alice asks each • other peers are choked by Alice (do
not receive chunks from her)
peer for list of chunks that • re-evaluate top 4 every10 secs
they have  every 30 secs: randomly select
 Alice requests missing another peer, starts sending
chunks from peers, rarest chunks
first • “optimistically unchoke” this peer
• newly chosen peer may join top 4
Application Layer: 2-8
BitTorrent: tit-for-tat
(1) Alice “optimistically unchokes” Bob
(2) Alice becomes one of Bob’s top-four providers; Bob reciprocates
(3) Bob becomes one of Alice’s top-four providers

higher upload rate: find better trading


partners, get file faster !

Application Layer: 2-9


Socket programming
goal: learn how to build client/server applications that
communicate using sockets
socket: door between application process and end-end-transport
protocol
application application
socket controlled by
process process app developer

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

Application Layer: 2-10


Socket programming
Two socket types for two 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
data to server
2. server receives the data and converts characters to uppercase
3. server sends modified data to client
4. client receives modified data and displays line on its screen

Application Layer: 2-11


Socket programming with UDP
UDP: no “connection” between client & server
 no handshaking before sending data
 sender explicitly attaches IP destination address and port # to each
packet
 receiver extracts sender IP address and port# from received packet
UDP: transmitted data may be lost or received out-of-order
Application viewpoint:
 UDP provides unreliable transfer of groups of bytes (“datagrams”)
between client and server

Application Layer: 2-12


Client/server socket interaction: UDP
server (running on serverIP) client
create socket:
create socket, port= x: clientSocket =
serverSocket = socket(AF_INET,SOCK_DGRAM)
socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP and
port=x; send datagram via
read datagram from clientSocket
serverSocket

write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket
Application Layer: 2-13
Example app: UDP client
Python UDPClient
include Python’s socket library from socket import *
serverName = ‘hostname’
serverPort = 12000
create UDP socket for server clientSocket = socket(AF_INET,
SOCK_DGRAM)
get user keyboard input message = raw_input(’Input lowercase sentence:’)
attach server name, port to message; send into socket clientSocket.sendto(message.encode(),
(serverName, serverPort))
read reply characters from socket into string
modifiedMessage, serverAddress =
clientSocket.recvfrom(2048)
print out received string and close socket print modifiedMessage.decode()
clientSocket.close()

Application Layer: 2-14


Example app: UDP server
Python UDPServer
from socket import *
serverPort = 12000
create UDP socket serverSocket = socket(AF_INET, SOCK_DGRAM)
bind socket to local port number 12000 serverSocket.bind(('', serverPort))
print (“The server is ready to receive”)
loop forever while True:
Read from UDP socket into message, getting message, clientAddress = serverSocket.recvfrom(2048)
client’s address (client IP and port)
modifiedMessage = message.decode().upper()
send upper case string back to this client serverSocket.sendto(modifiedMessage.encode(),
clientAddress)

Application Layer: 2-15


Socket programming with TCP
Client must contact server  when contacted by client, server
 server process must first be TCP creates new socket for server
running process to communicate with that
 server must have created socket particular client
(door) that welcomes client’s • allows server to talk with multiple
contact clients
• source port numbers used to
Client contacts server by: distinguish clients (more in Chap 3)
 Creating TCP socket, specifying IP
address, port number of server Application viewpoint
process TCP provides reliable, in-order
 when client creates socket: client byte-stream transfer (“pipe”)
TCP establishes connection to between client and server
server TCP
Application Layer: 2-16
Client/server socket interaction: TCP
server (running on hostid) client
create socket,
port=x, for incoming
request:
serverSocket = socket()

wait for incoming create socket,


connection request
TCP connect to hostid, port=x
connectionSocket = connection setup clientSocket = socket()
serverSocket.accept()

send request using


read request from clientSocket
connectionSocket

write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
Application Layer: 2-17
Example app: TCP client
Python TCPClient
from socket import *
serverName = ’servername’
serverPort = 12000
create TCP socket for server, clientSocket = socket(AF_INET, SOCK_STREAM)
remote port 12000
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
clientSocket.send(sentence.encode())
No need to attach server name, port modifiedSentence = clientSocket.recv(1024)
print (‘From Server:’, modifiedSentence.decode())
clientSocket.close()

Application Layer: 2-18


Example app: TCP server
Python TCPServer
from socket import *
serverPort = 12000
create TCP welcoming socket serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
server begins listening for
incoming TCP requests
serverSocket.listen(1)
print ‘The server is ready to receive’
loop forever while True:
server waits on accept() for incoming connectionSocket, addr = serverSocket.accept()
requests, new socket created on return

read bytes from socket (but sentence = connectionSocket.recv(1024).decode()


not address as in UDP) capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.
encode())
close connection to this client (but not connectionSocket.close()
welcoming socket)
Application Layer: 2-19

You might also like