0% found this document useful (0 votes)
256 views120 pages

C2 1page Per Sheet

This document discusses the application layer and application layer protocols. It describes client-server and peer-to-peer architectures and provides examples like HTTP, FTP, and email protocols. It also covers sockets and how processes communicate over the network.

Uploaded by

tiendatctt3
Copyright
© Attribution Non-Commercial (BY-NC)
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)
256 views120 pages

C2 1page Per Sheet

This document discusses the application layer and application layer protocols. It describes client-server and peer-to-peer architectures and provides examples like HTTP, FTP, and email protocols. It also covers sockets and how processes communicate over the network.

Uploaded by

tiendatctt3
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 120

Chapter 2 Application Layer

A note on the use of these ppt slides:


Were making these slides freely available to all (faculty, students, readers). Theyre in PowerPoint form so you can add, modify, and delete slides (including this one) and slide content to suit your needs. They obviously represent a lot of work on our part. In return for use, we only ask the following: If you use these slides (e.g., in a class) in substantially unaltered form, that you mention their source (after all, wed like people to use our book!) If you post any slides in substantially unaltered form on a www site, that you note that they are adapted from (or perhaps identical to) our slides, and note our copyright of this material. Thanks and enjoy! JFK/KWR All material copyright 1996-2009 J.F Kurose and K.W. Ross, All Rights Reserved

Computer Networking: A Top Down Approach,


5th edition. Jim Kurose, Keith Ross Addison-Wesley, April 2009.

2: Application Layer

2: Application Layer

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

Chapter 2: Application Layer


Our goals: conceptual, implementation aspects of network application protocols transport-layer service models client-server paradigm peer-to-peer paradigm learn about protocols by examining popular application-level protocols
HTTP FTP SMTP / POP3 / IMAP DNS

programming network applications socket API

2: Application Layer

Some network apps


e-mail web instant messaging remote login P2P file sharing multi-user network games streaming stored video clips social networks voice over IP real-time video conferencing grid computing

2: Application Layer

To ra mt ng dng mng, ngha l:


vit chng trnh sao cho:
C th chy trn cc host khc nhau. C th truyn thng vi nhau qua mng e.g., web server software communicates with browser software
application transport network data link physical

khng cn phi vit cc phn mm chy trn thit b mng (router, switch, )
Cc thit b mng ko chy cc apps do ngui dng vit Li ny cho php pht trin nhanh cc apps chy trn cc host.

application transport network data link physical

application transport network data link physical

2: Application Layer

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

Application architectures
Kin trc ng dng l yu t phi quyt nh trc tin khi xy dng ng dng mng
Client-server
Including data centers / cloud computing

Peer-to-peer (P2P) Hybrid of client-server and P2P

Kin trc ng dng kin trc mng (VD Internet c kin trc phn tng!)

2: Application Layer

Client-server architecture
server:
always-on host a ch IP c nh S dng server farms khi ti ln

clients:
client/server Truyn thng vi server C th kt ni khng lin tc C th s dng a ch IP ng Khng truyn thng trc tip vi nhau.
2: Application Layer 9

Google Data Centers


Chi ph c tnh ca data center: $600M Google tiu $2.4B trong 2007 cho 1 data center mi Mi data center c cng sut 50-100 megawatts in

Pure P2P architecture


Ko dng server Cc host truyn thng trc tip vi nhau peer-peer Cc peer c th kt ni ko lin tc v thay i a ch IP VD: Gnutella ng dng ngun m cho php chia s file C tnh m rng cao nhng kh qun l.
2: Application Layer 11

Hybrid of client-server and P2P


Skype L ng dng truyn m trn mng IP (VoIP) Server trung tm:tm a ch ca bn th 3 Kt ni client-client l trc tip, ko qua Server Instant messaging Dng tn gu gia 2 user trn mng Dch v centralized service: pht hin/nh v s hin din/v tr ca client User ng k a ch IP ca n vi server trung tm mi khi online User lin lc vi server trung tm tm a ch IP ca bn chat.

2: Application Layer

12

Processes communicating
Tin trnh (process): chng trnh chy trn 1 host. Trn cng host, 2 tin trnh truyn thng vi nhau s dng interprocess communication (c h iu hnh nh ngha ). Cc tin trnh trn cc host khc nhau truyn thng nhau bng trao i thng ip (messages) Tin trnh client : tin trnh khi to truyn thng Tin trnh server: tin trnh ch i c lin lc

Lu : cc ng dng P2P

c c tin trnh client ln tin trnh server.

2: Application Layer

13

Sockets
Cc tin trnh gi/nhn cc thng ip n/t socket ca n Tin trnh~ ngi nh => socket ~ ca ra vo
Tin trnh y thng ip ra ca. H tng vn chuyn bn ngoi ca chuyn giao thng ip ti socket ca tin trnh bn nhn.
host or server controlled by app developer host or server

process socket TCP with buffers, variables

process socket

Internet

TCP with buffers, variables

controlled by OS

API: (1) choice of transport protocol; (2) ability to fix a few parameters (lots more on this later)
2: Application Layer 14

Addressing processes
nhn thng ip, tin trnh phi c nh danh (identifier) Cc host u c 1 a ch IP di 32 bit, duy nht. Bi tp: dng lnh ipconfig t console, ly a ch IP trn my tnh Windows.

Q: liu a ch IP ca host
m process ang chy trn , c xc nh tin trnh? A: Khng , v c c nhiu tin trnh c th chy trn cng host. nh danh bao gm c a ch IP v s hiu cng (port number) c lin kt vi tin trnh trn host. V d v cc port number:
HTTP server: 80 Mail server: 25
2: Application Layer 15

App-layer protocol defines


Cc kiu thng ip c trao i,
e.g., request, response

Cc giao thc cng hu: c nh ngha RFCs


HTTP: RFC 2616

K php thng ip:


Cc field trn thng ip v m t ca tng field.

Ng ngha thng ip
ngha ca thng tin trn tng field

