Chương 2. Giao thức tầng ứng dụng (application layer) Trần Quang Hải Bằng Computer Networking and Internet (course of 2009) Faculty of Information Technology University of Communication and Transport (Unit 2) Office location: Administration building, Block D3, Room 6 Office phone: 38962018 Cell phone: N/A Email:
Application Layer
24/8-25/10/2009
Chapter 2. The Application Layer
2
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.
24/8-25/10/2009
Chapter 2. The Application Layer
3
2.1. Một số khái niệm v nguyờn tc n
Mt s chng trỡnh ng dng (network applications): Ô Ô
Ô Ô Ô Ô Ô Ô Ô
Email (Yahoo! Mail, MS Outlook, Google mail,…). Web browser (MS Internet Explorer, Fire Fox, Netscape Navigator,…). Instant messenger (Yahoo! Messenger, ICQ, AOL,…). P2P file sharing (Napster, KazaA…). Internet games (Yahoo! Games). Internet telephone (Skype, FPTFone,VNNFone…). Streaming stored video clips. Real-time video conference. Massive parallel computing.
24/8-25/10/2009
Chapter 2. The Application Layer
4
Mt s giao thc (application-layer protocols) n
HTTP (Hyper Text Transfer Protocol) Ô
n
FTP (File Transfer Protocol) Ô
n
Giao thc nhn th in t (khỏc POP3)
DNS (Domain Name System) Ô
n
Giao thc nhn th in t.
IMAP (Internet Mail Access Protocol) Ô
n
Giao thc truyn th in t.
POP (Post Office Protocol) Ô
n
Giao thc truyn tp.
SMTP (Simple Mail Transfer Protocol) Ô
n
Giao thc web.
DNS = hệ thống tên miền, giao thức DNS quy định quy tc s dng tờn min.
TFTP (Trivial FTP) Ô
Mt dng khỏc ca FTP.
24/8-25/10/2009
Chapter 2. The Application Layer
5
Net applications vs. application-layer protocols n
Applications: Ô Ô
Ô
n
Chng trỡnh ứng dụng chạy trên các máy tính. Trao đổi thơng điệp (message) với các ứng dụng khác nhằm thực thi vai trò của ứng dng. Vd: web browser, email client
Application-layer protocols: Ô Ô Ô
L mt thnh phn ca chương trình ứng dụng. Định nghĩa các quy tắc trao đổi thơng điệp. Giao tiếp với các dịch vụ được
cung cấp bởi tầng dưới (TCP, UDP).
24/8-25/10/2009
Chapter 2. The Application Layer
6
Các chương trình giao tiếp với nhau ntn? n
Các chương trình trờn cựng mt mỏy tớnh Ô
n
Cỏc chng trỡnh trờn cỏc mỏy tớnh khỏc nhau Ô
n
S dng cỏc dch v c cung cp bởi hệ điều hành (interprocess communication). Giao tiếp thông qua giao thức của tầng ứng dụng (application-layer protocol).
Chương trình phía người dùng = user agent Ô
Ô
Giao tip vi ngi s dng (giao din s dụng). Giao tiếp với các giao thức mạng.
24/8-25/10/2009
7
Chapter 2. The Application Layer
Mơ hình khách-phục vụ (client-server paradigm) Mơ hình khách-phục vụ được quen gọi là mơ hình khách-chủ. Một ứng dụng application cụ thể bao gm hai phn: transport n
Client Ô Ô Ô Ô
n
network data link physical
Thit lp liờn kt vi server. Yờu cu dch v t server. Nhiu clients. Vd: web browser.
request
Server Ô Ô Ô
Ch v cung cp các dịch vụ khi client yêu cầu. Thường chỉ có 1 server, không thay đổi địa chỉ, chạy 24/7. Vd: web server.
24/8-25/10/2009
Chapter 2. The Application Layer
reply application transport network data link physical
8
Mơ hình ngang hàng (Peer-to-peer paradigm) n
n
n
Khơng có máy dịch vụ cố định, các máy tính trong mạng lúc này là server (nhận yêu cầu kết nối) nhưng lúc khác lại là client (yêu cầu máy khác kết nối). Các máy tính là ngang hàng, tự do kết nối và giao tiếp đôi một. Địa chỉ của các máy tính tham gia giao tiếp liên tục thay đổi.
24/8-25/10/2009
Chapter 2. The Application Layer
9
Client-server & P2P examples n
CNN.com
Client-server
n
Yahoo! Mail
Client-server
n
Google
Client-server
n
Yahoo! Messenger
Client-server & P2P
n
Internet Relay Chat (IRC)
Client-server & P2P
24/8-25/10/2009
Chapter 2. The Application Layer
10
Dịch vụ tầng dưới đối với application-layer n
Có thể coi tầng application (Internet) = tầng application + presentation + session trong mô hình OSI. Các dịch vụ với application: §
Presentation: gắn liền với application trong TCP/IP §
Cung cấp dịch vụ vận chuyển cho application (TCP/IP). Hai giao thức: TCP và UDP.
24/8-25/10/2009
Chapter 2. The Application Layer
11
Yêu cầu đối với việc vận chuyn d liu n
Data loss Ô
Ô
n
Timing Ô
n
Cỏc ng dng audio/video cú th chp nhận mất mát, sai lệch dữ
liệu trong khoảng cho phép. Email, file transfer yêu cầu dữ liệu phải chính xác. Các ứng dng online cn tr (delay) nh (phone, games).
Bandwidth Ô
Ô
Cỏc ng dụng multimedia cần đường truyền tốc độ cao để đảm bảo chất lượng. Các ứng dụng email, file transfer mềm dẻo hơn, tuỳ theo tốc độ.
24/8-25/10/2009
Chapter 2. The Application Layer
12
Các yêu cầu giao vận của một số ứng dụng
24/8-25/10/2009
Chapter 2. The Application Layer
13
Internet Transport protocols: TCP & UDP TCP (Transmission Control Protocol)
§
§
§
§
§
connection-oriented: setup required between client, server. reliable transport between sending and receiving process. flow control: sender won’t overwhelm receiver. congestion control: throttle sender when network overloaded. does not providing: timing, minimum bandwidth guarantees.
24/8-25/10/2009
UDP (User Datagram Protocol) §
§
unreliable data transfer between sending and receiving process does not provide: connection setup, reliability, flow control, congestion control, timing, or bandwidth guarantee.
Chapter 2. The Application Layer
14
Internet applications: TCP or UDP?
24/8-25/10/2009
Chapter 2. The Application Layer
15
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 programming. 2.4. File Transfer Protocol. 2.5. Electronic Mail Protocols. 2.6. Domain Name System.
24/8-25/10/2009
Chapter 2. The Application Layer
16
2.2. Web & HTTP n
n
Trang web (web page): một loại tài liệu có chứa trong đó nhiều đối tượng: text, image, audio, Java applet, HTML… Về cơ bản, web page = HTML file (base HTML). Ô Ô
n
HTML = Hyper Text Markup Language. HTML s dng cỏc thẻ (tag) để mơ tả đối tượng chứa trong nó.
Mỗi đối tượng được chỉ ra bởi địa chỉ URL (Uniform Resource Locator) Ô
/> Ô
:80/userimages/tqhbang/nhaphoc.jpg
Protocol
Host name & port
Path
Chapter 2. The Application Layer
24/8-25/10/2009
17
HTTP (Hyper Text Transfer Protocol) n n
Giao thức tầng ứng dụng web. Sử dụng mơ hình client/server. Ô
Client (user agent): n n
Ô
Server n n
n
Trỡnh duyt web (web browser) Yờu cu truy cp web thông qua URL (URL request). Máy phục vụ web (web server). Gửi cỏc i tng ti client thụng quan phn hi (response).
HTTP version: Ô Ô
HTTP 1.0: RFC 1945 HTTP 1.1: RFC 2068
24/8-25/10/2009
Chapter 2. The Application Layer
18
HTTP: Q trình trao đổi thơng điệp n
n n
n
Client (browser) thiết lập liên kết tới web server (TCP connection) qua cổng 80 (mặc định). Server đồng ý kết nối (accept). Các thông điệp (HTTP messages) được trao đổi giữa browser và server. Ngắt kết nối.
24/8-25/10/2009
Chapter 2. The Application Layer
19
HTTP example Giả sử người dùng truy cập URL: www.uct2.edu/index.html (trang web có text và 10 hình ảnh jpeg) 1a. http client thiết lập liên kết TCP với http server (process) tại địa chỉ www.uct2.edu.vn, cổng 80 (ngầm định với http server).
2. http client gửi http request message (bao gồm cả URL) tới TCP connection socket
time 24/8-25/10/2009
1b. http server tại máy phục vụ www.uct2.edu.vn chờ yêu cầu kết nối TCP tại cổng 80, chấp nhận kết nối rồi thông báo với client.
3. http server nhận request message, tạo ra http response message có chứa các đối tượng được yêu cầu rồi gửi vào socket.
Chapter 2. The Application Layer
20
4. http server ngắt liên kết. 5. http client nhận response message có chứa html file, hiển thị html. Sau đó, phân tích html file, tìm URL của 10 hình ảnh jpeg trong tài liệu.
time
6. Bước 1-5 được lặp lại với từng hình ảnh.
24/8-25/10/2009
Chapter 2. The Application Layer
21
HTTP Connection: Persistent & Non-persistent Non-persistent HTTP n Nhiều nhất là một đối tượng được truyền qua liên kết TCP. n HTTP 1.0
24/8-25/10/2009
Persistent HTTP n Cho phép nhiều đối tượng được truyền trên cùng một liên kết. n Client phân tích, tìm ra và gửi u cầu tới tất cả các đối tượng ngay sau khi nhận được trang HTML ban đầu (base HTML). n HTTP 1.1 sử dụng liên kết loại này ở chế độ mặc định.
Chapter 2. The Application Layer
22
HTTP Message Format: request & response n n
Hai loại message: request và response. HTTP request message: dạng ASCII (đọc được!)
Carriage return, line feed indicates end of message
24/8-25/10/2009
(extra carriage return, line feed)
Chapter 2. The Application Layer
23
HTTP request message
24/8-25/10/2009
Chapter 2. The Application Layer
24
HTTP response message status line (protocol status code status phrase) header lines
HTTP/1.0 200 OK 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 ...
data, e.g., requested html file
24/8-25/10/2009
Chapter 2. The Application Layer
25
HTTP response message
24/8-25/10/2009
Chapter 2. The Application Layer
26
HTTP response message: status codes 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
24/8-25/10/2009
Chapter 2. The Application Layer
27
Ví dụ thực hành 1. Telnet tới web server: telnet www.uct2.edu.vn 80 Mở một kết nối (TCP) tới cổng 80 tại web server của UCT2. Sau khi kết nối, tất cả ký tự gõ vào sẽ được gửi tới cổng 80.
2. Nhập vào GET http request: GET /dangkythilai/help.htm HTTP/1.0 Đây là một http request đơn giản. Nhập xong gõ 2 lần Enter để gửi tới server
3. Kiểm tra kết quả nhận được (http response message)!
24/8-25/10/2009
Chapter 2. The Application Layer
28
User-server interaction: authentication & cookies Authenthication
Server phát sinh một con số server nào đó (#) khi client request, client đồng thời ghi nhớ số này để usual http request msg s dng sau ny: usual http response + Ô Chng thc Set-cookie: # (authentication) Ô Ghi nh s thớch, thúi usual http request msg quen, lựa chọn truy cập cookiecookie: # lần trước. spectific server gửi “cookie” tới usual http response msg action client qua response msg, vd: Set-cookie: 1678453
n
Lần sau truy cập website, client gửi kèm cookie, vd: cookie: 1678453
24/8-25/10/2009
usual http request msg
cookie: # usual http response msg
Chapter 2. The Application Layer
cookiespectific action 30
Conditional GET: client-side caching n
n
MĐ: server không cần gửi client obj tới client khi client đã có http request msg phiên bản cập nhật (up-toIf-modified-since: <date> date) của obj. Client phải chỉ ra thời gian
http response sửa gần đây nhất của obj HTTP/1.0 304 Not Modified trong http request msg.
server object not modified
If-modified-since: <date> n
http request msg
Server sẽ trả về response msg không chứa obj nếu obj là cập nhật:
If-modified-since: <date>
object modified
http response
HTTP/1.0 304 Not Modified
HTTP/1.1 200 OK
<data> 24/8-25/10/2009
31
Chapter 2. The Application Layer
Web Caches (proxy server) n
Client gửi http requests tới web cache thay vỡ ti server. Ô
Ô
n
n
Nu obj cú ti web cache: web cache tạo http response msg gửi về cho client. Nếu không, web cache tạo gửi http request tới server, nhận obj, lưu trữ, rồi gửi http response msg tới client.
Web cache sẽ khiến người dùng có cảm giác duyệt web nhanh hơn, giảm nghẽn mạng. Vd: proxy server trong IE.
24/8-25/10/2009
origin server h tt pr
client http
Proxy server
eq u es t
res pon se st ue q e pr
nse po ht t s re tp ht
client
Chapter 2. The Application Layer
es t eq u r p se h tt pon s e pr h tt
origin server
32
Why Web Caching? Assume: cache is “close” to client (e.g., in same network) n smaller response time: cache closer to client n decrease traffic to distant servers Ô
origin servers public Internet
1.5 Mbps access link institutional network
link out of institutional/local ISP network often bottleneck
24/8-25/10/2009
10 Mbps LAN
institutional cache
Chapter 2. The Application Layer
33
Web servers n
IIS (Microsoft Internet Information Server) Ô Ô
n
Apache web server Ô Ô Ô Ô
n
Chy trên môi trường Windows Đi kèm Windows 2000 Server (IIS 5.0). Mã nguồn mở. Linux/Windows…
Web server được dùng nhiều nhất.
Tomcat, Netscape, Xitami…
24/8-25/10/2009
Chapter 2. The Application Layer
34
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
24/8-25/10/2009
35
Web design: HTML n n
Thiết kế web: To ra ti liu HTML HTML (Hyper Text Markup Language) Ô
Text editor: Notepad, Ultra Edit... Microsoft FrontPage Macromedia Dreamweaver
()
24/8-25/10/2009
Chapter 2. The Application Layer
36
Web programming n
n
HTML = trang web tĩnh (Static web): Web server luôn trả về một trang HTML cố định khi nhận được yêu cầu của người dùng. Lập trình web: tương tác với người s dng thụng qua web. Ô
HTML Form: Send user requests <html> HTML <head> <title>Simple Form</title> </head> <body> <form method="POST" action = "simple.asp">
<input type="text" name="myName" size="20">
name="B1"> </form> </body> </html>
24/8-25/10/2009
<%@ Language=VBScript %> ASP <HTML> <HEAD>
<Title>Simple ASP</title> </HEAD> <BODY> <% dim welcome dim myName myName=Request.Form("myName") welcome = “
Welcome " & myName & “!
” Response.Write (welcome ) %> </BODY> </HTML>
Chapter 2. The Application Layer
38
ASP (Active Server Page) n n
n
n
Microsoft/IIS. Một tài liệu ASP có thể coi như một tài liệu HTML có nhúng các script ASP, được ghi lại dạng tệp *.asp, đặt tại web server.
Mỗi khi được request, các script được chạy ở phía server, kết quả trả v client dng HTML (client khụng thy source). Cỏc i tng: Ô Ô Ô Ô
n
Request Client request. Response Server response. Application QueryString…
Công cụ: MS Visual InterDev Chapter 2. The Application Layer
24/8-25/10/2009
39
Mơ hình 3 lớp trong lập trình web req WEB BROWSER
WEB SERVER (Apache, IIS) resp
Server-side scripts
Các thủ tục truy xuất CSDL
CSDL
24/8-25/10/2009
ASP, PHP, JSP
DB API
DB
Chapter 2. The Application Layer
40
TCP Socket Programming n n
Giao thức HTTP: application-layer protocol Hai ứng dụng application muốn giao tiếp phải sử dụng dịch vụ của tầng giao vn: Ô
Socket: a door between application process and end-end-transport protocol (UCP or TCP).
controlled by application developer controlled by operating system
process
process
socket TCP with buffers, variables
socket TCP with buffers, variables
internet
controlled by operating system
host or server
host or server 24/8-25/10/2009
controlled by application developer
Chapter 2. The Application Layer
41
TCP Socket: Client/server n
Server socket đã mở sẵn và i ti mt cng (port) no ú. Ô
n n
n
Port c s dng chỉ một process cụ thể sử dụng TCP trên một host.
Client phải thiết lập kết nối với server (request). Khi server nhận yêu cầu kết nối, nó tạo ra một
socket mới để tip nhn (cho phộp nhiu clients). Microsoft Visual Basic: Ô
Winsock: Windows Socket Component.
24/8-25/10/2009
Chapter 2. The Application Layer
42
Winsock & OSI
Chapter 2. The Application Layer
24/8-25/10/2009
43
Bài tập n
n
Tìm hiểu cú pháp ngơn ngữ HTML, từ đó xây dựng 3 trang web tĩnh page1.htm, page2.htm, page3.htm (nội dung của 3 trang web này tùy theo ý tưởng sáng tạo của mỗi người)
Sử dụng một trong các ngôn ngữ script ASP, PHP, JSP lập trang web ng (khụng bt buc). Ô
n
Tham kho:
S dng cụng c Visual Basic và Winsock component để viết một web server đơn giản (khụng bt buc) Ô Ô
Phõn tớch lnh GET Tr v response message thớch hợp. n n