Professional Documents
Culture Documents
Lp Application
Computer Networking:
A Top Down Approach
Featuring the Internet,
3rd edition.
Jim Kurose, Keith Ross
Addison-Wesley, July
2004.
Lp Application
ng dng mng
2.2 Web v HTTP
2.3 FTP
2.4 Electronic Mail
2.5 DNS
viTCP
2.8 Lp trnh socket vi
UDP
2.9 Xy dng mt Web
server
Lp Application
Chng 2: Lp Application
Mc tiu:
Khi nim, cc kha
cnh hin thc ca
cc giao thc ng
dng mng
Cc m hnh dch
v lp transport
M hnh clientserver
M hnh peer-to-peer
HTTP
FTP
SMTP / POP3 / IMAP
DNS
Lp trnh ng dng
mng
socket API
Lp Application
Mt s ng dng mng
E-mail
in thoi Internet
Web
ng nhp t xa
Chia s file P2P
gian thc
Tnh ton ln, tnh
ton song song
trn mng
Streaming cc video
clips
Lp Application
To mt ng dng mng
Vit chng trnh :
application
transport
network
data link
physical
application
transport
network
data link
physical
application
transport
network
data link
physical
Lp Application
Lp Application
Lp Application
clients:
Lp Application
hot ng
truyn thng trc tip
vi h thng u cui bt
k
cc im kt ni khng
lin tc v thay i a
ch IP
V d: Gnutella
linh hot cao nhng kh
qun l
Lp Application
Lp Application
10
11
Sockets
cc tin trnh gi/nhn
cc thng ip n/t
socket ca n
socket tng t nh ca
host /
server
Host /
server
process
socket
TCP vi
b m,
cc bin
iu khin
bi ngi
pht trin
ng dng
process
socket
Internet
TCP vi
b m,
cc bin
iu khin
bi h iu
hnh
Lp Application
12
Tin trnh nh a ch
nhn c thng
nh danh bao gm c a
ch IP v cc s cng
(port) lin kt vi tin
trnh trn host.
V d v s port:
HTTP server: 80
Mail server: 25
gi thng ip HTTP
IP address: 128.119.245.12
Port number: 80
Lp Application
13
thng ip
V d: yu cu, p ng
v phng thc cc
tin trnh gi v p ng
cc thng ip
Lp Application
14
Thi gian
mt s ng dng (vd:
in thoi Internet,
tr chi tng tc) yu
cu tr thp t
hiu qu
Lp Application
15
Mt s yu cu i vi cc ng dng ph bin
Application
Truyn file
e-mail
Web
audio/video
thi gian thc
audio/video lu
Tr chi tng tc
Tin nhn nhanh
Data loss
Bandwidth
Time Sensitive
khng
khng
Khng
chu li
mm do
mm do
mm do
audio: 5kbps-1Mbps
video:10kbps-5Mbps
Nh trn
Mt vi kbps
mm do
khng
khng
khng
c, 100 mili giy
chu li
chu li
khng
c, mt vi giy
c, 100 mili giy
C v khng
Lp Application
16
UDP:
truyn d liu khng tin
cy gia gi v nhn
Khng h tr: thit lp kt
ni, tin cy, iu khin
lung, iu khin tc nghn,
nh th, bo m bng
thng ti thiu
Th th sinh ra UDP lm
g?
Lp Application
17
Giao thc lp
Application
SMTP [RFC 2821]
Telnet [RFC 854]
HTTP [RFC 2616]
FTP [RFC 959]
c quyn
(vd: RealNetworks)
c quyn
(vd: Vonage,Dialpad)
Giao thc di lp
transport
TCP
TCP
TCP
TCP
TCP / UDP
UDP
Lp Application
18
Lp Application
19
Web v HTTP
Mt s thut ng chuyn mn
Web page (trang Web) bao gm cc objects (i
tng)
i tng c th l file HTML, hnh nh JPEG, Java
applet, file audio,
Trang Web file HTML c bn s cha mt s i
tng c tham chiu
Mi i tng c th nh a ch bng mt URL
V d URL:
www.someschool.edu/someDept/pic.gif
Tn host
Tn ng dn
Lp Application
20
Web
M hnh client/server
Client: trnh duyt yu
cu, nhn v hin th cc
i tng Web
Server: Web server gi
cc i tng p ng
cho yu cu
HTTP 1.0: RFC 1945
HTTP 1.1: RFC 2068
PC chy
Explorer
Server
chy
Apache Web
server
Mac chy
Navigator
Lp Application
21
tin v cc yu cu trc
ca client
vn lin quan
22
Cc kt ni HTTP
HTTP khng bn vng
Ch c ti a l mt i
tng c gi qua mt
kt ni TCP.
HTTP/1.0 dng HTTP
khng bn vng
HTTP bn vng
Nhiu i tng c th
c gi qua mt kt
ni TCP n gia client
v server.
HTTP/1.1 mc nhin
dng HTTP bn vng
Lp Application
23
(cha text,
tham chiu n
10 hnh)
www.someSchool.edu ch kt
ni TCP ti port 80. chp
nhn kt ni, thng bo cho
client
Thi gian
Lp Application
24
Lp Application
25
Thi
gian
truyn
file
Thi gian
Lp Application
26
HTTP bn vng
Vn vi HTTP khng bn
vng:
Yu cu 2 RTT mi i tng
H iu hnh lin quan n
mi kt ni TCP
Cc trnh duyt thng m
song song cc kt ni TCP
em v cc tham chiu n
cc i tng
HTTP bn vng
server m kt ni sau khi
gi p ng.
cc thng ip HTTP ca
cng client/server c gi
thng qua kt ni m
Lp Application
27
Thng ip yu cu HTTP
2 kiu thng ip HTTP: yu cu, p ng
Thng ip yu cu HTTP:
ASCII ( dng thc con ngi c th c c)
dng yu cu
(cc lnh GET, POST, GET /somedir/page.html HTTP/1.1
HEAD)
Host: www.someschool.edu
User-agent: Mozilla/4.0
cc dng Connection: close
header Accept-language:fr
k t xung dng,
(thm mt k t xung dng)
v u dng mi ch
im cui cng ca
thng ip
Lp Application
28
Lp Application
29
Ti ln form input
Phng php Post:
Web page thng cha
form input
Input c ti ln vo
server trong thn thc
th
www.somesite.com/animalsearch?monkeys&banana
Lp Application
30
hi server mc i
tng yu cu m khng
p ng
HTTP/1.1
GET, POST, HEAD
PUT
DELETE
xa file c xc nh
trong trng URL
Lp Application
31
Thng ip p ng HTTP
dng trng thi
(giao thc
m trng thi
cm t trng thi)
cc dng
header
HTTP/1.1 200 OK
Connection close
Date: Thu, 06 Aug 1998 12:00:15 GMT
Server: Apache/1.3.0 (Unix)
Last-Modified: Mon, 22 Jun 1998 ...
Content-Length: 6821
Content-Type: text/html
Lp Application
32
Lp Application
33
M kt ni TCP port 80
(port HTTP server mc nhin) ti cis.poly.edu.
Mi th nhp vo gi n
port 80 ti cis.poly.edu
34
Lp Application
35
V d:
Lp Application
36
server
ebay: 8734
Cookie file
amazon: 1678
ebay: 8734
Set-cookie: 1678
usual http request msg
cookie: 1678
usual http response msg
mt tun sau:
Cookie file
amazon: 1678
ebay: 8734
cookie: 1678
usual http response msg
server
to ID
1678 cho user
cookiespecific
action
cookiespectific
action
Lp Application
37
cc cookie (tip)
Cc cookie em li:
s cp php
gi mua hng
cc khuyn co
Lm th no gi trng
thi:
ngoi ra
cc cookie v s ring t:
cc cookie cho php
cc site bit nhiu hn
v bn
bn c th cung cp tn
v e-mail cho sites
Lp Application
38
n server ngun
user thit lp trnh duyt:
truy cp Web thng qua
cache
trnh duyt gi tt c yu
cu HTTP cho cache
server
ngun
client
client
Proxy
server
server
ngun
Lp Application
39
Web caching
Cache hot ng ti c
client v server
Tiu biu cache c ci
t bi ISP (trng hc,
cng ty, ISP ring)
yu cu ca client
Gim lu thng trn lin kt
truy cp
Internet rt ng nghch vi
caches: cho php nhng
ngi cung cp ni dung
ngho nn phn pht hiu
qu ni dung (cng vy
i vi P2P file sharing)
Lp Application
40
V d Caching
Gi s
kch thc trung bnh i
tng= 100,000 bits
tc trung bnh yu cu t
trnh duyt n server = 15/s
tr t router ni gi yu
cu n server ngun ri quay
li = 2 s
Kt qu
kh dng ca LAN = 15%
servers
ngun
Internet
cng cng
1.5 Mbps
lin kt truy cp
network gi
yu cu
10 Mbps LAN
cp= 100%
tng thi gian tr = tr Internet +
tr truy cp+ tr LAN = 2 s + cc
pht+ mili s
cache ni gi
yu cu
Lp Application
41
V d Caching (tip)
Gii php c th
tng bng thng truy cp ln,
v d 10 Mbps
Kt qu
servers
ngun
Internet
cng cng
= 15%
tng thi gian tr = tr Internet +
tr truy cp+ tr LAN = 2 s + mili
s + mili s
thng tng chi ph
10 Mbps
lin kt truy cp
network gi
yu cu
10 Mbps LAN
cache ni gi
yu cu
Lp Application
42
V d Caching (tip)
servers
ngun
ci t cache
tc h tr l 0.4
kt qu
Internet
cng cng
40% yu cu s c tha
mn hu nh ngay lp tc
60% yu cu s c tha
mn bi server ngun
kh dng trn lin kt
truy cp gim n 60%, do
tr khng ng k (vd 10 mili
s)
tng thi gian tr = tr
Internet + tr truy cp+ tr
LAN = 0.6*(2.01) s +
0.4*mili s < 1.4 s
1.5 Mbps
lin kt truy cp
network
gi yu cu
10 Mbps LAN
cache ni
gi yu cu
Lp Application
43
GET c iu kin
Mc tiu: khng gi i tng
nu cache cp nht
cache: xc nh ngy ca bn
sao cache trong yu cu
HTTP:
If-modified-since:
<date>
server: p ng khng cha
server
cache
HTTP request msg
If-modified-since:
<date>
HTTP response
i tng
khng
sa i
HTTP/1.0
304 Not Modified
HTTP response
HTTP/1.0 200 OK
i tng
c
sa i
<data>
Lp Application
44
2.3 FTP
Lp Application
45
user
ti host
truyn file
FTP
server
h thng
file t xa
h thng
file
cc b
Lp Application
46
FTP
client
kt ni d liu TCP
port 20
FTP
server
Lp Application
47
V d m tr v
gi nh vn bn ASCII trn
knh iu khin
USER username
PASS password
(ly) file
HTTP)
331 Username OK,
password required
125 data connection
already open;
transfer starting
425 Cant open data
connection
452 Error writing
file
Lp Application
48
Lp Application
49
Electronic Mail
outgoing
hng message
hp th user
user
agent
mail
server
SMTP
protocol: SMTP
User Agent
cn gi l mail reader
vit, sa i, c cc thng
ip mail
V d: Eudora, Outlook, elm,
Netscape Messenger
cc thng ip i v n
c lu trn server
SMTP
mail
server
user
agent
SMTP
user
agent
mail
server
user
agent
user
agent
user
agent
Lp Application
50
Mail Servers
mailbox (hp th) cha cc
thng ip n user
hng thng ip cho cc
thng ip email ra ngoi
(chun b gi)
giao thc SMTP gia cc
mail servers gi cc
thng ip email
client: mail server gi
server: mail server
nhn
mail
server
SMTP
SMTP
mail
server
user
agent
SMTP
user
agent
mail
server
user
agent
user
agent
user
agent
Lp Application
51
Lp Application
52
mail
server
3
mail
server
4
user
agent
Lp Application
53
V d tng tc SMTP
S:
C:
S:
C:
S:
C:
S:
C:
S:
C:
C:
C:
S:
C:
S:
220 hamburger.edu
HELO crepes.fr
250 Hello crepes.fr, pleased to meet you
MAIL FROM: <alice@crepes.fr>
250 alice@crepes.fr... Sender ok
RCPT TO: <bob@hamburger.edu>
250 bob@hamburger.edu ... Recipient ok
DATA
354 Enter mail, end with "." on a line by itself
Do you like ketchup?
How about pickles?
.
250 Message accepted for delivery
QUIT
221 hamburger.edu closing connection
Lp Application
54
DATA, QUIT
lnh trn cho php bn gi email khng cn dng email
client (reader)
Lp Application
55
SMTP
SMTP dng cc kt ni bn
vng
SMTP yu cu cc thng
ip (header & body) phi
dng thc 7-bit ASCII
SMTP server dng
CRLF.CRLF xc nh kt
thc thng ip
So snh vi HTTP:
HTTP: ko
SMTP: y
tt c u c tng tc
HTTP: mi i tng c
ng kn trong thng ip
p ng ca n
SMTP: nhiu i tng c
gi trong thng ip nhiu
phn
Lp Application
56
header
dng
trng
body
body
thng ip, ch c cc k
t ASCII
Lp Application
57
From: alice@crepes.fr
To: bob@hamburger.edu
Subject: Picture of yummy crepe.
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
base64 encoded data .....
.........................
......base64 encoded data
Lp Application
58
SMTP
SMTP
giao thc
truy cp
user
agent
59
ip
retr: trch xut thng ip
theo s
dele: xa
quit
S:
C:
S:
C:
S:
C:
S:
S:
S:
C:
S:
S:
C:
C:
S:
S:
C:
C:
S:
list
1 498
2 912
.
retr 1
<message 1 contents>
.
dele 1
retr 2
<message 1 contents>
.
dele 2
quit
+OK POP3 server signing off
Lp Application
on
60
POP3 v IMAP
nghin cu thm v POP3
V d trc dng ch
ti xung v xa.
Bob khng th c li
email nu thay i
client
ti xung-v-gi: sao
chp cc thng ip
trn cc client khc
nhau
POP3 khng gi trng
thi ca cc phin lm
vic
IMAP
Gi tt c thng ip
ti 1 v tr: server
Cho php user t chc
cc thng ip theo
dng th mc
IMAP gi trng thi
xuyn sut cc phin
lm vic:
cc tn ca th mc v
nh x gia ID ca
thng ip v tn th
mc
Lp Application
61
2.5 DNS
Lp Application
62
nh x gia a ch IP v
tn?
Lp Application
63
DNS
Cc dch v DNS
Tn Host chuyn thnh
a ch IP
B danh Host
cc tn ng chun v b
danh
Ti phn b
Cc Web server bn
sao: tp cc a ch IP
cho 1 tn ng chun
Lp Application
64
poly.edu
umass.edu
DNS serversDNS servers
65
DNS: cc server tn gc
tip xc qua server tn cc b no khng th phn gii tn
server tn gc:
e NASA Mt View, CA
f Internet Software C. Palo Alto,
CA (and 17 other locations)
13 name servers
gc trn ton cu
b USC-ISI Marina del Rey, CA
l ICANN Los Angeles, CA
Lp Application
66
Lp Application
67
Server tn cc b
Khng hon ton theo cu trc phn cp
Mi ISP (ISP c nhn, cng ty, trng hc)
c mt server cc b nh vy.
vn c gi ti DNS server cc b ca
n
Lp Application
68
V d
DNS server gc
Host ti cis.poly.edu
mun a ch IP ca
gaia.cs.umass.edu
4
5
DNS server cc b
dns.poly.edu
host yu cu
cis.poly.edu
gaia.cs.umass.edu
Lp Application
69
Cc truy vn
quy
truy vn quy:
y trch nhim phn
DNS server gc
truy vn tun t:
tn tip xc c
3
7
6
TLD DNS server
DNS server cc b
dns.poly.edu
tr li vi tn ca
1
8
server
Ti khng bit tn
, nhng c th hi
host yu cu
server ny
cis.poly.edu
gaia.cs.umass.edu
Lp Application
70
c ch cp nht/thng bo bn di c thit k
bi IETF
RFC 2136
http://www.ietf.org/html.charters/dnsind-charter.html
Lp Application
71
Cc DNS record
DNS: c s d liu phn b lu tr cc record ti nguyn (RR)
Type=CNAME
name l b danh ca tn
chun (tn thc)
www.ibm.com l tn thc
Type=NS
servereast.backup2.ibm.com
name l tn min (vd:
value l tn chun
foo.com)
value l tn host ca
Type=MX
server tn c thm quyn
value l tn ca email
cho tn min ny
server lin kt vi name
Lp Application
72
Lp Application
73
Lp Application
74
75
Lp Application
76
77
a ch IP
ni dung
Bob
directory server
trung tm
1
peers
1
1
2
Alice
Lp Application
78
c chai
Xm phm bn quyn
Lp Application
79
chung
nhiu Gnutella client
hin thc giao thc
overlay network: th
c cnh gia peer X v
Y nu c 1 kt ni TCP
tt c cc peer ang
hot ng v cc cnh
l mng overlay
network
cnh khng phi l mt
lin kt vt l
peer s kt ni vi < 10
peer ln cn
Lp Application
80
ip gi trn cc kt
ni TCP ang c
peer chuyn thng
ip truy vn
QueryHit
gi trn ng
ngc chiu
Truyn File:
HTTP
Truy vn
QueryHit
Truy vn
QueryHit
linh hot:
hn ch trn ngp
theo phm vi
Lp Application
81
Gnutella: hi t Peer
Hi t peer X phi tm mt s peer khc trong
Gnutella network: dng mt danh sch cc peer
d tuyn
2. X ln lt th to TCP vi cc peer trn danh sch
cho n khi kt ni thit lp c vi Y
3. X gi thng ip Ping n Y; Y chuyn thng ip
Ping.
4. Tt c cc peer nhn thng ip Ping s tr li
bng thng ip Pong
5. X nhn c nhiu thng ip Pong. N sau c
th thit lp thm cc kt ni TCP.
Peer leaving: xem mt s vn mng gia nh!
1.
Lp Application
82
KaZaA
Mi peer l mt hoc
c gn thnh ch huy
nhm
ni dung trong tt c
thnh vin bn trong
ordinary peer
group-leader peer
neighoring relationships
in overlay network
Lp Application
83
KaZaA: Truy vn
Mi file c mt bm (hash) hoc mt m t
ca n
Nhm ch huy p ng vi truy vn ca
nhm:
84
KaZaA: cc th thut
Hn ch khi ti ln ng thi
Xp hng yu cu
Khch l u tin
ti xung song song
Lp Application
85
Lp Application
86
Lp trnh socket
Mc tiu: nghin cu cch xy dng ng dng truyn
thng client/server dng sockets
Socket API
c gii thiu trong
socket
mt giao din host-cc b,
to bi ng dng,
iu khin bi h iu hnh
interface (mt ca)
trong tin trnh ng
dng c th gi v nhn
cc thng ip n/t cc
tin trnh khc
Lp Application
87
tin trnh
tin trnh
socket
TCP vi
b m,
cc bin
host hoc
server
internet
socket
TCP vi
b m,
cc bin
iu khin bi
ngi pht trin
ng dng
iu khin bi
h iu hnh
host hoc
server
Lp Application
88
Khi c tip xc bi
Nhn di gc ng dng
TCP cung cp vic truyn cc byte
tin cy v theo th t
gia client v server
Lp Application
89
Client
hostid)
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
TCP
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
90
Thut ng Stream
keyboard
monitor
output
stream
inFromServer
tin
Process
trnh
Client
input
stream
outToServer
cc k t c chy vo
hoc ra khi mt tin trnh
Mt input stream ch
ngun vo ca mt tin
trnh, vd: bn phm hoc
socket.
Mt output stream ch
ngun ra ca mt tin trnh,
vd: mn hnh hoc socket.
inFromUser
Mt stream l mt chui
input
stream
client
TCP
clientSocket
socket
to network
TCP
socket
from network
Lp Application
91
Lp Application
92
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
Lp Application
93
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
Gi dng
n server
outToServer.writeBytes(sentence + '\n');
c dng
t server
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Lp Application
94
to
socket mi tip xc
ti port 6789
Ch, client
tip cn vi
server
To input
stream, gn vo
socket
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
Lp Application
95
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
c dng
t socket
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
Vit dng ra
t socket
outToClient.writeBytes(capitalizedSentence);
}
}
Lp Application
96
Lp Application
97
gc nhn ng dng
Lp Application
98
Client
create socket,
clientSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
Lp Application
99
Client
Process
process
monitor
inFromUser
keyboard
Input: nhn gi
(cn gi l TCP
nhn dng byte)
UDP
packet
receivePacket
(cn gi l TCP
gi dng byte)
sendPacket
Output: gi gi
UDP
packet
client
UDP
clientSocket
socket
to network
UDP
socket
from network
Lp Application
100
to
input stream
to
client socket
dch
hostname thnh
a ch IP dng DNS
class UDPClient {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("hostname");
101
to datagram vi
d liu gi,
di, a ch IP, port
gi datagram
n server
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
c datagram
t server
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
Lp Application
102
To
datagram socket
ti port 9876
class UDPServer {
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
To khng gian
nhn datagram
nhn
datagram
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Lp Application
103
ly a ch IP
port #, ca
ngi gi
sendData = capitalizedSentence.getBytes();
to datagram
gi ti client
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);
vit
datagram
vo socket
serverSocket.send(sendPacket);
}
}
Lp Application
104
Lp Application
105
header
ly file c yu cu t
h thng file ca server
to thng ip p ng
HTTP:
c th yu cu file
dng trnh duyt (v d:
IE)
xem gio trnh bit
thm chi tit
gi p ng n client
Lp Application
106
Chng 2: Tng kt
Tng quan mt s vn v ng dng mng!
Cc kin trc ng dng
client-server
P2P
lai
cc yu cu dch v:
tin cy, bandwidth, tr
transport
connection-oriented, tin
cy: TCP
khng tin cy, datagrams:
UDP
Lp Application
107
Chng 2: Tng kt
Phn quan trng: nghin cu v cc giao thc
trao i thng ip yu
cu/tr li in hnh:
iu khin vi cc thng ip
d liu
in-band, out-of-band
tp trung v khng tp trung
khng trng thi v c trng
thi
truyn thng ip tin cy v
khng tin cy
s phc tp ca cc vn
lin quan n mng
Lp Application
108