ươ

ế

ng 2 (ti p). ứ ầ ứ

Ch ụ Giao th c t ng  ng d ng (application layer)

ị Đoàn Th  Thu Hà

Discussion

 Web programming with ASP.

 Visual Basic programming.

 Object oriented programming.

 Event driven programming.

 Component driven programming.

 TCP Socket programming.

 Simple web server.

Chapter 2. The Application Layer 2 1-6/2005

Ch2. The Application Layer

ộ ố

ệ 2.1. M t s  khái ni m và nguyên t c.

2.2. Web & Hyper Text Transfer Protocol.

2.3. Web design and HTTP, Web programming.

2.4. File Transfer Protocol.

2.5. Electronic Mail Protocols.

2.6. Domain Name System.

Chapter 2. The Application Layer 3 1-6/2005

2.4. FTP ­ File Transfer Protocol

file transfer

FTP client

FTP server

FTP user interface

user  at host

remote file system

local file system

ề ả ệ

 Truy n/t

i t p (to/from remote host).

 Client/server model

ề ả

ư

 Client: đ a ra yêu c u truy n t

i.

 Server = remote host.

 FTP: RFC 959

Chapter 2. The Application Layer 4 1-6/2005

FTP: control & data connections

TCP control connection port 21

TCP data connection port 20

FTP client

FTP server

 FTP s  d ng TCP. ử ụ

ử ụ

ế

 FTP s  d ng đ ng th i 2 liên k t TCP t

ổ i 2 c ng:

 TCP control connection, port 21: trao đ i các thông đi p đi u

khi n (commands, responses…).

ề ả ệ

 TCP data connection, port 20: truy n t

i t p.

ư

ữ ạ

 FTP l u gi

tr ng thái client trong phiên làm vi c (state

vs. HTTP is stateless).

Chapter 2. The Application Layer 5 1-6/2005

FTP: Quá trình trao đ i/truy n t

ề ả i

TCP control connection port 21

client

server

TCP data connection port 20

ạ ổ

i c ng 21.

ạ ổ

 FTP server nghe t

i c ng 21.

ầ G i user & password đ  đăng nh p.

ể ể ắ ầ

ế ậ

 FTP client yêu c u k t n i v i FTP server qua TCP t ế ố ớ

ượ đ

c thi

ế ữ ệ

ở c l nh truy n t p, nó m  liên k t d  li u (data

ệ ượ

ế

 FTP server ch p nh n, liên k t đi u khi n (control connection)  ế t l p. Quá trình trao đ i có th  b t đ u.

ậ ượ ệ ớ i client, t p đ

ề ệ ề c truy n qua liên k t này.

ế ữ ệ

 Khi server nh n đ connection) t

ề ỉ ử ụ

ộ ệ ề

ể ế k t ch  s  d ng đ  truy n m t t p).

 Sau khi truy n xong m t t p, server ng t liên k t d  li u (m i liên  ộ ệ

Chapter 2. The Application Layer 6 1-6/2005

FTP commands, responses

Lệnh (commands), phúc đáp (responses) được truyền dạng ASCII

Sample commands:  sent as ASCII text over control

Sample return codes  status code and phrase (as in

channel

HTTP)

current directory

 331 Username OK, password required  125 data connection  USER username  PASS password  LIST return list of file in

 RETR filename retrieves

already open; transfer starting  425 Can’t open data

(gets) file

connection

 452 Error writing

 STOR filename stores  (puts) file onto remote host

file

Chapter 2. The Application Layer 7 1-6/2005

FTP clients & servers

file transfer

FTP client

FTP server

FTP user interface

user  at host

remote file system

local file system

 Microsoft FTP Service  …

 Command­based client:  Windows FTP command.  Linux FTP command.

 GUI clients

 Windows Commander.  CuteFTP, WS_FTP…

Chapter 2. The Application Layer 8 1-6/2005

CuteFTP

Chapter 2. The Application Layer 9 1-6/2005

Ch2. The Application Layer

ộ ố

ệ 2.1. M t s  khái ni m và nguyên t c.

2.2. Web & Hyper Text Transfer Protocol.

2.3. Web design and HTTP, Web programming.

2.4. File Transfer Protocol.

2.5. Electronic Mail Protocols.

2.6. Domain Name System.

Chapter 2. The Application Layer 10 1-6/2005

2.5. Electronic Mail

user agent

mail server

user agent

ầ Ba thành ph n chính:  User agents (mail clients)  So n, đ c th  (messages). ư ọ  Vd: Outlook, Eudora, Netscape

SMTP

Messenger...

mail server

user agent

SMTP

SMTP

user agent

mail server

 Mail servers  L u tr , x  lý th . ư ữ ử ư  Vd: Exchange, MDeamon…

user agent

outgoing  message queue

user agent

 Simple Mail Transfer Protocol (SMTP).  Post Office Protocol (POP).  Internet Mail Access Protocol (IMAP).  HTTP.

user mailbox

 Protocols

Chapter 2. The Application Layer 11 1-6/2005

Alice   Bob 

ư

ủ ế

ư

4) Mail server c a Alice g i  th  qua liên k t TCP.

