Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.

Similar presentations


Presentation on theme: "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."— Presentation transcript:

1 CSCE 515: Computer Network Programming Chin-Tser Huang huangct@cse.sc.edu University of South Carolina

2 3/25/20042 Application Layer Handle details of network applications Common applications supported by most implementations HyperText Transfer Protocol (HTTP) telnet and rlogin File Transfer Protocol (FTP) Simple Mail Transfer Protocol (SMTP) Simple Network Management Protocol (SNMP)

3 3/25/20043 Resources on Web Identified with URL syntax protocol://host/resource Two types of resources Static resources: files like HTML documents, text files, images, audio files, video files Dynamic resources: result of a request to a URL that represents a program or database access

4 3/25/20044 HyperText Transfer Protocol An application-layer protocol that supports MIME message exchanging between Web clients and Web servers or proxies HTTP is a stateless protocol Each client request is serviced by server independently of any other requests No info about a client is stored in server after connection is closed Latest version is HTTP/1.1 (RFC 2616)

5 3/25/20045 HTTP Conversation HTTP conversation consists of a client request and a server response over a single TCP connection Client initiates by sending an HTTP request to server Server fulfills the request and closes connection Newer version of HTTP supports multiple transactions over a single connection (keepalive)

6 3/25/20046 HTTP Requests HTTP request contains information about a resource on server and the action client wishes server to perform on the resource An example of request line GET /document.html HTTP/1.0 First element is method Second element is request URI Third element is version

7 3/25/20047 GET Request Simple get request GET /document.html[CRLF] Full get request GET /document.html HTTP/1.0[CRLF] [LF] Full get request with headers GET /document.html HTTP/1.0[CRLF] User-Agent: Surfer/1.01 libhttp/0.1[CRLF] If-Modified-Since: Thu, 25 Mar 2004 17:30:00 GMT[CRLF] [LF]

8 3/25/20048 POST Request Allow client to include data in a request To submit a large body of information to CGI script or to upload a file to be processed by a target script content-length header is mandatory POST /cgi-bin/code.cgi HTTP/1.0[CRLF] Content-type: application/octet-stream[CRLF] Content-length: 2048[CRLF] [LF] body

9 3/25/20049 HEAD Request Return only headers of resource, or headers of error message if an error occurs Find out info about a document without expense of transmitting the document HEAD /index.html HTTP/1.0[CRLF] [LF]

10 3/25/200410 Request URIs and Virtual Paths URI consists of a virtual path and an optional query /cgi-bin/code.cgi?query Virtual path is independent of client or server OS and is translated using aliasing rules to prevent external users from accessing arbitrary files Query string will be supplied to dynamic resources index.html document.html cgi-bin cgi.exe tmp win2000 apache html C: My Drive http://localhost/document.html http://localhost/cgi-bin/cgi.exe

11 3/25/200411 HTTP Responses Simple response body Full response HTTP/1.0 200 OK[CRLF] [LF] body Full response with headers HTTP/1.0 200 OK[CRLF] Server: Apache/1.2b11[CRLF] Content-type: text/html[CRLF] Content-encoding: x-gzip[CRLF] [LF] body

12 3/25/200412 HTTP Response Codes CodeMeaning 200OK 201Created 202Accepted 204No Content 301Moved Permanently 302Moved Temporarily 304Not Modified 400Bad Request 401Unauthorized 403Forbidden 404Not Found 500Internal Server Error 501Not Implemented 502Bad Gateway 503Service Unavailable

13 3/25/200413 A Web Client Example Use a socket to download Web pages Sit in a loop waiting for user to enter URLs For each URL, create a GrabPage object that attempts to connect to Web server and download specified page If successful, display page contents on screen

14 3/25/200414 GrabPage.java /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.net.*; import java.io.*; public class GrabPage { // public GrabPage (String textURL) throws IOException … // public void grab () throws IOException… // public static void main (String[] args) throws IOException … }

15 3/25/200415 Constructor GrabPage public GrabPage (String textURL) throws IOException { dissect (textURL); } // protected void dissect (String textURL) throws MalformedURLException …

16 3/25/200416 Method dissect protected String host, file; protected int port; protected void dissect (String textURL) throws MalformedURLException { URL url = new URL (textURL); host = url.getHost (); port = url.getPort (); if (port == -1) port = 80; file = url.getFile (); }

17 3/25/200417 Method grab public void grab () throws IOException { connect (); try { fetch (); } finally { disconnect (); } // protected void connect () throws IOException … // protected void fetch () throws IOException … //

18 3/25/200418 Method connect protected Writer writer; protected BufferedReader reader; protected void connect () throws IOException { Socket socket = new Socket (host, port); OutputStream out = socket.getOutputStream (); writer = new OutputStreamWriter (out, "latin1"); InputStream in = socket.getInputStream (); Reader reader = new InputStreamReader (in, "latin1"); this.reader = new BufferedReader (reader); }

19 3/25/200419 Method fetch protected void fetch () throws IOException { writer.write ("GET " + file + " HTTP/1.0\r\n\n"); writer.flush (); PrintWriter console = new PrintWriter (System.out); String input; while ((input = reader.readLine ()) != null) console.println (input); console.flush (); }

20 3/25/200420 Method disconnect protected void disconnect () throws IOException { reader.close (); }

21 3/25/200421 Method main public static void main (String[] args) throws IOException { Reader kbd = new FileReader (FileDescriptor.in); BufferedReader bufferedKbd = new BufferedReader (kbd); while (true) { String textURL; System.out.print ("Enter a URL: "); System.out.flush (); if ((textURL = bufferedKbd.readLine ()) == null) break; try { GrabPage grabPage = new GrabPage (textURL); grabPage.grab (); } catch (IOException ex) { ex.printStackTrace (); continue; } System.out.println ("- OK -"); } System.out.println ("exit"); }

22 3/25/200422 Next Class A Web server example URL classes Read JNP Ch. 19 Project 4 will be passed out


Download ppt "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."

Similar presentations


Ads by Google