Cc quy tc v thi im v cch thc cc tin trnh gi v phn hi li cc thng ip.

Cho php lm vic phi hp. e.g., HTTP, SMTP, BitTorrent Cc giao thc t hu: e.g., Skype, ppstream

2: Application Layer

16

What transport service does an app need?


Mt d liu Mt s ng dng (VD audio) c th chp nhn mt d liu. Cc ng dng khc (e.g., file transfer, telnet) i hi phi truyn d liu chnh xc tuyt i Kp thi Mt s ng dng (e.g., Internet telephony, interactive games) i hi tr thp mc chp nhn c. Bng thng Vi ng dng (e.g., multimedia) cn bng thng ti thiu t hiu qu. Cc ng dng khc (elastic apps) c th thch nghi vi bng thng hin c (VD email, truyn file,) An ton M ha, ton vn d liu,
2: Application Layer 17

Transport service requirements of common apps


Lo i ng d ng file transfer e-mail Web documents real-time audio/video stored audio/video interactive games instant messaging M t d li u no loss no loss no loss loss-tolerant loss-tolerant loss-tolerant no loss Bng thng elastic elastic elastic audio: 5kbps-1Mbps video:10kbps-5Mbps same as above few kbps up elastic K p th i no no no yes, 100s msec yes, few secs yes, 100s msec yes and no

2: Application Layer

18

Internet transport protocols services


TCP service:
connection-oriented: cn qu trnh
thit lp kt ni ( handshake) gia cc tin trnh server v client

UDP service:
Vn chuyn thng (ko bo m) gia cc tin trnh gi v nhn. Ko bo m: qu trnh thit lp thng s, vn chuyn m bo, flow control, congestion control, kp thi, bo m bng thng ti thiu hay, an ninh

Vn chuyn bo m (reliable transport) gia 2 tin trnh gi v


nhn
ng ni dung, ko mt gi tin, ko trng gi tin ng th t

flow control: bn gi ko gi qu

nhanh, lm trn d liu bn nhn congestion control: iu tit tc Q: why bother? Why is there gi d liu khi mng qu ti (ko a UDP? hn tt!). Ko bo m: tnh kp thi, bng 2: Application Layer thng ti thiu, an ninh

19

Internet apps: application, transport protocols


ng d ng e-mail remote terminal access Web file transfer streaming multimedia Internet telephony Giao th c ng d ng t ng Giao th c v n chuy n TCP TCP TCP TCP TCP or UDP

SMTP [RFC 2821] Telnet [RFC 854] HTTP [RFC 2616] FTP [RFC 959] HTTP (eg Youtube), RTP [RFC 1889] SIP, RTP, proprietary (e.g., Skype)

typically UDP

2: Application Layer

20

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

21

Web and HTTP


First some jargon Trang Web bao gm cc i tng (objects) i tng c th l file HTML, nh JPEG, Java applet, file m thanh, Hu ht trang Web bao gm file HTML c s (base HTML file) v nhiu i tung c tham chiu Mi mt i tung c nh v bng mt URL VD URL: www.someschool.edu/someDept/pic.gif host name path name
2: Application Layer 22

HTTP overview
HTTP: hypertext transfer protocol
L giao thc tng ng dng ca Web HTTP/1.0 (RFC 1945) HTTP/1.1 (RFC 2616) M hnh client/server client: l trnh duyt (browser) cho php yu cu (request), nhn v hin th cc i tng Web. server: Web server gi cc i tng n client p ng cc yu cu tng ng t client.
PC running Explorer

Server running Apache Web server Mac running Navigator

2: Application Layer

23

HTTP overview (continued)


Uses TCP:
Client khi to kt ni TCP (to socket) server, port 80. Server chp nht kt ni TCP t pha client Trnh duyt v Web Server trao i cc HTTP messages (chnh l cc message ca giao thc tng ng dng) Kt ni TCP c ng.

HTTP is stateless
Server khng lu li bt k thng tin no v cc request t pha client Protocols that maintain state are complex! Thng tin qu kh phi c qun l Nu lin kt server/client b b, thng tin v trng thi ca 2 pha c th ko nht qun, cn c iu chnh.
2: Application Layer

aside

24

HTTP connections
HTTP s dng c 2 cch: nonpersistent connection hoc persistent connection (mc nh)

Nonpersistent HTTP Ti a mt object c gi qua kt ni TCP.

Persistent HTTP Nhiu i tng c th c gi qua 1 kt ni TCP duy nht gia client v server

2: Application Layer

25

Nonpersistent HTTP
Gi s user nhp vo chui URL URL www.someSchool.edu/someDepartment/home.index
1a. HTTP client khi to kt ni TCP
n HTTP server (process) ti www.someSchool.edu trn port 80 (trang ny g m text, tham chi u n 10 nh jpeg)

1b. HTTP server ti host


www.someSchool.edu ch i kt ni TCP connection port 80. chp nhn kt ni, bo cho client

2. HTTP client gi HTTP request


message (cha URL) vo socket
ca kt ni. Message ny ch th rng client mun i tng someDepartment/home.index

3. HTTP server nhn c request message, hnh thnh nn response message cha
cc i tng c yu cu, v gi message no vo socket ca n.
2: Application Layer 26

time

Nonpersistent HTTP (cont.)


4. HTTP server ng kt ni TCP 5. HTTP client nhn c responee
message cha file HTML , hin th HTML. Phn tch HTML file, tm ra 10 nh jpeg

time

6. Bc 1-5 lp li cho mi i tng nh jpeg.

2: Application Layer

27

Non-Persistent HTTP: Response time


inh ngha RTT(Round trip time): l thi gian 1 gi tin nh i t client n server v quay tr v. Response time: Cn 1 RTT khi to kt ni TCP t client > server Cn 1 RTT client gi request HTTP v vi byte ca HTTP respons tr v Thi gian truyn file total = 2RTT+transmit time

initiate TCP connection RTT request file RTT file received time

time to transmit file

time

2: Application Layer

28

Persistent HTTP
Nonpersistent HTTP issues: Cn 2 RTT cho mi i tng Tn chi ph x l ca HH cho tng kt ni TCP Trnh duyt thng phi m nhiu kt ni TCP ng thi ti cc i tng Web Persistent HTTP Server vn duy tr kt ni sau khi gi cc phn hi. Cc HTTP message sau gia client v server lm vic trn cng kt ni ang m. Client gi request ngay khi n gp cc i tng. Tn mt RTT cho tt c cc i tng c ti.

2: Application Layer

29

HTTP request message


Hai kiu thng ip HTTP : request, response Thng ip HTTP request:
ASCII (human-readable format) Dng request, g m 1 trong cc l nh (GET, POST, HEAD) Cc dng header

GET /somedir/page.html HTTP/1.1 Host: www.someschool.edu User-agent: Mozilla/4.0 Connection: close Host: ni cha trang web Accept-language:fr User-agent: loi browser pht ra

Xu ng hng, (extra carriage return, V u dng line feed) Ch th cu i thng i p

request Connection: close =>nonpersistent connection Accept language: Fr =>cho bit user mun nhn phin bn Php ng ca i tng
2: Application Layer 30

HTTP request message: general format

2: Application Layer

31

Uploading form input


POST method: Trang Web thng bao gm cc form input Phn d liu ngui dng nhp vo, c gn vo phn thn (entity body) ca thng ip request v gi v cho server

URL method: S dng GET method Phn d liu ngui dng nhp vo, c gn vo trong field URL dng request

www.somesite.com/animalsearch?monkeys&banana

2: Application Layer

32

Method types
HTTP/1.0 GET POST HEAD
Dng cho mc ch debug Yu cu server ko cn gi i tng v client trong thng ip response

HTTP/1.1 GET, POST, HEAD PUT


Upload file c ni dung phn thn (entity body) ca thng ip request, n ni c ch ra URL field.

DELETE
Xa file c ch ra trong URL field

2: Application Layer

33

HTTP response message


Dng tr ng thi (protocol status code status phrase) 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 data data data data data ...

d li u, e.g., requested HTML file

2: Application Layer

34

HTTP response status codes


Nm trn dng u tin ca thng ip phn hi t server Mt vi m trng thi: 200 OK
request succeeded, requested object later in this message

301 Moved Permanently


requested object moved, new location specified later in this message (Location:)

400 Bad Request


request message not understood by server

404 Not Found


requested document not found on this server

505 HTTP Version Not Supported


2: Application Layer 35

Trying out HTTP (client side) for yourself


1. Telnet to your favorite Web server:
telnet cis.poly.edu 80 Opens TCP connection to port 80 (default HTTP server port) at cis.poly.edu. Anything typed in sent to port 80 at cis.poly.edu

2. Type in a GET HTTP request:


GET /~ross/ HTTP/1.1 Host: cis.poly.edu By typing this in (hit carriage return twice), you send this minimal (but complete) GET request to HTTP server

3. Look at response message sent by HTTP server!


2: Application Layer 36

User-server state: cookies


Many major Web sites use cookies Four components:
1) cookie header line of HTTP response message 2) cookie header line in HTTP request message 3) cookie file kept on users host, managed by users browser 4) back-end database at Web site

Example: Susan lun truy cp Internet t my PC Ln u gh thm site ecommerce, c sd cookies Khi e-commnerce server ln u nhn c 1 HTTP requests, n to ra: ID duy nht cho Susan t my PC. 1 dng trong backend database ng vi ID
2: Application Layer 37

Cookies: keeping state (cont.)


client
ebay 8734

server
usual http request msg usual http response

cookie file
ebay 8734 amazon 1678

Set-cookie: 1678
usual http request msg

Amazon server creates ID 1678 for user create cookiespecific action cookiespectific action

entry

cookie: 1678
Mt tun sau:
ebay 8734 amazon 1678

access backend database

usual http response msg usual http request msg

access

cookie: 1678
usual http response msg

2: Application Layer

38

Cookies (continued)
What cookies can bring: authorization shopping carts recommendations user session state (Web e-mail) Cookies and privacy: cookies permit sites to learn a lot about you you may supply name and e-mail to sites
aside

How to keep state: protocol endpoints: maintain state at sender/receiver over multiple transactions cookies: http messages carry state
2: Application Layer 39

Web caches (proxy server)


Goal: satisfy client request without involving origin server User thit lp trnh duyt: truy cp Web thng qua cache Trnh duyt gi tt c cc HTTP requests ti cache
Nu c i tng trong cache: cache tr v i tng cho trnh duyt Ngc li, cache request i tng t server gc, sau mi tr v cho trnh duyt.
origin server

Proxy server
client

client

origin server
2: Application Layer 40

More about Web caching


Cache hnh x nh client v nh server Thng thng, cache c ISP (trng H, cng ty, cc ISP a phng) thit lp. Why Web caching? Gim thi gian p ng cho cc request t client Gim lu lng ng truy cp ra ngoi ca t chc Internet dense with caches: enables poor content providers to effectively deliver content (but so does P2P file sharing)
2: Application Layer 41

Caching example
Gi nh
Kch thc bnh qun ca 1 i tng web = 1,000,000 bits public Internet S lng request trung bnh t trnh duyt ca t chc gi n origin servers = 15 ln/giy tr tnh t router ca t chc, i 15 Mbps access link n origin server v quay v li router = 2 giy institutional origin servers

H qu
Mc s dng Web trn LAN = 15% Mc s dng Web trn access link = 100% tr tng cng = Internet delay + access delay + LAN delay = 2 sec + minutes + milliseconds

network

100 Mbps LAN

institutional cache
2: Application Layer 42

Caching example (cont)


Gii php 1
Tng bng thng access link (ng truyn t router ca t chc ra router ca Internet) ln 100 Mbps
public Internet

origin servers

consequence
Mc s dng Web trn LAN = 15% = (15 ln/sec * 1000000 institutional bits/ln)/100Mbps=15% network Mc s dng Web trn access link= 15% tr tng cng= Internet delay + access delay + LAN delay = 2 sec + msecs + msecs Chi ph nng cp ng truyn rt t

100 Mbps access link

100 Mbps LAN

institutional cache
2: Application Layer 43

Caching example (cont)


Gii php kh thi: dng cache
Gi s t l p ng ca cache (hit rate) l 0.4 origin servers
public Internet

H qu
C 40% request s c p ng gn nh tc th. C 60% request s c origin server p ng. Mc s dng Web trn access link gim xung cn 60%, dn n tr khng ng k ( khong 10 msec trn ng truyn 15 Mbps) tr tng cng = Internet delay + access delay + LAN delay = .6*(2.01) secs + .4*milliseconds < 1.4 secs

15 Mbps access link institutional network

100 Mbps LAN

institutional cache
2: Application Layer 44

Mc ch: khng server gi i tng v cache nu nh cache c phin bn c cache cp nht ca i tng HTTP request msg If-modified-since: Pha cache: khi gi request <date> n server, cache ch r ngy cp nht sau cng ca i HTTP response tng ang c lu cache
If-modified-since: <date>
HTTP/1.0 304 Not Modified

Conditional GET

server
i tng khng thay i

V cache y/cu server ch gi i tng nu nh i tng c thay i. Pha server: khng cn gi i tng nu nh bit i tng ko thay i
HTTP/1.0 304 Not Modified

HTTP request msg


If-modified-since: <date>

i tng thay i

HTTP response
HTTP/1.0 200 OK

<data>
2: Application Layer 45

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

46

FTP: the file transfer protocol


FTP FTP user client interface user at host local file system file transfer FTP server remote file system

Truyn file n/t mt host xa. M hnh Client/server client: bn khi to vic truyn/nhn server: host xa ftp: RFC 959 ftp server: port 21
2: Application Layer 47

FTP: separate control, data connections


FTP client lin lc vi FTP Server port 21, s dng giao thc TCP. Client truy cp n Server qua kt ni iu khin ( gi l control conection)
Client c th lm nhiu vic (VD xem th mc ca Server, ti file, i th mc hin hnh,) bng cch gi lnh qua kt ni iu khin FTP c coi l gi thng tin iu khin ngoi di bng (out of band) TCP control connection port 21

FTP client

TCP data connection port 20

FTP server

Khi server nhn c lnh chuyn file, n s m 1 kt ni th 2 n client (gi l kt ni d liu, data connection), truyn file.

Sau khi truyn xong file, server s ng kt ni d liu ny. Server s m mt kt ni d liu khc truyn file khc FTP server c duy tr trng thi: th mc hin hnh, cc chng thc trc .
2: Application Layer 48

FTP commands, responses


Sample commands:
c gi nh on ASCII text qua kt ni iu khin USER username PASS password LIST tr v danh sch file trong th mc hin hnh RETR filename ti xung file STOR filename t file ln remote host

Sample return codes


status code and phrase (as in HTTP) 331 Username OK, password required 125 data connection already open; transfer starting 425 Cant open data connection 452 Error writing file

2: Application Layer

49

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

50

Electronic Mail
Ba thnh phn chnh:
user agents mail servers simple mail transfer protocol: SMTP User Agent Cn gi l Chng trnh c mail Son message, c message e.g., Eudora, Outlook, elm, Mozilla Thunderbird Cc message n, gi i u c lu tr server
mail server user agent

outgoing message queue user mailbox

user agent

SMTP SMTP SMTP


mail server user agent user agent

mail server

user agent

user agent

2: Application Layer

51

Electronic Mail: mail servers


Mail Servers
mailbox cha cc message n message queue cha cc message c gi i SMTP protocol giao thc gia cc mail server, gi messages client: sending mail server server: receiving mail server
mail server user agent user agent

SMTP SMTP SMTP


mail server user agent user agent

mail server

user agent

user agent

2: Application Layer

52

Electronic Mail: SMTP [RFC 2821]


S dng TCP gi bo m cc message t client n server, dng port 25. Chuyn trc tip : t server-gi n server-nhn Ba giai on gi Bt tay (cho mng, tng ng dng) Chuyn message ng S dng li tng tc: lnh/phn hi Lnh (commands): ASCII text Phn hi (response): m trng thi (status code) v cm t m t

Cc thng ip phi s dng bng m ASCII 7-bit


2: Application Layer 53

Scenario: Alice sends message to Bob


1) Alice s dng 1 user agent son 1 message v gi ti [email protected] 2) User Agent gi message ti mail server; message c xp vo hng i. 3) Mail server pha Alice m mt kt ni TCP connection n mail server pha Bob
1 user agent 2

4) Mail server pha Alice gi message ca Alice qua kt ni TCP n Mail server pha Bob 5) Mail server pha Bob t message vo mailbox ca Bob 6) Bob dng user agent c message.

mail server 3

mail server 4 5 6

user agent

2: Application Layer

54

Sample SMTP interaction


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: <[email protected]> 250 [email protected]... Sender ok RCPT TO: <[email protected]> 250 [email protected] ... 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
2: Application Layer

55

Try SMTP interaction for yourself:


telnet servername 25 see 220 reply from server enter HELO, MAIL FROM, RCPT TO, DATA, QUIT commands above lets you send email without using email client (reader)

2: Application Layer

56

SMTP: final words


SMTP s dng kt ni thng trc (persistent connections) SMTP i hi message (bao gm c header v body) s dng bng m ASCII 7-bit SMTP server s dng CRLF.CRLF xc nh im cui ca message

So snh vi HTTP:
HTTP: ko SMTP: y C hai u s dng li tng tc lnh/phn hi, m trng thi. HTTP: mi i tng c bao bc trong chnh msg phn hi ca chnh n SMTP: nhiu i tng c gi trong cc thng ip a phn (multipart msg)
2: Application Layer 57