ử ụ i “to”:

ử ớ g i t

1) Alice s  d ng UA so n th , đ/c  bob@yahoo.com.

ư ủ 5) Mail server c a Bob c t th

ấ ư c vào hòm th

ư ế ứ

c

ậ ượ nh n đ ủ c a Bob.

ử 2) Alice’s UA g i th  đ n mail  ư ượ server c a Alice, b c th  đ ế x p vào hàng đ i (message  queue).

ể ấ

ư ừ

6) Bob dùng UA đ  l y th  t ư ề ồ ọ server v  r i đ c th .

ế

3) Mail server c a Alice m  liên k t

TCP t

i mail server c a Bob.

1

mail server

user agent

2

6

mail server 3

4

user agent Outlook

5 yahoo.com

Yahoo! Mail hua.edu.vn

Chapter 2. The Application Layer 12 1-6/2005

SMTP (Simple Mail Transfer Protocol)

ế

ể ử

 S  d ng liên k t TCP (port 25) đ  g i mails:

ử ụ T  sender’s mail client t ớ ừ i sender’s mail server. T  sender’s mail server t ớ ừ i receiver’s mail server.

 Ba pha:

B t tay (handshaking/greeting). Trao đ i messages. K t thúc. ế

 Command/response: commands: ASCII. responses: status code & phrase.

Chapter 2. The Application Layer 13 1-6/2005

SMTP: sample interaction (C: client; S: server)

S: 220 yahoo.com C: HELO hau.edu.vn S: 250 Hello hau.edu.vn, pleased to meet you C: MAIL FROM: S: 250 alice@hau.edu.vn... Sender ok C: RCPT TO: S: 250 bob@yahoo.com ... Recipient ok C: DATA S: 354 Enter mail, end with "." on a line by itself C: Hi Bob! C: Would you like to play tennis this evening? C: . S: 250 Message accepted for delivery C: QUIT S: 221 yahoo.com closing connection

Chapter 2. The Application Layer 14 1-6/2005

SMTP: more

ử ụ

ế

đi p.ệ

ả ượ

ư

 SMTP s  d ng persistent connection: m t liên k t, nhi u thông

ạ c mã hoá d ng ASCII 7

bit (US­ASCII).

ở CRLF.CRLF

ể ứ CRLF.CRLF  c n ầ

 Thông đi p (k  c  n i dung th ) ph i đ ể ả ộ

ộ ố ữ ệ ể ả

ữ ệ

ị ả

ế

mã hoá đ  đ m b o không có d  li u nào trùng v i ký hi u k t  thúc message (vd: base64).

 Đánh d u k t thúc msg b i  ế  M t s  d  li u nh  phân (vd: picture) có th  ch a  ớ

ử ụ

 HTTP: pull protocol <> SMTP: push protocol.  Cùng s  d ng ASCII (command/req/resp/status code).  HTTP: m i obj ch a trong m t msg <> SMTP: cho phép nhi u obj (text,

ứ ộ

picture…) trong m t msg (multipart msg). Chapter 2. The Application Layer

 So sánh v i HTTP:

15 1-6/2005

Mail message format

SMTP: giao th c trao đ i email msg

(exchange msgs).

header

RFC 822: standard for text message

format:

blank line

body

 To:  From:  Subject:

Khác v i ớ smtp commands (MAIL FROM,  RCPT TO,…)!

 Header lines, vd:

 The “message”.  ASCII

 Body

Chapter 2. The Application Layer 16 1-6/2005

MIME (Multipurpose Internet Mail Extension)

 RFC 822: text msg, g p khó khăn v i multimedia.  MIME: RFC 2045, RFC 2056.  So v i RFC 822, MIME thêm vào các header lines đ  mô

ớ ả ộ

t

ủ  n i dung c a email msg.

MIME version

method used to encode data

multimedia data type, subtype,  parameter declaration

From: alice@hau1.edu.vn To: bob@yahoo.com Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: image/jpeg

encoded data

base64 encoded data ..... ......................... ......base64 encoded data

Chapter 2. The Application Layer 17 1-6/2005

