mạng may tinh tran ba nhiem chương 2 sinhvienzone com

109 48 0
mạng may tinh tran ba nhiem chương 2   sinhvienzone com

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chương Lớp Application Computer Networking: A Top Down Approach Featuring the Internet, 3rd edition Jim Kurose, Keith Ross Addison-Wesley, July 2004 Slide biên dịch sang tiếng Việt theo cho phép tác giả All material copyright 1996-2006 J.F Kurose and K.W Ross, All Rights Reserved SinhVienZone.com Lớp Application https://fb.com/sinhvienzonevn Chương 2: Nội dung trình bày  2.1 Các nguyên lý ứng dụng mạng  2.2 Web HTTP  2.3 FTP  2.4 Electronic Mail  SMTP, POP3, IMAP  2.5 DNS  2.6 Chia sẻ file P2P  2.7 Lập trình socket vớiTCP  2.8 Lập trình socket với UDP  2.9 Xây dựng Web server Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn Chương 2: Lớp Application Mục tiêu:  Khái niệm, khía cạnh thực giao thức ứng dụng mạng  Các mơ hình dịch vụ lớp transport  Mơ hình clientserver  Mơ hình peer-to-peer  Nghiên cứu giao thức thông qua xem xét số giao thức lớp application     HTTP FTP SMTP / POP3 / IMAP DNS  Lập trình ứng dụng mạng  socket API Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn Một số ứng dụng mạng  E-mail  Điện thoại Internet  Web  Hội thảo video thời  Tin nhắn nhanh  Đăng nhập từ xa  Chia sẻ file P2P gian thực  Tính tốn lớn, tính tốn song song  Trò chơi nhiều người mạng  Streaming video clips Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn Tạo ứng dụng mạng Viết chương trình để:    chạy hệ thống đầu cuối khác, truyền thông qua mạng Ví dụ: Web: phần mềm Web server truyền thơng với phần mềm trình duyệt application transport network data link physical Phần mềm nhỏ viết cho thiết bị trung tâm mạng   thiết bị trung tâm mạng không chạy mã ứng dụng người dùng ứng dụng hệ thống đầu cuối cho phép phát triển ứng dụng nhanh, phổ biến application transport network data link physical application transport network data link physical Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 2.1 Các nguyên lý ứng dụng mạng Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn Các kiến trúc ứng dụng  Client-server  Peer-to-peer (P2P)  Lai client-server P2P Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn Kiến trúc client-server server:    host hoạt động địa IP cố định nhóm server để chia sẻ công việc clients:     truyền thông với server kết nối khơng liên tục có địa IP thay đổi khơng truyền thơng trực tiếp với client khác Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn Kiến trúc P2P túy  khơng có server ln hoạt động  truyền thông trực tiếp với hệ thống đầu cuối  điểm kết nối không liên tục thay đổi địa IP  Ví dụ: Gnutella Độ linh hoạt cao khó quản lý Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn Lai client-server P2P Skype    ứng dụng điện thoại Internet Tìm địa thành viên xa: server trung tâm Kết nối trực tiếp Client-client (không thông qua server) Tin nhắn nhanh   Chat user P2P Mơ hình client-server: • User đăng ký địa IP họ với server trung tâm trực tuyến • User tiếp xúc với server trung tâm để tìm địa IP bạn Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 10 Ví dụ: Java server (TCP) import java.io.*; import java.net.*; class TCPServer { tạo socket mời tiếp xúc port 6789 public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; Chờ, client tiếp cận với server Tạo input stream, gắn vào socket ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 95 Ví dụ: Java server (TCP) Tạo output stream, gắn vào socket DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); Đọc dòng từ socket clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; Viết dòng từ socket outToClient.writeBytes(capitalizedSentence); } } kết thúc vòng lặp while quay lại chờ cho kết nối client khác } Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 96 2.8 Lập trình socket với UDP (xem thêm slide Lập trình socket) Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 97 Lập trình socket với UDP UDP: khơng “kết nối” client server  không bắt tay  người gửi rõ ràng gắn địa IP port đích vào gói  phải trích địa IP, port người gửi từ gói nhận UDP: liệu truyền khơng theo thứ tự, bị mát góc nhìn ứng dụng UDP cung cấp việc truyền khơng tin cậy nhóm byte (“datagrams”) client server Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 98 Giao tiếp socket Client/server: UDP Server (chạy hostid) create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port number Client create socket, clientSocket = DatagramSocket() Create, address (hostid, port=x, send datagram request using clientSocket read reply from clientSocket close clientSocket Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 99 Ví dụ: Java client (UDP) in p u t s tre a m Client process m o n ito r in F r o m U s e r k e y b o a rd Input: nhận gói P ro c e s s (còn gọi TCP nhận “dòng byte”) UDP p a cke t sendPacket (còn gọi TCP gửi “dòng byte”) re c e iv e P a c k e t Output: gửi gói client UDP socket UDP p a cke t c lie n tS o c k e t to n e tw o rk UDP so cke t fro m n e tw o rk Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 100 Ví dụ: Java client (UDP) import java.io.*; import java.net.*; tạo input stream tạo client socket dịch hostname thành địa IP dùng DNS 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(); Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 101 Ví dụ: Java client (UDP) tạo datagram với liệu để gửi, độ dài, địa IP, port gửi datagram đến server DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); đọc datagram từ server clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } } Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 102 Ví dụ: Java server (UDP) import java.io.*; import java.net.*; Tạo datagram socket port 9876 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) { Tạo không gian để nhận datagram nhận datagram DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 103 Ví dụ: Java server (UDP) String sentence = new String(receivePacket.getData()); lấy địa IP port #, người gửi InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); tạo datagram để gửi tới client DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); viết datagram vào socket serverSocket.send(sendPacket); } } kết thúc vòng lặp while, quay lại chờ datagram khác } SinhVienZone.com Lớp Application https://fb.com/sinhvienzonevn 104 2.9 Xây dựng Web server Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 105 Xây dựng Web server đơn giản  quản lý yêu cầu HTTP  chấp nhận yêu cầu  phân tích cú pháp header  lấy file yêu cầu từ hệ thống file server  tạo thông điệp đáp ứng HTTP:   sau tạo server, bạn u cầu file dùng trình duyệt (ví dụ: IE)  xem giáo trình để biết thêm chi tiết dòng header + file  gửi đáp ứng đến client Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 106 Chương 2: Tổng kết Tổng quan số vấn đề ứng dụng mạng!  Các kiến trúc ứng dụng  client-server  P2P  lai  yêu cầu dịch vụ:  tin cậy, bandwidth, trễ  giao thức đặc biệt:  HTTP  FTP  SMTP, POP, IMAP  DNS  lập trình socket  mơ hình dịch vụ Internet transport   connection-oriented, tin cậy: TCP không tin cậy, datagrams: UDP Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 107 Chương 2: Tổng kết Phần quan trọng: nghiên cứu giao thức  trao đổi thơng điệp u cầu/trả lời điển hình:   client yêu cầu thông tin dịch vụ service server đáp ứng với liệu, mã trạng thái  dạng thông điệp:  headers: trường cho biết thông tin liệu  liệu: thông tin để truyền thông  điều khiển với thông điệp     liệu  in-band, out-of-band tập trung khơng tập trung khơng trạng thái có trạng thái truyền thông điệp tin cậy không tin cậy “sự phức tạp vấn đề liên quan đến mạng” Lớp Application SinhVienZone.com https://fb.com/sinhvienzonevn 108  k2c62@googlegroups.com SinhVienZone.com https://fb.com/sinhvienzonevn .. .Chương 2: Nội dung trình bày  2. 1 Các nguyên lý ứng dụng mạng  2. 2 Web HTTP  2. 3 FTP  2. 4 Electronic Mail  SMTP, POP3, IMAP  2. 5 DNS  2. 6 Chia sẻ file P2P  2. 7 Lập trình... SinhVienZone. com https://fb .com/ sinhvienzonevn 18 2. 2 Web HTTP Lớp Application SinhVienZone. com https://fb .com/ sinhvienzonevn 19 Web HTTP Một số thuật ngữ chuyên môn  Web page (trang Web) bao gồm... data link physical Lớp Application SinhVienZone. com https://fb .com/ sinhvienzonevn 2. 1 Các nguyên lý ứng dụng mạng Lớp Application SinhVienZone. com https://fb .com/ sinhvienzonevn Các kiến trúc ứng

Ngày đăng: 28/01/2020, 22:51

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan