Network Read/Write. Review of Streams and Files java.io package InputStream and OutputStream classes for binary bytes Reader and Writer classes for characters.

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
Java I/O and Java Networking (Client Side) Yoshi.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
1 Fall 2005 Socket Programming Qutaibah Malluhi Computer Science and Engineering Qatar University.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Performance measurements for inter-process communication.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Two Ways to Store Data in a File Text format Binary format.
Dynamic Arrays Dynamic arrays are arrays that are re- allocated to a larger size. –See Eck Section 8.3 for details. –For instance, what happens with the.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
Sockets Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection, the client side and the server.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
File IO Basics By Dan Fleck Coming up: Data Streams.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
Introduction to Java Network Programming and HTTP
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Read and Write Files  By the end of this lab you will be able to:  Write a file in internal storage  Read a file from internal storage  Write a file.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
SOCKET PROGRAMMING.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Program Input/Output (I/O)
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
I/O Basics.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Streams and File I/O Chapter 14.
JAVA IO.
הרצאה 12: קבצים וחריגות (Exceptions)
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT
Reading and Writing Text Files
Web Design & Development Lecture 8
File Input and Output.
ECE 122 April 14, 2005.
Presentation transcript:

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 – readLine() returns null at end of file Example 1 FileReader frdr = new FileReader(“filename.txt”); BufferedReader rdr = new BufferedReader( frdr ); String line = rdr.readLine(); rdr.close(); Example 2 BufferedReader rdr = new BufferedReader( new InputStreamReader( System.in ));

Review Output Streams FileWriter – Create a FileWriter, write to it and close it. – You must write line separator characters FileWriter fw = new FileWriter(“filename.txt”); fw.write(“abc def\n”); fw.close(); PrintWriter – print() and println() will take of line separator characters. PrintWriter pw = 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

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 PrintWriter from 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.

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 BufferedReader instance InputStream is = s.getInputStream(); InputStreamReader isrdr = new InputStreamReader(is); BufferedReader rdr = 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()); }

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