Lab 2C Primer I/O in Java Sockets Threads More Java Stuffs.

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Lecture 15: I/O and Parsing
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
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.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
File Handling and Serialization CSIS 3701: Advanced Object Oriented Programming.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
1 Multithreading. 2 Threads Program units that execute independently; multiple threads run “simultaneously” Virtual machine executes each thread for short.
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.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Practical Session 11 Multi Client-Server Java NIO.
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.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
CS 11 java track: lecture 6 This week: networking basics Sockets Vectors parsing strings.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
Chapter 15 Text Processing and File Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Lab 2 Primer Assignment 3 Structure File I/O More parsing and HTTP Formatting.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
Practical Session 11 Multi Client-Server Java NIO.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Lecture 5 I/O and Parsing
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.
Java Server Programming Web Interface for Java Programs.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
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.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
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.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
Information and Computer Sciences University of Hawaii, Manoa
Threads in Java Two ways to start a thread
OO Design and Programming II I/O: Reading and Writing
Java Exceptions and I/O
Client-server Programming
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Streams and File I/O Chapter 14.
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
CSCD 330 Network Programming
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Clients and Servers 13-Sep-19.
Presentation transcript:

Lab 2C Primer I/O in Java Sockets Threads More Java Stuffs

Server Flow 1. Start the server with a port if specified (if not, start the server with a default port of 7000) 2. Create a Polling Socket 3. Start the server running “forever” and wait for a client to connect 4. Open I/O to client 5. When connected, send a message to allow client to know they are connected 6. Accept a request from the client 7. Respond to the client’s request 8. Client port and I/O are closed * Servers run forever

Input/Output in Java In Java, there are 2 types of major I/O streams classes we are looking at – Reader/Writer: ASCII text – Streams: binary For our assignments, we could either read binary strings and convert them to ASCII or we could simply use the provided writer classes We will use buffered connections for efficiency and coding elegance – Buffers allow packets to be sent when a buffer is filled, not on a byte- by-byte basis – Buffers allow us to send an object to a I/O stream instead of a bit

I/O Classes of Interest InputStreamReader – Takes an input stream from an I/O port and interprets it as ASCII OutputStreamReader – Sends an output stream to an I/O port as ASCII BufferedReader – Takes in InputStream and adds useful methods and a buffer to the stream PrintWriter – Takes an OutputStream and adds useful methods and an output buffer to the stream

Sockets in Java Socket support is provided through: java.net.* – import java.net.* We have 2 types of sockets we are interested in: – ServerSocket class – Socket class (client sockets)

ServerSocket ServerSockets are the sockets used to poll a specific port for incoming connections Steps to using a ServerSocket 1. Create a new ServerSocket object ServerSocket ss = new ServerSocket(port); 2. Wait until a connection has been received This requires blocking statement until a connection is received 3. Upon receiving a connection request, a new socket must be opened Socket client = ss.accept();

Socket I/O Provided by the methods Socket.getInputStream() and Socket.getOutputStream() Can be used in conjunction with any other stream sub-class – E.g. PrintWriter and BufferedReader

Example Code for Socket I/O BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true); This could also be accomplished in 2 steps each; e.x.: InputStreamReader temp = new InputStreamReader(client.getInputStream())); BufferedReader in = new BufferedReader(temp);

Using Socket I/O Once BufferedReader and PrintWriter objects are created, they are used with the following methods: –.readLine(); –.println(String);

Full Example of Streams BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true); in.readLine(); out.println(“Hello”); in.close(); out.close(); client.close();

Parsing Arguments from the Command Prompt Arguments from the prompt are passed into your main method through the string array args args[] is an array of space separated Strings – Strings are stored in the array from the leftmost argument to the rightmost – Since each element of args is type String, you can’t directly pass that value into your server for use – Java has built in functions for converting data types To convert from String to Int, the method to use is: Integer.parseInt(String)

String Parsing Can’t test for String equality with if (string == “hello”) Methods provided by Java to do String comparisons – String.equals(String)  case sensitive – String.equalsIgnoreCase(String)  case insensitive

Try/Catch Statements Exceptions are a java element used for troubleshooting and catching conditions that could “break” a program – e.x.: null-pointer exceptions, passing the wrong data type, etc. Useful around any code block with I/O, as that’s where mistakes can be made – The Java compiler enforces try/catch blocks in newer versions by default

Try/Catch Statements Idea is: first you try to execute some code – If it fails you get an exception instead of the program crashing Code in the catch block allows you to keep running the program, but deal with the error – If it works you simply exit the try portion and continue executing the program