MIME: Content types

Content-type: type/subtype; parameters

Chapter 2. The Application Layer 18 1-6/2005

MIME: Encoding types

Content-Transfer-Encoding: Encoding type

Chapter 2. The Application Layer 19 1-6/2005

MIME: Multiple objects mail message

From: alice@hau.edu.vn To: bob@yahoo.com Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=StartOfNextPart --StartOfNextPart Content-Transfer-Encoding: quoted-printable Content-Type: text/plain

Dear Bob, Please find a picture of a crepe. --StartOfNextPart Content-Transfer-Encoding: base64 Content-Type: image/jpeg

base64 encoded data ..... ......................... ......base64 encoded data --StartOfNextPart

Chapter 2. The Application Layer 20 1-6/2005

The received mail message

ể ượ

 Received:   Return­path:  Mail có th  đ

c chuy n qua nhi u server khác nhau (forward).

 Server thêm vào dòng header m i khi nh n th : ư

Received: from hamburger.edu by sushi.jp; 12 Oct 98 15:30:01 GMT  Received: from crepes.fr by hamburger.edu; 12 Oct 98 15:27:39 GMT  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

Chapter 2. The Application Layer 21 1-6/2005

Hi n th  full headers v i Y! Mail

2.General Preference

1.Mail Options

3.Messages: Show all…

Chapter 2. The Application Layer 22 1-6/2005

Mail access protocol

SMTP

SMTP

user agent

access protocol

user agent

receiver’s mail  server

sender’s mail  server

ứ ử

ớ i server. ứ

 SMTP: giao th c g i mail t  Mail access protocol: giao th c nh n mail t

server.

 POP (Post Office Protocol): RFC 1939. TCP Port 110.  IMAP (Internet Mail Access Protocol): RFC 1730. TCP port 143.  HTTP: Hotmail, Yahoo!...

Chapter 2. The Application Layer 23 1-6/2005

POP3 protocol

[1] Authorization phase  client commands:

 user: declare username  pass: password

 server responses  +OK  -ERR

[1]

[2] Transaction phase, client:  list: list message numbers  retr: retrieve message by number  dele: delete

ượ ậ

[3] Update phase:  Quit  nh ng thay đ i (dele) đ

c c p

ữ nh tậ

[2]

[3]

S: +OK POP3 server ready C: user bob S: +OK C: pass hungry S: +OK user successfully logged on C: list S: 1 498 S: 2 912 S: . C: retr 1 S: S: . C: dele 1 C: retr 2 S: S: . C: dele 2 C: quit S: +OK POP3 server signing off

Chapter 2. The Application Layer 24 1-6/2005

POP3 (more) and IMAP

ế ộ

IMAP4  L u tr  toàn b  msg t

i

More about POP3  Ch  đ  “download and  c):

ư server.

i dùng t

ầ ữ

ướ delete” (xem vd tr  Msg b  xoá khi thoát. ị  Không th  nh n msg l n n a. ậ ể

 Cho phép ng ứ ư ữ ư ụ

ữ ạ  l

c gi

ườ ạ ch c l u tr  trên server (t o  th  m c, di chuy n msg…)  L u tr ng thái gi a các phiên

ề ầ ừ

 Msg đ  Có th  nh n msg nhi u l n t ậ ể nhi u client.

ư ụ ự ạ

t o.

ư

ạ ư (state):  Th  m c t  Các msgs đã di chuy n.ể

 “Download­and­keep”: ượ i server.

các phiên làm vi c (stateless).

 Không l u tr ng thái gi a

Chapter 2. The Application Layer 25 1-6/2005

Ch2. The Application Layer

ộ ố

ệ 2.1. M t s  khái ni m và nguyên t c.

2.2. Web & Hyper Text Transfer Protocol.

2.3. Web design and HTTP, Web programming.

2.4. File Transfer Protocol.

2.5. Electronic Mail Protocols.

2.6. Domain Name System.

Chapter 2. The Application Layer 26 1-6/2005

2.6. DNS (Domain Name System)

Mr Binh HTTP (Internet Explorer)

From: Đ.V.Bình To: Phòng ĐT

www.hau.edu.vn

www.hau.edu.vn => 203.162.8.82

203.162.8.82

Phòng ĐT => H.101

DNS client DNS servers

ượ ử ụ

Ms Nam

c s  d ng đ  xác đ nh m t nút trong m ng

ử ụ

TCP/IP: 203.162.8.82  khó nh !ớ  S  d ng tên (host name) song song v i IP address.

ớ  DNS (RFC 1034, RFC 1035, RFC 2136):

 Chuy n host name

 IP address.

 Đ a ch  IP (32 bit) đ

Chapter 2. The Application Layer 27 1-6/2005

2.6. DNS (Domain Name System)

Chapter 2. The Application Layer 28 1-6/2005

DNS services

ự ế ớ

ng tác tr c ti p v i users mà cung  ng trình khác (HTTP,

ươ DNS không t ươ ụ ấ c p các d ch v  cho các ch ụ ộ ầ ứ FTP…) thu c t ng  ng d ng:

 Host name  IP (hostname to IP resolve).  Host aliasing. ộ

 M t máy tính có tên chính th c và có th  có bí danh (alias). ứ

 Mail server aliasing.

 @hau1.edu.vn  mail server address = ?

i)ả

