Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | raphael-hardy |
View: | 62 times |
Download: | 0 times |
:
Telecooperation
Technische Universität Darmstadt
Copyrighted material; for TUD student use only
Introduction to Java Network Programming and HTTP
FG TelekooperationProf. Jussi KangasharjuFernando Lyardet, Dirk Bradler, Julian Schröder-Bernhardi
:
Prof. Dr. M. MühlhäuserTelekooperation
©
Outline
• Hypertext Transfer Protocol – HTTP– Different versions– Requests and responses
• Java network programming– Opening sockets in Java– Reading and writing with sockets
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
<cr> <lf>
Header field
:<sp> <cr> <lf>Value
<sp>
HTTP Request Format
Method <sp>
URL Version
<cr> <lf>
...
Entity body
Header lines
Request line
Header field
:<sp> <cr> <lf>Value
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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&...
:
Prof. Dr. M. MühlhäuserTelekooperation
©
HTTP Responses
• Example response:
HTTP/1.1 200 OKDate: Thu, 06 Aug 1998 12:00:15 GMTConnection: closeServer: Apache/1.3.0 (Unix)Last-Modified: Mon, 22 Jun 1998 09:23:24 GMTContent-Length: 6821Content-Type: text/html<CR><LF>(data data data data data)
• Status line, header lines, requested document
:
Prof. Dr. M. MühlhäuserTelekooperation
©
<cr> <lf>
Header field
:<sp> <cr> <lf>Value
<sp>
The BufferedReader Problem
Version <sp>
Status
Phrase <cr> <lf>
...
Entity body
Header lines
Status line
Header field
:<sp> <cr> <lf>Value
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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();
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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!
:
Prof. Dr. M. MühlhäuserTelekooperation
©
Writing to a Socket
• Typical codeOutputStream 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!
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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()
:
Prof. Dr. M. MühlhäuserTelekooperation
©
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!
:
Prof. Dr. M. MühlhäuserTelekooperation
©
Assignment 1
• Java Network Programming
:
Prof. Dr. M. MühlhäuserTelekooperation
©
Assignment Details
• Assignment sheet on the Web page of the Praktikum
• Milestones:1. TCP client and server2. Simple Web server3. 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 [email protected]
:
Prof. Dr. M. MühlhäuserTelekooperation
©
Questions?