Bài giảng Mạng máy tính - Chương 2 (Phần 1): Giao thức tầng ứng dụng (Application layer)

ppt 49 trang hapham 2070
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Mạng máy tính - Chương 2 (Phần 1): Giao thức tầng ứng dụng (Application layer)", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptbai_giang_mang_may_tinh_chuong_2_giao_thuc_tang_ung_dung_app.ppt

Nội dung text: Bài giảng Mạng máy tính - Chương 2 (Phần 1): Giao thức tầng ứng dụng (Application layer)

  1. Chương 2. Giao thức tầng ứng dụng (application layer) Quang Dieu Tran, PhD Faculty of Information Technology University of Communication and Transport (Branch in Ho Chi Minh City) Email: dieutq@gmail.com Website: sites.google.com/sites/tranlectures
  2. Application Layer Chapter 2. The Application Layer 2
  3. 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
  4. 2.1. Một số khái niệm và nguyên tắc ◼ Một số chương trình ứng dụng (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. Chapter 2. The Application Layer 4
  5. Một số giao thức (application-layer protocols) ◼ HTTP (Hyper Text Transfer Protocol)  Giao thức web. ◼ FTP (File Transfer Protocol)  Giao thức truyền tệp. ◼ SMTP (Simple Mail Transfer Protocol)  Giao thức truyền thư điện tử. ◼ POP (Post Office Protocol)  Giao thức nhận thư điện tử. ◼ IMAP (Internet Mail Access Protocol)  Giao thức nhận thư điện tử (khác POP3) ◼ DNS (Domain Name System)  DNS = hệ thống tên miền, giao thức DNS quy định quy tắc sử dụng tên miền. ◼ TFTP (Trivial FTP)  Một dạng khác của FTP. Chapter 2. The Application Layer 5
  6. Net applications vs. application-layer protocols ◼ Applications:  Chương 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 dụng.  Vd: web browser, email client ◼ Application-layer protocols:  Là một thành phần của 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). Chapter 2. The Application Layer 6
  7. Các chương trình giao tiếp với nhau ntn? ◼ Các chương trình trên cùng một máy tính Sử dụng các dịch vụ được cung cấp bởi hệ điều hành (interprocess communication). ◼ Các chương trình trên các máy tính khác nhau 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 tiếp với người sử dụng (giao diện sử dụng). Giao tiếp với các giao thức mạng. Chapter 2. The Application Layer 7
  8. 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 cụ thể bao gồm hai phần: application transport network ◼ Client data link physical  Thiết lập liên kết với server.  Yêu cầu dịch vụ từ server. request  Nhiều clients.  Vd: web browser. ◼ Server  Chờ và cung cấp các dịch vụ khi client yêu cầu. reply  Thường chỉ có 1 server, không thay đổi địa chỉ, chạy 24/7.  application Vd: web server. transport network data link physical Chapter 2. The Application Layer 8
  9. Mô hình ngang hàng (Peer-to-peer paradigm) ◼ 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. Chapter 2. The Application Layer 9
  10. Client-server & P2P examples ◼ CNN.com → Client-server ◼ Yahoo! Mail → Client-server ◼ Google → Client-server ◼ Yahoo! Messenger → Client-server & P2P ◼ Internet Relay Chat (IRC) → Client-server & P2P Chapter 2. The Application Layer 10
  11. Dịch vụ tầng dưới đối với application-layer ◼ 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 ▪ File formats: picture (JPEG, GIF ), audio (MP3, RM ), video (MPEG, MOV ), document (HTML, XML ). ▪ Data encryption & compression. ▪ Session: gắn liền với application trong TCP/IP ▪ NFS (Network File System), RPC (Remote Procedure Call), SQL (Structured Query Language), SSL (Secure Socket Layer). ▪ Transport (*) ▪ Cung cấp dịch vụ vận chuyển cho application (TCP/IP). ▪ Hai giao thức: TCP và UDP. Chapter 2. The Application Layer 11
  12. Yêu cầu đối với việc vận chuyển dữ liệu ◼ Data loss  Các ứng dụng audio/video có thể chấp 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. ◼ Timing  Các ứng dụng online cần độ 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 độ. Chapter 2. The Application Layer 12
  13. Các yêu cầu giao vận của một số ứng dụng Chapter 2. The Application Layer 13
  14. Internet Transport protocols: TCP & UDP TCP (Transmission Control Protocol) UDP (User Datagram Protocol) ▪ connection-oriented: setup ▪ unreliable data transfer required between client, server. between sending and receiving ▪ reliable transport between process sending and receiving process. ▪ does not provide: connection ▪ flow control: sender won’t setup, reliability, flow control, overwhelm receiver. congestion control, timing, or bandwidth guarantee. ▪ congestion control: throttle sender when network overloaded. ▪ does not providing: timing, minimum bandwidth guarantees. Chapter 2. The Application Layer 14
  15. Internet applications: TCP or UDP? Chapter 2. The Application Layer 15
  16. 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. Chapter 2. The Application Layer 16
  17. 2.2. Web & HTTP ◼ 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).  HTML = Hyper Text Markup Language.  HTML sử dụng 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)   Protocol Host name & port Path Chapter 2. The Application Layer 17
  18. HTTP (Hyper Text Transfer Protocol) ◼ Giao thức tầng ứng dụng web. ◼ Sử dụng mô hình client/server.  Client (user agent): ◼ Trình duyệt web (web browser) ◼ Yêu cầu truy cập web thông qua URL (URL request).  Server ◼ Máy phục vụ web (web server). ◼ Gửi các đối tượng tới client thông quan phản hồi (response). ◼ HTTP version:  HTTP 1.0: RFC 1945  HTTP 1.1: RFC 2068 Chapter 2. The Application Layer 18
  19. HTTP: Quá trình trao đổi thông điệp ◼ 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. Chapter 2. The Application Layer 19
  20. 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) 1b. http server tại máy phục vụ tại địa chỉ www.uct2.edu.vn, www.uct2.edu.vn chờ yêu cầu cổng 80 (ngầm định với http kết nối TCP tại cổng 80, chấp server). nhận kết nối rồi thông báo với 2. http client gửi http request client. message (bao gồm cả URL) tới TCP connection socket 3. http server nhận request message, tạo ra http response message có chứa các đối tượng time được yêu cầu rồi gửi vào socket. Chapter 2. The Application Layer 20
  21. 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 time jpeg trong tài liệu. 6. Bước 1-5 được lặp lại với từng hình ảnh. Chapter 2. The Application Layer 21
  22. HTTP Connection: Persistent & Non-persistent Non-persistent HTTP Persistent HTTP ◼ Nhiều nhất là một đối ◼ Cho phép nhiều đối tượng tượng được truyền qua được truyền trên cùng một liên kết TCP. liên kết. ◼ HTTP 1.0 ◼ Client phân tích, tìm ra và gửi yê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). ◼ HTTP 1.1 sử dụng liên kết loại này ở chế độ mặc định. Chapter 2. The Application Layer 22
  23. HTTP Message Format: request & response ◼ Hai loại message: request và response. ◼ HTTP request message: dạng ASCII (đọc được!) request line (GET, POST, GET /somedir/page.html HTTP/1.0 HEAD commands) User-agent: Mozilla/4.0 Accept: text/html, image/gif,image/jpeg header Accept-language:fr lines (extra carriage return, line feed) Carriage return, line feed indicates end of message Chapter 2. The Application Layer 23
  24. HTTP request message Chapter 2. The Application Layer 24
  25. HTTP response message status line (protocol status code HTTP/1.0 200 OK status phrase) Date: Thu, 06 Aug 1998 12:00:15 GMT Server: Apache/1.3.0 (Unix) Last-Modified: Mon, 22 Jun 1998 header Content-Length: 6821 lines Content-Type: text/html data data data data data data, e.g., requested html file Chapter 2. The Application Layer 25
  26. HTTP response message Chapter 2. The Application Layer 26
  27. 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 Chapter 2. The Application Layer 27
  28. Ví dụ thực hành 1. Telnet tới web server: telnet 113.161.72.233 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)! Chapter 2. The Application Layer 28
  29. User-server interaction: authentication & cookies Authenthication client server usual http request msg 401: authorization req. WWW authenticate: usual http request msg + Authorization: usual http response msg usual http request msg + Authorization: time usual http response msg Chapter 2. The Application Layer 29
  30. Cookies: keeping “state” ◼ Server phát sinh một con số nào đó (#) khi client request, client server đồng thời ghi nhớ số này để sử dụng sau này: usual http request msg Chứng thực usual http response + (authentication) Set-cookie: # Ghi nhớ sở thích, thói usual http request msg quen, lựa chọn truy cookie: # cookie- cập lần trước. spectific usual http response msg ◼ server gửi “cookie” tới action client qua response msg, vd: Set-cookie: 1678453 usual http request msg cookie: # cookie- ◼ Lần sau truy cập website, spectific client gửi kèm cookie, vd: usual http response msg action cookie: 1678453 Chapter 2. The Application Layer 30
  31. Conditional GET: client-side caching ◼ Mục đích: server không cần client server gửi obj tới client khi client http request msg đã có phiên bản cập nhật If-modified-since: (up-to-date) của obj. object not ◼ Client phải chỉ ra thời gian http response modified sửa gần đây nhất của obj HTTP/1.0 trong http request msg. 304 Not Modified If-modified-since: ◼ Server sẽ trả về response http request msg If-modified-since: msg không chứa obj nếu obj object là cập nhật: modified HTTP/1.0 304 Not Modified http response HTTP/1.1 200 OK Chapter 2. The Application Layer 31
  32. Web Caches (proxy server) ◼ Client gửi http requests tới web cache thay vì tới server.  Nếu obj có tại web cache: web cache tạo http response msg origin gửi về cho client. server  Nếu không, web cache tạo gửi Proxy http request tới server, nhận server obj, lưu trữ, rồi gửi http client 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. ◼ client Ví dụ: proxy server trong IE. origin server Chapter 2. The Application Layer 32
  33. Why Web Caching? Assume: cache is “close” to origin client (e.g., in same servers public network) Internet ◼ smaller response time: cache “closer” to client 1.5 Mbps ◼ decrease traffic to distant access link servers institutional network 10 Mbps LAN  link out of institutional/local ISP network often bottleneck institutional cache Chapter 2. The Application Layer 33
  34. Caching example Assumptions origin ◼ average object size = 100 Kbits servers ◼ avg. request rate from public institution’s browsers to origin Internet servers = 15/sec ◼ delay from institutional router to any origin server and back to 1.5 Mbps router = 2 sec access link institutional Consequences network 10 Mbps LAN ◼ utilization on LAN = 15% ◼ utilization on access link = 100% ◼ Total delay = Internet delay + access delay + LAN delay institutional = 2 sec + minutes + milliseconds cache Chapter 2. The Application Layer 34
  35. Caching example (cont) Possible solution origin servers ◼ increase bandwidth of public access link to, say, 10 Internet Mbps Consequences 10 Mbps ◼ utilization on LAN = 15% access link ◼ utilization on access link = institutional network 15% 10 Mbps LAN ◼ Total delay = Internet delay + access delay + LAN delay institutional = 2 sec + msecs + msecs cache ◼ often a costly upgrade Chapter 2. The Application Layer 35
  36. Caching example (cont) Install cache origin ◼ suppose hit rate is 0.4 servers Consequence public Internet ◼ 40% requests will be satisfied almost immediately ◼ 60% requests satisfied by origin server 1.5 Mbps ◼ utilization of access link access link reduced to 60%, resulting in institutional network negligible delays (say 10 10 Mbps LAN msec). ◼ Total avg delay = Internet delay + access delay + LAN delay = 0.6*(2.01) secs + institutional milliseconds < 1.4 secs cache Chapter 2. The Application Layer 36
  37. Web servers ◼ IIS (Microsoft Internet Information Server) Chạy trên môi trường Windows Đi kèm Windows 2000 Server (IIS 5.0). ◼ Apache web server Mã nguồn mở. Linux/Windows  Web server được dùng nhiều nhất. ◼ Tomcat, Netscape, Xitami Chapter 2. The Application Layer 37
  38. 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 38
  39. Web design: HTML ◼ Thiết kế web: Tạo ra tài liệu HTML ◼ HTML (Hyper Text Markup Language)  Trong tài liệu HTML có thể có nhiều đối tượng ◼ Text ◼ Images ◼ Hyper link  Sử dụng thẻ (tag) để diễn đạt đối tượng. ◼ Thẻ mở, vd: ; ; ; ◼ Thẻ đóng, vd: ; ; ; ◼ Công cụ:  Text editor: Notepad, Ultra Edit  Microsoft FrontPage  Macromedia Dreamweaver ◼ ( Chapter 2. The Application Layer 39
  40. Web programming ◼ 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ử dụng thông qua web. Lập trình web # Lập trình Pascal, C: ◼ Tương tác thông qua HTTP Message. Client-side scripts: Java script, VB script. Server-side scripts: ASP, JSP, PHP, Perl Chapter 2. The Application Layer 40
  41. HTML Form: Send user requests HTML ASP Simple Form Simple ASP dim welcome dim myName myName=Request.Form("myName") welcome = “ Welcome " & myName & “! ” Response.Write (welcome ) %> Chapter 2. The Application Layer 41
  42. ASP (Active Server Page) ◼ 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 dạng HTML (client không thấy source). ◼ Các đối tượng:  Request → Client request.  Response → Server response.  Application  QueryString ◼ Công cụ: MS Visual InterDev Chapter 2. The Application Layer 42
  43. Mô hình 3 lớp trong lập trình web req WEB BROWSER WEB SERVER (Apache, IIS) resp Server-side scripts ASP, PHP, JSP Các thủ tục truy xuất CSDL DB API CSDL DB Chapter 2. The Application Layer 43
  44. Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API socket ◼ introduced in BSD 4.1 UNIX, a host-local, 1981 application-created, ◼ explicitly created, used, released OS-controlled interface by apps (a “door”) into which application process can ◼ client/server paradigm both send and ◼ two types of transport service via receive messages to/from socket API: another application  unreliable datagram process  reliable, byte stream-oriented Chapter 2. The Application Layer 44
  45. TCP Socket Programming ◼ 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 vận:  Socket: a door between application process and end-end-transport protocol (UCP or TCP). controlled by controlled by process process application application developer developer socket socket TCP with controlled by controlled by TCP with operating operating buffers, buffers, internet system system variables variables host or host or server server Chapter 2. The Application Layer 45
  46. TCP Socket: Client/server ◼ Server socket đã mở sẵn và đợi tại một cổng (port) nào đó. Port được sử dụng để 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 để tiếp nhận (cho phép nhiều clients). ◼ Microsoft Visual Basic: Winsock: Windows Socket Component. Chapter 2. The Application Layer 46
  47. Winsock & OSI Chapter 2. The Application Layer 47
  48. Bài tập ◼ 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) → nộp báo cáo bằng file nén .zip hoặc .rar bao gồm 3 file .htm và các file hình ảnh, âm thanh v.v liên quan (nếu có) ◼ Sử dụng một trong các ngôn ngữ script ASP, PHP, JSP lập trang web động (không bắt buộc).  Tham khảo: ◼ Sử dụng công cụ Visual Basic và Winsock component để viết một web server đơn giản (không bắt buộc)  Phân tích lệnh GET  Trả về response message thích hợp. ◼ Header ◼ Body Chapter 2. The Application Layer 48
  49. : bắt đầu → đóng tài liệu dạng HTML : quy định các đặc điểm phải hiển thị trang HTML : Hiển thị nội dung thanh tiêu đề cửa sổ WebBrowser : mở/đóng phần thân của 1 trang html : tạo liên kết (link) đến trang khác : font chữ : quy định đoạn văn : hiển thị ảnh : mở đóng một bảng : mở đóng 1 dòng trong bảng : mở/đóng 1 cell trong row của bảng Chapter 2. The Application Layer 49