Bài giảng Mạng máy tính - Chương 2: Giao thức tầng ứng dụng (application layer) - Trần Quang Diệu

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). 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.

ppt49 trang | Chia sẻ: thucuc2301 | Lượt xem: 633 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Bài giảng Mạng máy tính - Chương 2: Giao thức tầng ứng dụng (application layer) - Trần Quang Diệu, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chương 2. Giao thức tầng ứng dụng (application layer)Quang Dieu Tran, PhDFaculty of Information TechnologyUniversity of Communication and Transport (Branch in Ho Chi Minh City)Email: dieutq@gmail.comWebsite: sites.google.com/sites/tranlecturesApplication LayerChapter 2. The Application Layer2Ch2. The Application Layer2.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 Layer32.1. Một số khái niệm và nguyên tắcMộ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 Layer4Mộ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 Layer5Net applications vs. application-layer protocolsApplications: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 clientApplication-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 Layer6Cá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ínhSử 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 nhauGiao 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 agentGiao 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 Layer7Mô 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:ClientThiết lập liên kết với server.Yêu cầu dịch vụ từ server.Nhiều clients.Vd: web browser.ServerChờ và cung cấp 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.Chapter 2. The Application Layer8applicationtransportnetworkdata linkphysicalapplicationtransportnetworkdata linkphysicalrequestreplyMô 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 Layer9Client-server & P2P examplesCNN.comYahoo! MailGoogleYahoo! MessengerInternet Relay Chat (IRC)Chapter 2. The Application Layer10Client-serverClient-serverClient-serverClient-server & P2PClient-server & P2PDịch vụ tầng dưới đối với application-layerCó 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/IPFile 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/IPNFS (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 Layer11Yêu cầu đối với việc vận chuyển dữ liệuData lossCá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.TimingCác ứng dụng online cần độ trễ (delay) nhỏ (phone, games).BandwidthCá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 Layer12Các yêu cầu giao vận của một số ứng dụngChapter 2. The Application Layer13Internet Transport protocols: TCP & UDPTCP (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.UDP (User Datagram Protocol)unreliable data transfer between sending and receiving processdoes not provide: connection setup, reliability, flow control, congestion control, timing, or bandwidth guarantee.Chapter 2. The Application Layer14Internet applications: TCP or UDP?Chapter 2. The Application Layer15Ch2. The Application Layer2.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 Layer162.2. Web & HTTPTrang 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, HTMLVề 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) 2. The Application Layer17Protocol Host name & port PathHTTP (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).ServerMá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 1945HTTP 1.1: RFC 2068Chapter 2. The Application Layer18HTTP: Quá trình trao đổi thông điệpClient (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 Layer19HTTP exampleChapter 2. The Application Layer201a. 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 socket1b. 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.timeGiả sử người dùng truy cập URL: www.uct2.edu/index.html (trang web có text và 10 hình ảnh jpeg)Chapter 2. The Application Layer215. 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.6. Bước 1-5 được lặp lại với từng hình ảnh.4. http server ngắt liên kết. timeHTTP Connection: Persistent & Non-persistentNon-persistent HTTPNhiều nhất là một đối tượng được truyền qua liên kết TCP.HTTP 1.0Persistent HTTPCho phép nhiều đối tượng được truyền trên cùng một liên kết.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 Layer22HTTP Message Format: request & responseHai loại message: request và response.HTTP request message: dạng ASCII (đọc được!)Chapter 2. The Application Layer23GET /somedir/page.html HTTP/1.0 User-agent: Mozilla/4.0 Accept: text/html, image/gif,image/jpeg Accept-language:fr (extra carriage return, line feed) request line(GET, POST, HEAD commands)header linesCarriage return, line feed indicates end of messageHTTP request messageChapter 2. The Application Layer24HTTP response messageChapter 2. The Application Layer25HTTP/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 ... status line(protocolstatus codestatus phrase)header linesdata, e.g., requestedhtml fileHTTP response messageChapter 2. The Application Layer26HTTP response message: status codesChapter 2. The Application Layer27200 OKrequest succeeded, requested object later in this message301 Moved Permanentlyrequested object moved, new location specified later in this message (Location:)400 Bad Requestrequest message not understood by server404 Not Foundrequested document not found on this server505 HTTP Version Not SupportedVí dụ thực hành1. Telnet tới web server:Chapter 2. The Application Layer28Mở 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.telnet 113.161.72.233 802. 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 server3. Kiểm tra kết quả nhận được (http response message)!User-server interaction: authentication & cookiesChapter 2. The Application Layer29clientserverusual http request msg401: authorization req.WWW authenticate:usual http request msg+ Authorization: usual http response msgusual http request msg+ Authorization: usual http response msgtimeAuthenthicationCookies: keeping “state”Server phát sinh một con số nào đó (#) khi client request, đồng thời ghi nhớ số này để sử dụng sau này:Chứng thực (authentication)Ghi nhớ sở thích, thói quen, lựa chọn truy cập lần trước.server gửi “cookie” tới client qua response msg, vd:Set-cookie: 1678453Lần sau truy cập website, client gửi kèm cookie, vd:cookie: 1678453Chapter 2. The Application Layer30clientserverusual http request msgusual http response +Set-cookie: #usual http request msgcookie: #usual http response msgusual http request msgcookie: #usual http response msgcookie-spectificactioncookie-spectificactionConditional GET: client-side cachingMục đích: server không cần gửi obj tới client khi client đã có phiên bản cập nhật (up-to-date) của obj.Client phải chỉ ra thời gian sửa gần đây nhất của obj trong http request msg. If-modified-since: Server sẽ trả về response msg không chứa obj nếu obj là cập nhật: HTTP/1.0 304 Not ModifiedChapter 2. The Application Layer31clientserverhttp request msgIf-modified-since: http responseHTTP/1.0 304 Not Modifiedobject not modifiedhttp request msgIf-modified-since: http responseHTTP/1.1 200 OKobject modifiedWeb 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 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.Ví dụ: proxy server trong IE.Chapter 2. The Application Layer32clientProxyserverclienthttp requesthttp requesthttp responsehttp responsehttp requesthttp responseorigin serverorigin serverWhy Web Caching?Assume: cache is “close” to client (e.g., in same network)smaller response time: cache “closer” to clientdecrease traffic to distant serverslink out of institutional/local ISP network often bottleneck Chapter 2. The Application Layer33originserverspublic Internetinstitutionalnetwork10 Mbps LAN1.5 Mbps access linkinstitutionalcacheCaching example Assumptionsaverage object size = 100 Kbitsavg. request rate from institution’s browsers to origin servers = 15/secdelay from institutional router to any origin server and back to router = 2 secConsequencesutilization on LAN = 15%utilization on access link = 100%Total delay = Internet delay + access delay + LAN delay = 2 sec + minutes + millisecondsChapter 2. The Application Layer34originserverspublic Internetinstitutionalnetwork10 Mbps LAN1.5 Mbps access linkinstitutionalcacheCaching example (cont)Possible solutionincrease bandwidth of access link to, say, 10 MbpsConsequencesutilization on LAN = 15%utilization on access link = 15%Total delay = Internet delay + access delay + LAN delay = 2 sec + msecs + msecsoften a costly upgradeChapter 2. The Application Layer35originserverspublic Internetinstitutionalnetwork10 Mbps LAN10 Mbps access linkinstitutionalcacheCaching example (cont)Install cachesuppose hit rate is 0.4Consequence40% requests will be satisfied almost immediately60% requests satisfied by origin serverutilization of access link reduced to 60%, resulting in negligible delays (say 10 msec).Total avg delay = Internet delay + access delay + LAN delay = 0.6*(2.01) secs + milliseconds ; ; ; Thẻ đóng, vd: ; ;;Công cụ:Text editor: Notepad, Ultra Edit...Microsoft FrontPageMacromedia Dreamweaver ( Chapter 2. The Application Layer39Web programmingHTML = 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, PerlChapter 2. The Application Layer40HTML Form: Send user requestsSimple Form Chapter 2. The Application Layer41Simple ASPWelcome " & myName & “!” Response.Write (welcome )%>ASPHTMLASP (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.ApplicationQueryStringCông cụ: MS Visual InterDevChapter 2. The Application Layer42Mô hình 3 lớp trong lập trình webChapter 2. The Application Layer43WEB BROWSERWEB SERVER (Apache, IIS)ASP, PHP, JSPDBreqrespDB APICác thủ tục truy xuất CSDLCSDLServer-side scriptsSocket programmingSocket APIintroduced in BSD 4.1 UNIX, 1981explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: unreliable datagram reliable, byte stream-oriented Chapter 2. The Application Layer44a host-local, application-created, OS-controlled interface (a “door”) into whichapplication process can both send and receive messages to/from another application processsocketGoal: learn how to build client/server application that communicate using socketsTCP Socket ProgrammingGiao thức HTTP: application-layer protocolHai ứ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).Chapter 2. The Application Layer45processTCP withbuffers,variablessocketcontrolled byapplicationdevelopercontrolled byoperatingsystemhost orserverprocessTCP withbuffers,variablessocketcontrolled byapplicationdevelopercontrolled byoperatingsystemhost orserverinternetTCP Socket: Client/serverServer 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 Layer46Winsock & OSIChapter 2. The Application Layer47Bài tậpTì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 thanhv.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 GETTrả về response message thích hợp.HeaderBodyChapter 2. The Application Layer48 : 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ảngChapter 2. The Application Layer49

Các file đính kèm theo tài liệu này:

  • pptmang_may_tinhchapter2a_6608_2004688.ppt
Tài liệu liên quan