1 / 28

From last Lecture

From last Lecture. Applications and application-layer protocols Client and server Socket Port number Transport protocol services How to choose. 2.1 Principles of network applications 2.2 Web and HTTP 2.7 Socket programming with TCP 2.9 Building a Web server

aminia
Télécharger la présentation

From last Lecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. From last Lecture • Applications and application-layer protocols • Client and server • Socket • Port number • Transport protocol services • How to choose 2: Application Layer

  2. 2.1 Principles of network applications 2.2 Web and HTTP 2.7 Socket programming with TCP 2.9 Building a Web server 2.8 Socket programming with UDP 2.3 FTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P file sharing Chapter 2: Application layer 2: Application Layer

  3. Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API • introduced in BSD4.1 UNIX, 1981 • explicitly created, used, released by apps • client/server paradigm • two types of transport service via socket API: • reliable, byte stream-oriented (TCP) • unreliable datagram (UDP) 2: Application Layer

  4. Socket API: socket a host-local, application-created/owned, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another (remote or local) application process Establish connection Send/recv data close the socket Socket programming Create socket 2: Application Layer

  5. Socket API: process process TCP with buffers, variables TCP with buffers, variables Establish connection Send/recv data close the socket socket socket Socket programming using TCP controlled by application developer controlled by application developer controlled by operating system controlled by operating system internet server host Create socket 2: Application Layer

  6. Client must contact server server process must first be running server must have created socket (door) that welcomes client’s contact Client contacts server by: creating client-local TCP socket specifying IP address, port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server TCP creates new socket for server process to communicate with client allows server to talk with multiple clients source port numbers used to distinguish clients (more in Chap 3) TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint Socket programming with TCP 2: Application Layer

  7. 2: Application Layer

  8. A stream is a sequence of characters that flow into or out of a process. An input stream is attached to some input source for the process, eg, keyboard or socket. An output stream is attached to an output source, eg, monitor or socket. Stream jargon 2: Application Layer

  9. Example client-server app: 1) client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream) 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket (inFromServer stream) Socket programming with TCP Client process client TCP socket 2: Application Layer

  10. create socket, connect to hostid, port=x create socket, port=x, for incoming request: clientSocket = Socket() welcomeSocket = ServerSocket() TCP connection setup wait for incoming connection request connectionSocket = welcomeSocket.accept() send request using clientSocket read request from connectionSocket write reply to connectionSocket read reply from clientSocket close connectionSocket close clientSocket Client/server socket interaction: TCP Server (running on hostid) Client 2: Application Layer

  11. Example: Java client (TCP) import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket 2: Application Layer

  12. Example: Java client (TCP), cont. Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } } Send line to server Read line from server 2: Application Layer

  13. Example: Java server (TCP) import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket 2: Application Layer

  14. Example: Java server (TCP), cont DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } } } Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection 2: Application Layer

  15. Socket programming: references C-language tutorial (audio/slides): • “Unix Network Programming” (J. Kurose), http://manic.cs.umass.edu/~amldemo/courseware/intro. Java-tutorials: • “All About Sockets” (Sun tutorial), http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html • “Socket Programming in Java: a tutorial,” http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html 2: Application Layer

  16. 2.1 Principles of network applications 2.2 Web and HTTP 2.7 Socket programming with TCP 2.9 Building a Web server 2.3 FTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P file sharing Chapter 2: Application layer 2: Application Layer

  17. HTTP: hypertext transfer protocol Web’s application layer protocol client/server model client: browser that requests, receives, “displays” Web objects server: Web server sends objects in response to requests HTTP Nonpersistent and Persistent (with or without pipelining) Response time Message format Request and response HTTP overview HTTP request PC running Explorer HTTP response HTTP request Server running Apache Web server HTTP response Mac running Navigator 2: Application Layer

  18. fetch www.someSchool.edu/someDepartment/home.index 1a. http client initiates TCP connection to http server (process) at www.SomeSchool.edu, port 80 http example (cont.) (contains text, references to 10 jpeg images) time 1b.http server at host www.someSchool.edu waiting for TCP connection at port 80. “accepts” connection 2. http client sends http request message (containing URL) into TCP connection socket 3.http server receives request, forms response msg containing requested object (someDepartment/home.index), sends message into socket 5. http client receives response message containing html file, displays html. Parsing html file, finds 10 referenced jpeg objects 4.http server closes TCP connection 6. Steps 1-4(5) repeated for each of 10 jpeg objects 2: Application Layer

  19. handles one HTTP request accepts the request parses header obtains requested file from server’s file system creates HTTP response message: header lines + file sends response to client after creating server, you can request file using a browser (eg IE explorer) Building a simple Web server 2: Application Layer

  20. HTTP request message • two types of HTTP messages: request, response • HTTP request message: • ASCII (human-readable format) request line (GET, POST, HEAD commands) GET /somedir/page.html HTTP/1.1 Host: www.someschool.edu User-agent: Mozilla/4.0 Connection: close Accept-language:fr (extra carriage return, line feed) header lines Carriage return, line feed indicates end of message 2: Application Layer

  21. HTTP response message status line (protocol status code status phrase) HTTP/1.1 200 OK Connection close 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 ... header lines data, e.g., requested HTML file 2: Application Layer

  22. handles one HTTP request accepts the request Building a simple Web server (cont’d) • import java.io.*; • import java.net.*; • import java.util.*; • class WebServer { • public static void main(String argv[]) throws Exception • { • String requestMessageLine; • String fileName; • ServerSocket ListenSocket = new ServerSocket(6789); • Socket connectionSocket = ListenSocket.accept(); 2: Application Layer

  23. streams Building a simple Web server (cont’d) • BufferedReader inFromClient = • new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); • DataOutputStream outToClient = • new DataOutputStream(connectionSocket.getOutputStream()); • requestMessageLine = inFromClient.readLine(); 2: Application Layer

  24. parses header Building a simple Web server (cont’d) • StringTokenizer tokenizedLine = New StringTokenizer (requestMeesageLine); If (tokenizedLIne.nextToken().euals(“GET”)) { 2: Application Layer

  25. obtains requested file from server’s file system Building a simple Web server (cont’d) If (tokenizedLIne.nextToken().euals(“GET”)) { fileName = tokenizedLine.nextToken(); if (fileName.startsWith(“/”) == true ) fileName = fileName.substring(1); File file = new File (fileName); int numOfBytes = (int) file.length(); FileInputStream inFile = new FileInputStream (fileName); byte[] fileInBytes = new byte[numOfBytes]; inFile.read(fileInBytes); 2: Application Layer

  26. creates HTTP response message: header lines sends response to client Building a simple Web server (cont’d) outToClient.writeBytes(“HTTP/1.0 200 Document Follows\r\n”); if (fileName.endsWith(“.jpg”)) OutToClient.writeBytes(“Content-Type: image/jpeg\r\n”); if (fileName.endsWith(“.gif”)) OutToClient.writeBytes(“Content-Type: image/gif\r\n”); OutToClient.writeBytes(“Content-Length: “ + numberOfBytes + “\r\n”); OutToClient.writeBytes(“\r\n”); 2: Application Layer

  27. creates HTTP response message: + file sends response to client after creating server, you can request file using a browser Building a simple Web server (cont’d) OutToClient.write(fileInBytes, 0, numberOfBytes); connectionSocket.close(); } else System.out.println(“Bad Request Message”); } } 2: Application Layer

  28. a multi-threaded Web server: processing multiple simultaneous service requests in parallel. Nonpersistent HTTP Processing real HTTP request and response messages Follow the instructions given Supply the missing code Test your server Project #1: Building a Multi-Threaded Web Server 2: Application Layer

More Related