Mail message format


SMTP: giao thc trao i message RFC 822: chun nh dng msg dng vn bn: Dng header , e.g.,
To: From: Subject:

header

blank line

body

Khc vi cc lnh SMTP!

Phn thn (body)


Ni dung msg, ch bao gm cc k t ASCII

2: Application Layer

58

Mail access protocols


SMTP
user agent

SMTP

access protocol

user agent

Alice

senders mail server

receivers mail server

Bob

SMTP: chuyn tip/lu tr msg vo server ca bn nhn ti msg t server v, s dng giao thc truy cp mail: POP: Post Office Protocol [RFC 1939] authorization (agent <-->server) v ti msg IMAP: Internet Mail Access Protocol [RFC 1730] Nhiu tnh nng hn (phc tp hn) Thao tc trn cc msg c lu server HTTP: gmail, Hotmail, Yahoo! Mail, etc.
2: Application Layer 59

POP3 protocol POP3 server ready S: +OK


Giai on xc thc (authorization phase)
Cc lnh t client: user: khai bo user pass: mt m Cc phn hi t server +OK -ERR
C: S: C: S: C: S: S: S: C: S: S: C: C: S: S: C: C: S: user bob +OK pass hungry +OK user successfully
logged on

Giai on giao dch (transaction phase), client:


list: lit k s hiu msg retr: Ti msg theo s th t dele: xa quit

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
2: Application Layer 60

POP3 (more) and IMAP


More about POP3 V d trc s dng ch ti v xa mail. Bob ko th c li email nu anh ta thay i client ti v gi mail: chp li cc msg trn cc client khc nhau POP l giao thc phi trng thi (stateless) qua nhiu session IMAP Gi tt c cc message mt ni: server Cho php user t chc cc msg vo cc folders IMAP gi cc user state qua cc session
Tn ca cc folders v nh x gia Msg ID v tn folder

2: Application Layer

61

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

62

DNS: Domain Name System


Ngi: nhiu tn:
CMND, tn, S h chiu

Domain Name System:


CSDL phn b
c trin khai trn mt h thng cc name servers, c t chc phn cp. application-layer protocol : cho php host v name servers giao tip vi nhau nhm phn gii tn (chuyn i a ch IP/hostname) Lu : l chc nng li ca Internet, c ci t nh l mt application-layer protocol complexity at networks edge
2: Application Layer 63

Internet hosts, routers:


a ch IP (32 bit) c s dng nh a ch cho vic truyn datagram tn min, e.g., ww.yahoo.com dnh cho con ngi

Q: nh x gia a ch IP v tn?

DNS
Cc dch v DNS cung cp Cho bit a ch IP khi bit hostname t b danh host
Canonical, alias names

t b danh cho mail server Phn b ti


replicated Web servers: s dng nhiu a ch IP ng vi mt tn chnh thc.

Ti sao ko nn tp trung DNS? Nguy c hng hc ti DNS tp trung lm cho ton h thng Internet b nh hng. Lu lng tp trung ti DNS ln=> p ng chm 1 DNS Server ko th gn tt c cc client => lm gia tng cc kt ni ng di n DNS => tt nghn. Bo tr 1 DNS cho ton cu!
2: Application Layer 64

Distributed, Hierarchical Database


Root DNS Servers

com DNS servers yahoo.com amazon.com DNS servers DNS servers

org DNS servers pbs.org DNS servers

edu DNS servers poly.edu umass.edu DNS serversDNS servers

Client wants IP for www.amazon.com; 1st approx: Client hi root server tm DNS Server ph trch tn min com. Client li hi tip DNS Server ph trch tn min com, tm DNS Server ph trch tn min amazon.com Client li hi DNS Server ph trch tn min amazon.com, ly a ch IP cho www.amazon.com 2: Application Layer

65

Khi cc local name server khng th phn gii tn min (do ko lu trong DB), n s lin lc vi root name server root name server: Lin lc vi cc authoritative name server v yu cu server ny lo dm Recursive query Nhn kt qu phn gii t authoritative name server Tr kt qu phn gii v local name server
a Verisign, Dulles, VA c Cogent, Herndon, VA (also LA) d U Maryland College Park, MD g US DoD Vienna, VA h ARL Aberdeen, MD
j Verisign, ( 21 locations)

DNS: Root name servers

k RIPE London (also 16 other locations) i Autonomica, Stockholm (plus 28 other locations) m WIDE Tokyo (also Seoul, Paris, SF)

e NASA Mt View, CA f Internet Software C. Palo Alto,


CA (and 36 other locations)

13 root name servers worldwide


b USC-ISI Marina del Rey, CA l ICANN Los Angeles, CA

2: Application Layer

66

TLD and Authoritative Servers


Top-level domain (TLD) servers:
Chu trch nhim i vi cc tn com, org, net, edu, v tt c cc tn min quc gia mc 1 nh uk, fr, ca, vn,
Network Solutions qun l cc servers dnh cho TLD com Educause qun l cc servers dnh cho TLD edu

Authoritative DNS servers:


DNS server ca cc t chc, cung cp tt c cc nh x tn-a ch IP ca cc server ca t chc (e.g Web, mail) Cc DNS Server ny c chnh cc t chc hoc nh cung cp dch v qun l.
2: Application Layer 67

Local Name Server


Khng hon ton thuc v H thng DNS c t chc theo cu trc phn cp. Mi ISP (ISP a phng, cng ty, trng i hc) u c.
Cng c gi l default name server

Khi mt host truy vn DNS, cu hi c gi n cho local DNS Server


Lm vic nh 1 my i din (proxy), chuyn tip truy vn DNS n h thng phn cp DNS.

2: Application Layer

68

DNS name root DNS server resolution example


2

Mt host cis.poly.edu mun bit a ch IP address ca gaia.cs.umass.edu iterated query:


