Lecture 13 P2P File Sharing and Socket Programming
Lecture 13 P2P File Sharing and Socket Programming
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 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
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
transport transport
network network controlled
link
by OS
link Internet
physical physical
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()
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()