c cung  ượ

i do l

ng

 Load distribution (phân tán t  M t d ch v  web (cnn.com, yahoo.com…) có th  đ ụ ể ượ ả ề i nhi u server có IP khác nhau đ  phân tán t ườ ử ụ i s  d ng cao.

ộ ị ấ ạ c p t ng

Chapter 2. The Application Layer 29 1-6/2005

Domain name servers

Root DNS Servers

poly.edu DNS servers

umass.edu DNS servers

pbs.org DNS servers

yahoo.com DNS servers

amazon.com DNS servers

t IP c a

ủ www.amazon.com thì: ố ỉ ủ com DNS servers  h i Root DNS servers.

ế Vd: client mu n bi  Tìm đ a ch  c a  ị  amazon.com  com DNS servers.  www.amazon.com  amazon.com DNS servers.

org DNS servers edu DNS servers com DNS servers

Chapter 2. The Application Layer 30 1-6/2005

Internet domain name space

vn

 Host name: www.hau1.edu.vn;

 Domain name (tên mi n):ề

ề ậ

 Mi n = t p h p các nút m ng có  ợ cùng m t nút cha trong s  đ  trên.

 Vd:   hau1.edu.vn; yahoo.com; yale.edu.  Sub­domain: cs.yale.edu;

mail.hau1.edu.vn   vn: Vietnam.  edu: education.  hau: Hanoi Agricultural University.  www: máy ch  world wide web. ủ  mail: máy ch  mail. ủ

ạ ơ ồ ộ

Chapter 2. The Application Layer 31 1-6/2005

Internet domain name space

Chapter 2. The Application Layer 32 1-6/2005

Internet domain name space

Chapter 2. The Application Layer 33 1-6/2005

Internet domain name space

Chapter 2. The Application Layer 34 1-6/2005

Internet domain name space

Chapter 2. The Application Layer 35 1-6/2005

DNS: Root name servers

k RIPE London (also Amsterdam, Frankfurt)

i Autonomica, Stockholm (plus 3 other locations)

a Verisign, Dulles, VA c Cogent, Herndon, VA (also Los Angeles) d U Maryland College Park, MD g US DoD Vienna, VA h ARL Aberdeen, MD j Verisign, ( 11 locations)

m WIDE Tokyo

e NASA Mt View, CA f Internet Software C. Palo Alto, CA (and 17 other locations)

13 root name

servers  worldwide

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

ầ ớ

 Khi local name server không th  chuy n host name

 IP, g i yêu c u t

i root

srv.

ư

ế

ạ ớ authoritative name server n u ch a th  ánh x  đ/c ngay. ạ ử ề ừ authoritative name server. ế ấ ả ế

 root name server:  Liên l c v i   L y k t qu  ánh x  g i v  t ả  Tr  k t qu  v  cho  ả ề

local name server.

Chapter 2. The Application Layer 36 1-6/2005

TLD and Authoritative Servers

 Top­level domain (TLD) servers: ch u trách nhi m v  các

mi n ề com, org, net, edu… và uk, fr, ca, jp…  Network Solutions: qu n lý com TLD servers.  Educause: qu n lý edu TLD servers.

ố ượ

ế ế

ả ượ

 Authoritative DNS servers:   M i host trong m ng mu n đ

c bi

t đ n ph i đ

c khai báo

ạ ỗ ớ v i m t authoritiative DNS server.

 DNS servers c a các t

ạ  ch c ánh x  hostname

 IP cho các

ủ ủ ổ ứ

ổ ứ  ch c đó (vd: web, mail…).

hosts c a t

ể ượ

ổ ứ

 Có th  đ

c qu n lý b i các t

ụ  ch c hay nhà cung c p d ch v .

Chapter 2. The Application Layer 37 1-6/2005

Local name server

 Thu c qu n lý c a ISP (ISP, company, university…).

ế

ướ

t IP c a m t host nào đó, tr

c

 Khi m t host mu n bi ỏ

tiên, nó “h i” local name server.

ế

ả ằ

 Không nh t thi

ế t ph i n m trong ki n trúc DNS servers

ấ ộ ậ (đ c l p).

ườ

 “G n” máy tính c a ng

i s  d ng nh t nên th

ng

ầ ượ

ườ ử ụ ỗ c “h i thăm” đ u tiên m i khi có truy v n DNS.

đ

 Xem Windows Local DNS: ipconfig

Chapter 2. The Application Layer 38 1-6/2005

DNS example

Chapter 2. The Application Layer 39 1-6/2005

DNS example 1

root name server

Tình hu ngố : host surf.eurecom.fr c n đ/c  IP c a ủ gaia.cs.umass.edu

2 4

ạ ớ

1. Liên l c v i local DNS  server: dns.eurecom.fr

3 5

ạ ớ 2. dns.eurecom.fr liên l c v i  root name server (n u c n)

local name server dns.eurecom.fr

Authorititive name server dns.umass.edu

1 6

gaia.cs.umass.edu

ế ầ 3. root name server liên l c ạ v i ớ authoritative name  server: dns.umass.edu (n u ế c n)ầ

requesting host surf.eurecom.fr

Chapter 2. The Application Layer 40 1-6/2005

DNS example 2

root name server

6 2

Root name server có th :ể  Không bi t ế authoritative

name server (ans).

 Bi

t ế intermediate name

3 7

server (ins)  T  ins có th  tìm ra ans ừ

local name server dns.eurecom.fr

intermediate name server dns.umass.edu 5

4 1 8

authoritative name server dns.cs.umass.edu

requesting host surf.eurecom.fr

gaia.cs.umass.edu

Chapter 2. The Application Layer 41 1-6/2005

DNS example 3

root name server

iterated query 2

ượ

i ng

3

4

ị ả

c h i ch u t

i

7

recursive query:  A h i B, B h i C, C h i  ỏ ỏ ồ ả ờ c  D,… r i tr  l i.ạ l  Server đ ượ ỏ ấ ặ n ng g p đôi?

local name server dns.eurecom.fr

5

intermediate name server dns.umass.edu 6

1 8

authoritative name server dns.cs.umass.edu

iterative query:  DNS server đ ộ

ượ ỏ c h i  ợ g i ý h i m t server  khác.

requesting host surf.eurecom.fr

gaia.cs.umass.edu

 “I don’t know this name,  1-6/2005

but ask this server”

Chapter 2. The Application Layer 42

DNS: caching & updating

ậ ượ ượ ẽ ượ ư ạ

ứ ầ c yêu c u tra c u  ụ ụ i ph c v   c l u l

ầ ờ

ượ

 M i khi DNS server nh n đ c s  đ IP, IP mà nó tìm đ ế ứ cho nh ng l n tra c u ti p theo. Timeout: th i gian h t h n (sau m t kho ng th i gian,  ộ ế ạ ị ươ ứ ng  ng b  xoá). ườ c cache t ng đ ậ

b n ghi cache t TLD servers th ơ ế ậ

ạ i local name servers. ượ ư c đ a ra b i

 C  ch  c p nh t/thông báo đ

IETF: RFC 2136  http://www.ietf.org/html.charters/dnsind­charter.html

Chapter 2. The Application Layer 43 1-6/2005

DNS records

DNS: distributed db storing resource records (RR)

RR format: (name, value, type, ttl)

 Type=A

 Type=CNAME

 name = hostname  value = IP address

 name = “bí danh” (alias). www.ibm.com  value = tên th t.ậ

 Type=NS

servereast.backup2.ibm.com

 Type=MX

 name = domain name  value = IP address

c a ủ authoritative name  server cho domain này

 name = domain name  value = tên c a mail server

Chapter 2. The Application Layer 44 1-6/2005

DNS protocol, messages

DNS Protocol: Query/reply messages có cùng format.

ử ụ

 Msg header (12 bytes)  Identification: s  16 bit reply  msg s  d ng cùng id v i req  msg.

ộ m t ý nghĩa:  Query (0) hay reply (1)  Authoritative DNS.  recursion desired: client mong  ứ ệ

mu n DNS server tra c u đ  quy.  recursion available: server h  tr   ỗ ợ

ứ ệ tra c u đ  quy.

 Flags: nhi u bit, m i bit có

Chapter 2. The Application Layer 45 1-6/2005

DNS messages

Name, type fields  for a query

RRs in reponse to query

records for authoritative servers

additional “helpful” info that may be used

Chapter 2. The Application Layer 46 1-6/2005