Server c hi s tr li l tn ca server cn hi k tip I dont know this name, but ask this server

3 4 5 local DNS server


dns.poly.edu

TLD DNS server

authoritative DNS server dns.cs.umass.edu

requesting host
cis.poly.edu gaia.cs.umass.edu

2: Application Layer

69

DNS name root DNS server resolution example


recursive query:
2 6 TLD DNS server 3

t trch nhim phn 7 gii tn min hon ton ln cc server c hi. local DNS server heavy load?
dns.poly.edu

8
authoritative DNS server dns.cs.umass.edu

requesting host
cis.poly.edu gaia.cs.umass.edu
2: Application Layer 70

DNS: caching and updating records


Mi khi name server c c mapping, n s lu cache li mapping ny Cc mc cache s ht thi hiu (bin mt) sau mt thi gian. TLD Server thng c lu cache ti cc local name servers
Do cc root name server t khi c ving thm.

C ch cp nht/bo tin uc thc hin theo thit k ca IETF


RFC 2136
http://www.ietf.org/html.charters/dnsind-charter.html

2: Application Layer

71

DNS records
DNS: CSDL phn b, lu cc resource records (RR) RR format: (name, Type=A
name l hostname value l IP address
value, type, ttl)

Type=CNAME
name is b danh ca vi tn chnh thc (canonical name) www.ibm.com l b danh ca
servereast.backup2.ibm.com

Type=NS
name l domain (VD foo.com) value l hostname ca authoritative name server dnh cho domain ny

Type=MX

value l tn chnh thc (canonical name) value l tn ca mailserver uc lin kt vi name


2: Application Layer 72

DNS protocol, messages


DNS protocol : quy nh cc query msg v reply msg, c hai c cng nh dng message Phn msg header
identification: 16 bit # for query, reply to query uses same # flags: query or reply recursion desired recursion available reply is authoritative

2: Application Layer

73

DNS protocol, messages


Name, type fields for a query RRs in response to query records for authoritative servers additional helpful info that may be used

2: Application Layer

74

Inserting records into DNS


example: new startup Network Utopia register name networkuptopia.com at DNS registrar (e.g., Network Solutions)
provide names, IP addresses of authoritative name server (primary and secondary) registrar inserts two RRs into com TLD server: (networkutopia.com, dns1.networkutopia.com, NS) (dns1.networkutopia.com, 212.212.212.1, A)

create authoritative server Type A record for www.networkuptopia.com; Type MX record for networkutopia.com How do people get IP address of your Web site?
2: Application Layer 75

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

76

Pure P2P architecture


Khng c server ch phc v. Cc host truyn thng trc tip peer-peer Cc peers kt ni nhau khng lin tc v thay i a ch IP 3 trng hp:
Phn phi file Tm thng tin Case Study: Skype

2: Application Layer

77

File Distribution: Server-Client vs P2P


Cu hi : Truyn 1 file t 1 server n N peer mt
bao lu?
Server u1 d1 u2

us: server upload bandwidth ui: peer i upload bandwidth


d2

us File, size F
bits

di: peer i download bandwidth

dN uN

Network (with abundant bandwidth)

2: Application Layer

78

File distribution time: server-client


Server

Server ln lt gi N bn sao ca file:


ht NF/us giy

F
us dN uN

u1 d1 u2

d2

Client i ti 1 bn sao mt F/di giy

Network (with abundant bandwidth)

Th i gian chuy n file F t i N clients s = dcs = max { NF/us, F/min(di) } d ng cch ti p c n i client/server Tng tuy n tnh theo N (v i N l n) 2: Application Layer
79

File distribution time: P2P


Server

Server phi gi 1 bn sao ca file: ht F/us giy client i ti file ht F/di giy Tng cng c NF bits c ti nht c th: us + ui

F
us dN uN

u1 d1 u2

d2

Network (with abundant bandwidth)

Tc upload (ca ton h thng) nhanh

dP2P = max { F/us, F/min(di) , NF/(us + ui) }


i
2: Application Layer 80

Server-client vs. P2P: example


Client upload rate = u, F/u = 1 hour, us = 10u, dmin us
3.5 P2P Client-Server

Minimum Distribution Time

3 2.5 2 1.5 1 0.5 0 0

10

15

20

25

30

35

N
2: Application Layer 81

File distribution: BitTorrent


P2P file distribution tracker: theo di cc peers ang tham gia vo torrent torrent: nhm cc peers ang trao i cc phn mnh file (chunk of file)

Ly danh sch cc peer trading chunks

peer
2: Application Layer 82

BitTorrent (1)
file c chia thnh cc mnh (chunks)256KB. peer ang tham gia vo torrent: cha c mnh no, nhng s tch ly cc chunks trong sut qu trnh ghi danh vi tracker ( thu danh sch cc peers), kt ni ti mt tp con cc peers (lng ging) Trong khi ang download file, peer cng upload cc chunks (m n c v c yu cu) cho cc peer khc. peers c th tham gia vo torrent v rt lui. Khi peer c c file, n c th ri torrent (ch k) hoc li torrent (v tha) 2: Application Layer

83

Pulling Chunks Sending Chunks: tit-for-tatM1 Ti 1 thi im nht nh, Alice g i cc chunks t i 4 cc peer khc nhau ang c lng gi ng m hi n th i c cc chunks khc nhau. chng ang g i cho Alice nh k, mt peer (VD Alice) cc chunks khc m c s hi cc lng ging v danh cao nh t. sch cc chunks m chng Sau m i 10s, s nh c. gi l i top 4 ny. Peer ny (Alice) gi yu cu Sau m i 30 giy: ch n ng u xin cc chunks m n cn nhin peer khc, g i chunk thiu. cho peer ny. u tin xin chunks no m peer m i c ch n c t lng ging c nht th tham gia vo top 4 (rarest first) optimistically unchoke
2: Application Layer 84

BitTorrent (2)

Slide 84 M1 n ming tr ming


MVC, 5/1/2010

