720 likes | 752 Views
This lecture delves into network programming concepts such as sockets, I/O in Java, filtering streams, buffering, and dynamic loading. Explore HTTP protocols, server testing with telnet, and practical socket operations. For more details, visit the provided resource links.
E N D
Networking Part II May 17, 2017 JDP Networking Part II
Thought for the Day Networking (on UNIX) tries to emulate the notion of a sequential file. JDP Networking Part II
Last Lecture • Topic: Network programming • What is a network: introduction and basic concepts • Hosts, client/server, naming, ports • Sockets • Examples JDP Networking Part II
This Lecture Topics: • Sockets • I/O in Java • OutputStream and InputStream • Filter streams • Readers and writers • Buffering • I/O and Networking • Dynamic Class Loading • Applets • Datagrams • Multicast • NIO and Networking JDP Networking Part II
This Lecture Reading see http://condor.depaul.edu/dmumaugh/JDP • Harold: Chapters 1-4, 12-14; pp 53-63; pp. 497, 525-542 • Pitt: Chapters 3, 9, 11, 5, 13 • Core Java, Vol. 1: pp. 516-550 • Core Java, Vol. 2: pp. 1-64, 169-217, • Technology Brief TCP • Technology Brief HTTP • Sun's (Oracle) API Javadoc <http://download.oracle.com/javase/8/docs/api/index.html> • Java Streams Basics • Java in a Nutshell: Input and Output Streams JDP Networking Part II
A More Complex Example Simple Single File HTTP Server JDP Networking Part II
HTTP HTTP - Hyper Text Transmission Protocol • See pp 53-63 of Harold • See Technology Brief: HTTP • Newest is HTTP 1.1, look up the RFC for that protocol. JDP Networking Part II
HTTP HTTP - Hyper Text Transmission Protocol • Three main commands GET, PUT, POST • Commands consist of • Command and optional header information • Blank line • Payload (data); format specified by header • Response consists of • Header information • Blank line • Payload (data); format specified by header JDP Networking Part II
HTTP HTTP/1.1 is the stable version of the HTTP protocol. Earlier HTTP/1.0 has fewer "commands". Requests: GET, POST, HEAD Response: HTTP/1.1 200 OK or 301 Moved Permanently or 404 Not Found or ... Header lines: Connection: close Accept-Language: en ... JDP Networking Part II
Typical HTTP Session $ telnet condor.depaul.edu 80 GET /dmumaugh/index.html HTTP/1.1 Host: condor.depaul.edu <blank line> HTTP/1.1 200 OK Date: Wed, 02 Apr 2003 20:35:34 GMT Server: Apache/2.0.39 (Unix) PHP/4.2.1 Last-Modified: Wed, 02 Apr 2003 05:06:49 GMT ETag: "29261-103d-e21d0c40" Accept-Ranges: bytes Content-Length: 4157 Content-Type: text/html; charset=ISO-8859-1 <blank line> Blah…Blah…Blah… JDP Networking Part II
Error Message HTTP/1.1 404 Not Found Date: Wed, 02 Apr 2003 20:33:34 GMT Server: Apache/2.0.39 (Unix) PHP/4.2.1 Vary: accept-language Accept-Ranges: bytes Content-Length: 804 Content-Type: text/html; charset=ISO-8859-1 <blank line> Message explaining that server could not find the file. JDP Networking Part II
Example: A HTTP Server • Single file server • Very limited, serves one filehttp://condor.depaul.edu/dmumaugh/JDP/examples/lect02/SingleFileHTTPServer.java JDP Networking Part II
Testing Servers Using telnet • The telnetprogram is invaluable for testing servers that transmit ASCII strings over Internet connections • Web servers • Mail servers • Usage: • unix> telnet <host> <portnumber> • Creates a connection with a server running on <host> and listening on port <portnumber>. • See slide 10 for example. JDP Networking Part II
Network Programming JDP Networking Part II
Network Programming • General theory • Create and/or open a socket • Convert a socket to a standard Java I/O class • Input stream • Output stream • Use standard Java I/O for all operations • Works for "normal" TCP connections • Does not work for UDP • Need something specific and lower level JDP Networking Part II
Sockets JDP Networking Part II
The Java Socket Class Sockets can • Connect to a remote machine • Send data • Receive data • Close a connection • Bind to a port • Listen for incoming connection • Accept connections from remote machines on a bound port JDP Networking Part II
The Java Socket Class The Socket class supports the • Connect to a remote machine Socket is = new Socket(hostname, portnumber); The Java ServerSocket Class supports • Open for connections ServerSocket ss = new ServerSocket(portnumber); JDP Networking Part II
More on Sockets • Our examples ignored sockets except to open them, now more details • What can we do with a socket? • bind(SocketAddress, bindpoint)Binds the socket to a local (port) address. • void close()Closes this socket. • InputStream getInputStream()Returns an input stream for this socket. [Note well!] • OutputStream getOutputStream()Returns an output stream for this socket. [Note well!] JDP Networking Part II
More on Sockets • What can we do with a socket? • SocketAddress getRemoteSocketAddress()Returns the address of the endpoint this socket is connected to, or null if it is unconnected. • Methods for SocketAddress • String getHostName()Gets the hostname. • int getPort()Gets the port number. • See the Sockets API for more details JDP Networking Part II
Network I/O • So the question is now that we have a connection, what do with it and how? JDP Networking Part II
More on Sockets Now what? • To make a connection: • Construct a socket • Convert to InputStream and/or OutputStream • Read and/or write • Flush • Close • To make a server • Construct a ServerSocket • Bind to a port • Accept a connection • Convert to InputStream or OutputStream • Read/Write, flush and close (as above) JDP Networking Part II
More on Sockets To make a connection: • Construct a socket Socket theSocket = new Socket(hostname, port); • Convert to InputStream or OutputStream rawIn = theSocket.getInputStream(); rawOut = theSocket.getOutputStream(); • Read and/or write int abyte = rawIn.read(); rawOut.write(something); • Flush rawOut.flush(); • Close rawOut.close(); rawIn.close(); theSocket.close(); JDP Networking Part II
More on Sockets To make a server • Construct a ServerSocket* ServerSocket server = new ServerSocket(); • Bind to a port* server.bind(8000); • Listen for connectionserver.listen() • Accept a connection for(;;) { Socket client = server.accept(); • Read/Write, flush and close (as in previous slide) String response = "Blah, blah!\r\n" OutputStream os = client.getOutputStream(); os.write(response.getBytes() ); os.flush(); client.close(); } JDP Networking Part II
Java I/O JDP Networking Part II
Files • There are two types of files: • The standard input, output and error: System.in, System.outandSystem.err • Regular files • Files are supported by the File class • Most the time you can use a convenience class such as FileReader instead • Or FileWriter • Or PrintWriter JDP Networking Part II
I/O in Java • Java has four basic classes for I/O: • OutputStream: sends binary data [Note previous!] • InputStream: receives binary data [Note previous!] • Writer: sends text data • Reader: receives text data • Everything else (sockets, files, System.out etc. are subclasses of these). • Why two of everything (binary vs. text)? JDP Networking Part II
OutputStream • Where do output streams come from? • System.out and • System.err • FileOutputStream and • Socket.getOutputStream()[Note!] • Where do these streams write to? • Most other output streams are built on top of these basic ones. JDP Networking Part II
OutputStream • The methods of an output stream are: void write (int b) throws IOException void write (byte[] data) throws IOException void write (byte[] data, int offset, int length) throws IOException void flush () throws IOException void close () throws IOException • What do these methods do? JDP Networking Part II
OutputStream What does this code try to do? What is wrong with it? How can we fix it? 1: void printHelloWorld(final OutputStream out) 2: { 3: final String hw = "Hello, World\n"; 4: for(int i=0; i < hw.length (); i++) 5: { 6: out.write (hw.charAt (i)); 7: } 8: out.close (); 9: } JDP Networking Part II
OutputStream Answer: 19: voidprintHelloWorld(final OutputStream out){ 20: try{ 21: final String hw ="Hello, World\n"; 22: for(int i=0; i < hw.length(); i++){ 23: out.write(hw.charAt(i)); 24: } 25: out.flush(); 26: }catch(final IOException ex){ 27: // Some error handling code goes here 28: System.err.println("Oops 1: "+ ex); 29: }finally{ JDP Networking Part II
OutputStream 29: }finally{ 30: // Always a good idea to close streams 31: try{ 32: out.close(); 33: }catch(final IOException ex){ 34: // Some more error handling code goes here 35: System.err.println("Oops 2: "+ ex); 36: } 37: } 38: } JDP Networking Part II
InputStream • Where do input streams come from? • System.in • FileInputStream and • Socket.getInputStream()[Note!] • Where do these streams read from? • Most other input streams are built on top of these basic ones. JDP Networking Part II
InputStream • The important methods of an input stream are: int read() throws IOException int read(byte[] input) throws IOException int read(byte[] input, int offset, int length) throws IOException int available() throws IOException void close() throws IOException • What do these methods do? • Why does read() return an int rather than a byte? • What happens at the end of file? JDP Networking Part II
Reading data from a stream ... Socket ss = ... InputStream sin = ss.getInputStream(); 01: StringBuffer sb =new StringBuffer(); 02: line =""; 03: int achar =0; 04: achar = sin.read(); 05: while(achar !=-1) 06: { 07: sb.append((char) achar); 08: achar = sin.read(); 09: } 10: line = sb.toString(); Problems with this code? JDP Networking Part II
InputStream What does this code try to do? What is wrong with it? How can we fix it? 01: byte[] get128 (final InputStream in) { 02: finalbyte[] result = newbyte[128]; 03: for(int bytesRead = 0; bytesRead < 128;) 04: { 05: finalint read = in.read (result, 06: bytesRead, 128-bytesRead); 07: bytesRead = bytesRead + read; 08: } 09: return result; 10: } JDP Networking Part II
InputStream Answer: 19: byte[]get128(final InputStream in){ 20: finalbyte[] result =newbyte[128]; 21: try{ 22: for(int bytesRead =0; bytesRead <128;){ 23: finalint read = in.read(result, bytesRead, 128-bytesRead); 24: // Check to see if we hit the end of the stream 25: if(read >0){ 26: bytesRead = bytesRead + read; 27: }else{ 28: // End of stream! 29: break; 30: } 31: } 32: }catch(IOException ex){ JDP Networking Part II
InputStream 32: }catch(IOException ex){ 33: System.err.println("Oops: "+ ex); 34: }finally{ 35: return result; 36: } 37: } JDP Networking Part II
Stream Hints • Remember to flush output buffers. • Remember IOExceptions! (Expect the majority of your code to be exception handling.). • Remember that exception handlers can raise exceptions! • Use finally to make sure clean-up is always executed. • Use close to close any streams you open. • Remember to flush output streams before closing them. JDP Networking Part II
Filter streams • Raw InputStreams and OutputStreams aren't much use! • Often we want to add functionality to an existing stream. • For example: • A BufferedInputStream which adds buffering to an input stream. • A CipherInputStream which decrypts an encrypted input stream. • A GZIPInputStream which uncompresses a compressed input stream. • Streams which read from other streams are called filter streams in Java. JDP Networking Part II
Filter streams • Filter streams can be chained together: final FileInputStream rawIn = new FileInputStream ("foo.des.gz"); final InputStream processedIn = new GZIPInputStream ( new CipherInputStream ( new BufferedInputStream ( rawIn), desCipher)); • This is an example of dataflow programming. JDP Networking Part II
Readers and writers • Input and OutputStreams handle raw data. • Readers and Writers handle text. • Most protocols (e.g. HTTP) and data exchange formats (e.g. XML) these days are text based. • Why? Isn't it much more efficient to send binary data? • Protocols expect Text strings; specifically TELNET format • For example: final OutputStream out = ...; // Create a new text writer with encoding UTF-8 final Writer writer = new OuputStreamWriter (out, "UTF-8"); JDP Networking Part II
Readers and writers • What is text data? • A string of characters, usually 8-bits each, that are printable • Common values for the encoding: ISO-8859-1, UTF-8 or UTF-16. (What are these?) • Traditional was ASCII 7-bit American code • Internationalization • European character sets: Latin-1 or ISO 8859 • Then came the other countries: Russian, Hebrew, Arabic, Chinese, Japanese and others • UNICODE: a 8 or 16 bit code for all languages: UTF-8 and UTF-16 JDP Networking Part II
Readers and writers • Two very useful methods: • String BufferedReader.readLine () • PrintWriter.println (String msg) • for example from a Socket s: PrintWriter writer = new PrintWriter (new OutputStreamWriter (s.getOutputStream (), "UTF-8")); writer.println ("GET /index.html HTTP/1.1"); writer.flush (); • and: BufferedReader reader = new BufferedReader (new InputStreamReader (s.getInputStream (), "UTF-8")); String request = reader.readLine (); • Connect these up and what happens? JDP Networking Part II
Readers and writers Unfortunately, there's a number of gotchas with Sun's (Oracle) code: • PrintWriter doesn't throw any IOExceptions, you have to do your error-handling the old-fashioned C way (blech). • Mixing text and binary is very hard! [Note well!!] • BufferedReader and PrintWriter can't get their act together about what a new line is! JDP Networking Part II
Readers and writers For example, on a Mac sending on a socket (non-Java): writer.println ("A message"); and on any Java machine receiving on a socket: final String line = reader.readLine (); this will deadlock! Why? • What is a line of text? • In MS/DOS and Windows: sequence of characters ending in "\r\n" • In LINUX and UNIX: a sequence of characters ending in "\n" • In MAC OS: a sequence of characters ending in "\r" • What is a line in Java? [As in println] • a sequence of characters ending in "\n" • What is a line on the Internet? What is TELNET protocol? • sequence of characters ending in "\r\n" JDP Networking Part II
Buffered Readers and Writers • What is buffering? • How does it work? • BufferedReaders have buffering. • Is this a problem? Why? • BufferedWriters have buffering. • Is this a problem? Why? • How do you switch from Buffered I/O to Unbuffered I/O? • Why do you want to? [Hint: see note]. • Is it possible? • No! JDP Networking Part II
Network Programming JDP Networking Part II
Network Programming Now what? • Normally, we do not do basic read/write on streams. • We encapsulate in a • BufferedInputStream • BufferedOutputStream • BufferedReader • BufferedPrinter • BufferedWriter • This provides a more powerful set of operations on the data. JDP Networking Part II
Network Programming • This allows us to use methods such as • readLine() • println • See slide 44 for an vague idea. • The basic methodology of network programming is to send data to the server (using print and println) • Then receive the result (using readLine() ) • You parse the result and take action. • Most of the work is in the parsing of possible responses. • Occasionally one has problems with mixed data streams (partly text and partly binary). See slide 47. JDP Networking Part II