1 / 6

Network Read/Write

Network Read/Write. Review of Streams and Files. java.io package InputStream and OutputStream classes for binary bytes Reader and Writer classes for characters Reading files: FileReader class FileReader reads 1 char at a time; use BufferedReader

carlo
Download Presentation

Network Read/Write

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. Network Read/Write

  2. Review of Streams and Files • java.io package • InputStream and OutputStream classes for binary bytes • Reader and Writer classes for characters • Reading files: FileReader class • FileReader reads 1 char at a time; use BufferedReader • readLine() returns null at end of file Example 1 FileReaderfrdr = new FileReader(“filename.txt”); BufferedReaderrdr = new BufferedReader( frdr ); String line = rdr.readLine(); rdr.close(); Example 2 BufferedReaderrdr = new BufferedReader( new InputStreamReader( System.in ));

  3. Review Output Streams • FileWriter • Create a FileWriter, write to it and close it. • You must write line separator characters FileWriterfw = new FileWriter(“filename.txt”); fw.write(“abcdef\n”); fw.close(); • PrintWriter • print() and println() will take of line separator characters. PrintWriterpw = new PrintWriter( new FileWriter(“filename.txt”)); pw.print( “abc “); pw.println( “def”); pw.close(); • Exceptions: IOException • File methods must be in a try/catch block for IOException

  4. Sockets • Data stream between client and server • Have to deal with security firewalls • Client • Create a Socket • Arguments: host address or name, listener port number • Create PrintWriterfrom the socket • Create BufferedReader from the socket • Close socket when done. • Server • Create ServerSocket (port number ) • Wait for incoming connection from client • Accept connection • A new port number is assigned to connection. • Create BufferedReader, PrintWriter using Socket • Reading and writing is typically done on separate thread so that the main thread that is listening for incoming connections is not blocked. • Close socket when done.

  5. Get Official Time from Time Server try { // Step1. Create a Socket object instance Socket s = new Socket("utcnist.colorado.edu", 13); // Step 2. Create a BufferedReaderinstance InputStreamis = s.getInputStream(); InputStreamReaderisrdr = new InputStreamReader(is); BufferedReaderrdr = new BufferedReader(isrdr); // Step 3. Repeat readLine() method until null value String line = rdr.readLine(); while (line!=null){ System.out.println(line); line = rdr.readLine(); }; // Step 4. Close the socket connection. s.close(); } catch (Exception e){ System.out.println(e.getMessage()); }

  6. Get Current Weather Socket s = new Socket("weather.yahooapis.com", 80); OutputStreamos = s.getOutputStream(); OutputStreamWriterout = new OutputStreamWriter(os); out.write("GET /forecastrss?w=2453280 HTTP/1.1\r\n"+ "Host: weather.yahooapis.com\r\n"+ "\r\n"); out.flush(); InputStreamis = s.getInputStream(); InputStreamReaderisrdr = new InputStreamReader(is); BufferedReaderrdr = new BufferedReader(isrdr); String line = rdr.readLine(); while (line!=null){ .. Do something .. line = rdr.readLine(); } s.close();

More Related