BitTorrent: Tit-for-tat
(1) Alice chn ngu nhin c Bob v gi chunks cho Bob (2) Alice tr thnh mt trong 4 peer gi chunks cho Bob nhiu nht; Bob gi chunks cho Alice p tr. (3) Bob tr thnh mt trong 4 peer gi chunk cho Alice nhiu nht.

Cng upload vi tc cao, cng c th tm thy cc peer trao i chunks d hn & nhn c file nhanh hn.
2: Application Layer 85

Distributed Hash Table (DHT)


DHT = CSDL phn tn trn mi trng P2P [distributed P2P database] CSDL cha cc cp (key, value);
key: s CMND; value: Tn ngi key: tn ni dung; value: a ch IP (ni lu ni dung)

Peers truy vn CSDL bng cch cung cp tr key


CSDL tr v gi tr ca value khp vi tr key

Peers cn c th chn [insert] cc cp (key, value) mi vo CSDL.

DHT Identifiers
Mi peer c gn mt s id nh danh, l s nguyn nm trong vng [0,2n-1] (n nguyn c nh)
Mi id c th c biu din qua chui n bits.

i hi mi tr key l mt s nguyn thuc cng vng [0,2n-1] thu c tr key nguyn nh i hi trn, s dng hm bm h, h nhn tr key gc v tr v tr nguyn trong vng [0,2n-1]
eg, key = h(Led Zeppelin IV) y l l do ti sao ta gi n l DHT (distributed hash table)

Lu cp (key, value) peer no?


Vn : Lu tr cp (key, value) peer no? Quy tc: Lu ti peer c id=key. Nu khng c peer nh vy th lu ti peer c id gn nht. Quy c trong bi ging: id gn nht l id nm lin sau trong danh sch cc id. VD: n=4; peers: 1,3,4,5,8,10,12,14;
key = 13, th peer lin sau l 14 key = 15, th peer lin sau l 1

Circular DHT (1)


1 15 3 4 12 10 8 5

Mi peer ch bit peer lin trc v lin sau. Overlay network

Circle DHT (2)


O(N) messages on avg to resolve query, when there are N peers

0001
L ti

Ai chu trch nhim cho key 1110 ?

0011
1110

1111
1110 1110

0100
1110 1110

1100 0101
Define closest as closest successor
1110

1010

1000

Circular DHT with Shortcuts


1 (1111) 15 4 12 10 8 (1000) 5 (0011) 3
Ai chu trch nhim cho key 14 (1110) ?

Mi peer qun l a ch IP ca peer lin trc, peer lin sau v cc short cuts. Gim t 6 cn 2 messages. C th thit k shortcuts sao cho s lng ging v s message khi truy vn u bng O(log N)

Peer Churn
1 15 3 4 12 10 5
x l trnh tr ng cc peer n v i m khng bo tr c (peer churn), m i peer c n ph i bi t cc a ch IP c a 2 peer li n sau c a n. M i peer s ping nh k 2 peer li n sau c a n xc nh l chng cn hi n h u hay rt lui.

8 Peer 5 t ngt ri khi vng. Peer 4 pht hin ra iu ny; nhn peer 8 lm peer lin sau (th nht); v hi peer 8 peer lin sau ca 8?; sau xem peer lin sau ca 8 l peer k lin sau (th hai)ca n. Nu peer 13 mun gia nhp vng th sao?

P2P Case study: Skype


Skype clients (SC)

Bn cht P2P: cc cp user c th truyn thng trc tip. Skype Giao thc tng ng dng login server s hu ca Skype. T chc phn cp, s dng cc Supernode nh x gia username v a ch IP, c lu phn b gia cc Supernode.

Supernode (SN)

2: Application Layer

93

Peers as relays
Vn gp phi khi c 2 peer (VD Alice v Bob) u nm ng sau NAT
NAT ngn chn cc peer t bn ngoi khi to cc kt ni (hay cuc gi) n cc peer bn trong

Gii php:
S dng cc supernode ca Alice v Bob, 1 supernode c chn ra lm relay node (nt chuyn tip) Tng peer s khi to kt ni vi nt chuyn tip. Do cc peer c th truyn thng cho nhau vt qua NAT nh nt chuyn tip.
2: Application Layer 94

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

95

Socket programming
Mc tiu: hc cch xy dng ng dng client/server truyn thng vi nhau s dng sockets Socket API
c gii thiu trong BSD4.1 UNIX, 1981 c cc ng dng to ra, s dng v gii phng mt cch tng minh. client/server paradigm C 2 loi dch v vn chuyn thng qua socket API
UDP TCP

socket
A application-created, OS-controlled interface (a door) into which application process can both send and receive messages to/from another application process

2: Application Layer

96

Socket programming basics


Server phi ang chy trc khi client c th gi d liu cho n. Server phi c 1 socket (ca) m nh n c th nhn/ gi i cc segment Tng t, client cng cn c 1 socket Socket c th xc nh trong ni b nh s hiu cng (port number)
Tng t nh s hiu cn h trong 1 ta nh

Client cn phi bit a ch IP ca Server v s hiu cng ca socket.

2: Application Layer

97

Socket programming with UDP


UDP: khng c kt ni gia client v server Khng bt tay Bn gi gn a ch IP v s hiu cng ln tng segment Server c th trch xut a ch IP v s hiu cng ca bn gi t segment nhn c.

Quan im ng dng

UDP cung cp dch v vn chuyn datagram khng m bo tin cy gia client and server

Lu : Thut ng chnh thc gi gi tin UDP l datagram. Trong lp ny chng ta s gi l UDP segment
2: Application Layer 98

Running example
Client:
Ngi s dng g vo mt dng vn bn Chng trnh client gi dng ny n server

Server:
Server nhn c dng vn bn Server chuyn thnh ch hoa tt c cc k t Gi dng vn bn c sa i n client

Client:
Nhn dng vn bn Hin th
2: Application Layer 99

