1 / 24

Introduction to Java Network Programming and HTTP

Introduction to Java Network Programming and HTTP. FG Telekooperation Dirk Bradler, Julian Schröder-Bernhardi. Outline. Hypertext Transfer Protocol – HTTP Different versions Requests and responses Java network programming Opening sockets in Java Reading and writing with sockets.

nardo
Download Presentation

Introduction to Java Network Programming and HTTP

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. Introduction to Java Network Programming and HTTP FG Telekooperation Dirk Bradler, Julian Schröder-Bernhardi :

  2. Outline • Hypertext Transfer Protocol – HTTP • Different versions • Requests and responses • Java network programming • Opening sockets in Java • Reading and writing with sockets :

  3. HyperText Transfer Protocol – HTTP • Origins in early 1990’s • Three versions of HTTP • HTTP/0.9 (name not official) • The original protocol, never standardized • Very simple, only one type of requests • HTTP/1.0 • First “real” HTTP protocol version, RFC 1945 • Widely used by the first browsers • Still quite a simple protocol • HTTP/1.1 • Current standardized version, RFC 2616 • Extends HTTP 1.0 in many ways, backwards compatible :

  4. HTTP Connections • Non-persistent connections (HTTP/1.0) • Use a new TCP connection for each object • Example: HTML page with 10 images  11 TCP conns! • Performance not good (TCP 3-way handshake, 2 extra RTTs) • TCP slow start for each connection • Persistent connections (HTTP/1.1) • Keep connection open for several requests  no slow start • Issue new request when old download finishes (extra RTT!) • No slow start, except at very beginning • Further improvement: Pipelining • Send several requests without waiting for reply • Continuous download! • Most efficient, but not always widely supported/implemented :

  5. HTTP Requests • Example request: GET /index.html HTTP/1.1 Host: www.google.com Connection: close User-Agent: Mozilla 1.6 <CR><LF> • Request line, header lines, possible body :

  6. HTTP Request Format <sp> URL <sp> Version <cr> <lf> Method Request line Header field :<sp> Value <cr> <lf> ... Header lines Header field :<sp> Value <cr> <lf> <cr> <lf> Entity body :

  7. HTTP Requests • Method: GET, POST, HEAD, ... (see RFC 2616) • Normal requests GET • Retrieving web pages, images, etc. • Simple forms also processed with GET • Entity body in POST • POST used for complicated forms (lot of info to handle) • Contents of form in entity body • Parameters from forms separated by & • Example: field1=value1&field2=value2&... :

  8. HTTP Responses • Example response: HTTP/1.1 200 OK Date: Thu, 06 Aug 1998 12:00:15 GMT Connection: close Server: Apache/1.3.0 (Unix) Last-Modified: Mon, 22 Jun 1998 09:23:24 GMT Content-Length: 6821 Content-Type: text/html <CR><LF> (data data data data data) • Status line, header lines, requested document :

  9. The BufferedReader Problem <sp> Status <sp> Phrase <cr> <lf> Version Status line Header field :<sp> Value <cr> <lf> ... Header lines Header field :<sp> Value <cr> <lf> <cr> <lf> Entity body :

  10. HTTP Responses • Status code gives result • Phrase for humans, only code is important! • Typical status codes with standard phrases: • 200 OK: Everything went fine, information follows • 301 Moved Permanently: Document moved, new location in Location-header in response • 400 Bad Request: Error in request • 404 Not Found: Document does not exist on server • 505 HTTP Version Not Supported: Requested protocol version not supported by server :

  11. More about HTTP • Above describes basic HTTP/1.0 • Read RFC for information about different header fields and their meaning • HTTP/1.1 adds lot of new headers and features :

  12. Java Network Programming • Network programming in Java in general much easier than in other languages... • ...except some advanced things which are harder  • E.g. setting socket options • Java supports both TCP and UDP sockets • Many different ways to read/write sockets • Differentiates between text and binary  • Often several correct ways to handle socket • TIMTOWTDI: There Is More Than One Way To Do It :

  13. Network Programming Guidelines • Robustness Principle • Be conservative in what you do; be liberal in what you accept from others. • Key ingredient for interoperability • Reformulation • Follow the standards for things you send • You may receive things that does not fully follow the standard • But: If you receive crap, you should return a well-defined error, don‘t try to understand it :

  14. Using TCP Sockets • Client side: • Socket sock = new Socket(host, port); • String host = host to contact • Host can also be InetAddress instead of String • int port = port to be used (e.g. 80 for HTTP) • Server side • ServerSocket sock = new ServerSocket(port); • Listen for incoming connections • Socket client = sock.accept(); :

  15. Using UDP Sockets • Same for client and server • DatagramSocket sock = new DatagramSocket(); • For server, give port number as argument • Send packets with send() • Receive packets with receive() • UDP packets implemented in DatagramPacket-class :

  16. Reading and Writing TCP Sockets • Socket has InputStream and OutputStream • Need to wrap other streams around them • Some wrappers implement buffers • Java has many different I/O Streams • See Java API for others (e.g., reading files) • Relevant for sockets: • InputStreamReader, OutputStreamWriter • BufferedReader, BufferedWriter • DataInputStream, DataOutputStream :

  17. Reading from a Socket • Prossible code: InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); • Read text by calling br.readLine() • Can be used only for reading text! • Possible solution: Implement a Reader on your own without buffering to mix and match text and binary data! :

  18. Writing to a Socket • Typical code OutputStream os = socket.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw); • Write by calling one of many write()-functions • See the different classes for different possibilities • Can also write directly to OutputStream • For Streams, Strings need to be converted to bytes with getBytes() • Writer are only for text output! :

  19. DataInputStream • DataInputStream can read binary data from socket • Also can send primitive data types • Typical code InputStream is = socket.getInputStream(); DataInputStream dis = new DataInputStream(is); • Read binary data with read() (see API for details) • Bonus functionality: Read text with readLine() • But: deprecated (does not work correctly in some cases), use in Praktikum results in a worse grade :

  20. DataOutputStream • DataOutputStream can be used to write • Typical code: • OutputStream os = socket.getOutputStream(); • DataOutputStream dos = new DataOutputStream(os); • DataOutputStream can also write text and binary • Has writeBytes()-function  no need for String.getBytes() :

  21. Differences Between Output Streams?!? • What is the difference between DataOutputStream and normal OutputStream wrapped with BufferedWriter? • Answer: There is almost no difference in practice • Some subtleties: • Possible problems with conversion between 8-bit and 16-bit characters (e.g., DataInputStream.readLine()) • Possible text/binary data issues • Possible problems with buffering (use flush()) • dos.writeBytes(str) vs. bw.write(str.getBytes()) • No “correct” way, use either as long as it works • Be careful not to get confused! :

  22. Assignment 1 • Java Network Programming :

  23. Assignment Details • Assignment sheet on the Web page of the Praktikum • Milestones: • TCP client and server • Simple Web server • Web server improvements • Important: pay attention to error-semantics, be conservative (i.e. follow the RFC) in what you send • Deadline for returning: 09.05.2007 at 15:00 • To be returned: Code with comments, test cases used • Send at bradler@tk.informatik.tu-darmstadt.de :

  24. Questions? :

More Related