Try/Catch Example BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in)); int string; try { string = keyIn.readLine(); System.out.print(string); } catch (Exception e) { System.out.print(“That wasn’t in Integer!”); }

Exception Types Different classes and methods throw different exception types – e.x.: PrintWriter(String, String) throws FileNotFoundException and UnsupportedEncodingException Can catch specific exceptions and execute different code based on them Can catch all exceptions simply with “catch (Exception e)”

Using Exceptions in Lab 2 You can use exceptions to parse the argument entered for the port You should wrap your server’s executable code itself either in a single or series of try statements to ensure it won’t crash – If it does, you should catch the exception and alert the system that the server crashed

Other Useful Classes Classes that have date objects – java.util.Calendar – java.util.Date (partially depreciated) – java.text.simpleDateFormat

Hints You can either try parsing the first argument of args in main and passing in a port by hand into the server object; OR You can use 2 constructors for both cases (argument passed in and no argument) Use the Java API document if you aren’t sure what you need or how to manipulate and object I don’t care if you use depreciated Date types in this assignment

Hints Look through the java.net.* and java.io.* packages – Focus on SocketServer, Socket (and.accept()), BufferedReader, and BufferedWriter Look at the simpleDateFormat and Date classes

Lab 2 Protocol-Code Structure getHead(): StringBuffer – Easy: Loops through receiving data and appending it into a StringBuffer until the end of the header section is found What must be added to each line? getMethod(): String – This parses the method out of the request. – Easy to do, due to location of method in the Request

Lab 2 Protocol-Code Structure getLenth(StringBuffer): int – Why is this needed? – Finds a String representing a number, parses it and returns it getBody(int): StringBuffer – Uses a Integer to specify how many characters to read – Must cast each byte you read() as a (char)

getLength() Use String.indexOf(String) to find what you are looking for: String temp = new String(“This is a test String for index”); int i = temp.indexOf(“String”); i = 16 Now you know the start of the header you are looking for, but how do you find the value? – Use a loop and iterate through with temp.charAt(i) until you find the end of the line as a character – Use substring to parse only the section of string you want – Use the same Integer.parseInt used in Assignment 1

getLength() //Find “for” int j; String temp = new String(“This is a test String for index”); int i = temp.indexOf(“String”); for(j=i+7;;j++){ if (!(temp.charAt(i)==' ')) j++; else break; } String answer = temp.substring(i+7, j); Why do we need the +7 for the index (i+7)?

getBody() StringBuffer temp = new StringBuffer(""); for(i=0;i<length;i++){ temp.append((char)netIn.read()); } Why do we have to cast the input as (char)? Why can’t we use readLine()?

Threads We know a processor can only execute one program at a time – One process may run at a time – To run multiple programs (processes), the CPU must run one for a certain amount of time, save the state of the program, load another executing program and so on One program can have multiple modules that need to be execute “at the same time” for different functions. – THREADS!

Threads A parent process can spawn children – A child must complete before parent regains control: except it can open more children Like a stack, threads only communicate with their children or parents (sequential) Threads are treated like independent processes – Each thread gets a slice of the processor/resources Warning: when different threads access same resources, must ensure they don’t corrupt data. – Who gets to read/write from/to a file?

Threads - Example Outlook Express TCP/IP Stack Routines GUI POP, IMAP Protocol I/O Routines Disk I/O

Using Threads in Java There are 2 ways – Implement Interface Runnable() – Extend Class Thread() We will implement Runnable(). Why? – You can only extend a class once in your hierarchy, but can implement as many Interfaces as you want

Threading in Java – Running Class public class ThreadTest implements Runnable{ public ThreadTest() { … } Public void run() { … }

Threading in Java – Calling Class public void run(){ ServerSocket ss = new ServerSocket(port); ThreadTest client = new ThreadTest(ss.accept()); Thread t = new Thread(client); t.start(); }

Java Items for Lab 2 StringBuffer (java.lang.StringBuffer) –.append() takes a string and keeps appending it to the end of the StringBuffer – Useful for appending a sequence of input together – Cast it to a String to do comparisons Socket.get*() – This assignment expects you to be able to get information about your connection. Look over Socket.get () in the API to see what information is available for you to receive. String.startsWith(String) – Used to test if a String begins with a certain sub-string

Items for Lab 2 String parsing – All commands should be case-insensitive – Actual input should not be directly type-cast (forced to upper or lower case), or else the message inputted with lose the intended case – String.equals(), String.equalsIgnoreCase() Can use equals() if you type-cast temporary input string to compare and keep original untouched Remember: In HTTP headers are separated from body data with a blank line.