Client/server socket interaction: UDP


Server (running on hostid)
create socket, port= x. serverSocket = DatagramSocket()

Client
create socket, clientSocket = DatagramSocket()

c datagram t serverSocket So n tr l i cho client (v i a ch IP client v s hi u c ng), g i thng qua serverSocket

T o datagram v i server IP v port=x; g i datagram thng qua clientSocket

c datagram t clientSocket ng clientSocket

2: Application Layer

100

Example: Java client (UDP)


keyboard monitor input stream inFromUser

Process
Output: sends

Client process

Input: receives

UDP packet

receivePacket

packet (recall that TCP sent byte stream)

sendPacket

packet (recall thatTCP received byte stream)


UDP packet

client UDP clientSocket socket


to network

UDP socket

from network

2: Application Layer

101

Example: Java client (UDP)


import java.io.*; import java.net.*; 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"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes();
2: Application Layer 102

Create input stream Create client socket Translate hostname to IP address using DNS

Example: Java client (UDP), cont.


Create datagram with data-to-send, length, IP addr, port Send datagram to server Read datagram from server
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } }
2: Application Layer 103

Example: Java server (UDP)


import java.io.*; import java.net.*; 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) {

Create datagram socket at port 9876

Create space for received datagram Receive datagram

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket);


2: Application Layer 104

Example: Java server (UDP), cont


String sentence = new String(receivePacket.getData());

Get IP addr port #, of sender

InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes();

Create datagram to send to client Write out datagram to socket


} }

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); }

End of while loop, loop back and wait for another datagram

2: Application Layer

105

UDP observations & questions


C client v server u dng DatagramSocket a ch IP bn nhn v s hiu cng c gn tng minh vo segment. iu g s xy ra nu thay i c hai clientSocket , serverSocket thnh mySocket? Liu client c th gi 1 segment n 1 server m khng bit n a ch IP ca server v/hoc port number? C th no nhiu client s dng server ny?
2: Application Layer

106

Chapter 2: Application layer


2.1 Principles of network applications 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP

2.6 P2P applications 2.7 Socket programming with UDP 2.8 Socket programming with TCP

2.5 DNS

2: Application Layer

107

Socket-programming using TCP


TCP service: reliable transfer of bytes from one process to another

controlled by application developer controlled by operating system

process socket TCP with buffers, variables

process socket TCP with buffers, variables

controlled by application developer controlled by operating system

internet

host or server

host or server
2: Application Layer 108

Socket programming with TCP


Client phi lin lc vi server Trc tin, tin trnh server phi ang chy. Server phi c socket c to sn, ch n cc lin lc t pha client. Client lin lc vi server bng cch: To client-local TCP socket Ch nh a ch IP, port number ca server process Khi client to socket: client TCP thit lp kt ni n server TCP Khi c client lin lc, server TCP to mi mt socket server process lin lc vi client Cho php server trao i vi nhiu client source port numbers c dng phn bit client (ni r trong Ch. 3) Quan im ng dng

TCP provides reliable, in-order transfer of bytes (pipe) between client and server
2: Application Layer 109

Client/server socket interaction: TCP


Server (running on hostid)
create socket, port=x, for incoming request: welcomeSocket = ServerSocket() wait for incoming connection request connection connectionSocket = welcomeSocket.accept() read request from connectionSocket write reply to connectionSocket close connectionSocket

Client

TCP

setup

create socket, connect to hostid, port=x clientSocket = Socket() send request using clientSocket

read reply from clientSocket close clientSocket 2: Application Layer 110

Stream jargon
keyboard monitor

output stream

inFromServer

outToServer

Stream l dy cc k t i vo hoc ra khi mt process Mt input stream c lin kt vi mt vi ngun nhp liu dnh cho process, VD bn phm hoc socket. Mt output stream c lin kt vi mt u ra, VD mn hnh hoc socket.

input stream

Client Process process

inFromUser

input stream

client TCP clientSocket socket


to network

TCP socket

from network

2: Application Layer

111

Socket programming with TCP


VD v /dng client-server:
1) Client c dng t standard input (inFromUser stream), gi n server thng qua socket (outToServer stream) 2) Server c dng t socket 3) Server chuyn i k t trong dng thnh ch hoa, gi ngc li client. 4) Client c, in ra dng b sa i t socket (inFromServer stream)

2: Application Layer

112

Example: Java client (TCP)


import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; Create input stream Create client socket, connect to server Create output stream attached to socket BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
2: Application Layer 113

Example: Java client (TCP), cont.


Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); Send line to server Read line from server outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } }
2: Application Layer 114

Example: Java server (TCP)


import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket

2: Application Layer

115

Example: Java server (TCP), cont


Create output stream, attached to socket Read in line from socket DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; Write out line to socket } } End of while loop, loop back and wait for another client connection outToClient.writeBytes(capitalizedSentence); }

2: Application Layer

116

TCP observations & questions


Server c 2 kiu socket:
ServerSocket v Socket

Khi client g vo ca serverSocket, server to ra connectionSocket v hon tt TCP conx. a ch IP ch v s hiu cng khng c gn vo segment tng minh. Liu nhiu clients c th s dng server?.

2: Application Layer

117

Chapter 2: Summary
our study of network apps now complete!
application architectures
client-server P2P hybrid

specific protocols:
HTTP FTP SMTP, POP, IMAP DNS P2P: BitTorrent, Skype

application service requirements:


reliability, bandwidth, delay

socket programming

Internet transport service model


connection-oriented, reliable: TCP unreliable, datagrams: UDP
2: Application Layer 118

Chapter 2: Summary
Most importantly: learned about protocols
typical request/reply message exchange:
client requests info or service server responds with data, status code

Important themes:
control vs. data msgs in-band, out-of-band centralized vs. decentralized stateless vs. stateful reliable vs. unreliable msg transfer complexity at network edge
2: Application Layer 119

message formats:
headers: fields giving info about data data: info being communicated

You might also like