CS 2113 Software Engineering

Similar documents
IT101. File Input and Output

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

FILE I/O IN JAVA. Prof. Chris Jermaine

CS 251 Intermediate Programming Java I/O Streams

I/O Streams. Object-oriented programming

CS September 2017

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

1.00 Lecture 30. Sending information to a Java program

Example: Copying the contents of a file

Darshan Institute of Engineering & Technology for Diploma Studies

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Files and Streams

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Streams and File I/O

Distributed Programming - Sockets

CSPP : Introduction to Object-Oriented Programming

Programmierpraktikum

Algorithms. Produced by. Eamonn de Leastar

CS193j, Stanford Handout #26. Files and Streams

Performing input and output operations using a Byte Stream

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Object-Oriented Programming Design. Topic : Streams and Files

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Internet Technology 2/7/2013

Events and Exceptions

Exceptions and Working with Files

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

protocols September 15,

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Lecture 11.1 I/O Streams

Java Networking (sockets)

Chapter 10. IO Streams

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Networking Basics. network communication.

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Programming Methods Dr Robert Harle

Byte and Character Streams. Reading and Writing Console input and output

Info 408 Distributed Applications programming 2 nd semester of 2017/2018 Credits: 5 Lecturer: Dr. Antoun Yaacoub

Software 1. Java I/O

Objec&ves. Review. Standard Error Streams

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

CS18000: Programming I

CS 351 Design of Large Programs Sockets Example

Chapter 12. File Input and Output. CS180-Recitation

CS2 Advanced Programming in Java note 8

I/O STREAM (REQUIRED IN THE FINAL)

Input from Files. Buffered Reader

Pieter van den Hombergh Richard van den Ham. March 17, 2018

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Active Learning: Streams

Simple Java I/O. Part I General Principles

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Java Input/Output. 11 April 2013 OSU CSE 1

Object-Oriented Programming in the Java language

TCP connections. Fundamentals of Internet Connections Objectives. Connect to an Echo port. java.net.socket

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Java Support for developing TCP Network Based Programs

Programming Languages and Techniques (CIS120)

Techniques of Java Programming: Streams in Java

Unit 9: Network Programming

pre-emptive non pre-emptive

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

CN208 Introduction to Computer Programming

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Networking Code CSCI 201 Principles of Software Development

Core Java Contents. Duration: 25 Hours (1 Month)

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

HST 952. Computing for Biomedical Scientists Lecture 8

CS 51 Laboratory # 12

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

Lecture 4: Exceptions. I/O

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Distributed Systems Recitation 2. Tamim Jabban

CSCD 330 Network Programming

Multimedia Programming

CSCD 330 Network Programming

Software Practice 1 - Socket

String temp [] = {"a", "b", "c"}; where temp[] is String array.

PIC 20A Streams and I/O

CSCD 330 Network Programming

CS11 Java. Fall Lecture 4

Transcription:

CS 2113 Software Engineering Java 6: File and Network IO https://github.com/cs2113f18/template-j-6-io.git Professor Tim Wood - The George Washington University

Project 2 Zombies Basic GUI interactions Due on Sunday!!2

Keyboard input See practice.dots.dotkey for keyboard example package dots; public class DotKeys extends JFrame implements ActionListener, KeyListener { { public DotKeys() { //... in constructor add: this.addkeylistener(this); } @Override public void keytyped(keyevent keyevent) { System.out.println(keyEvent.getKeyChar()); } // also need to implement keypressed() and keyreleased() } https://github.com/cs2113f18/template-j-5!3

Keyboard input Be careful with buttons: Keyboard input will go to UI widget currently in focus If you have a button in your window, it will be focused and may block events from reaching the JFrame Solutions: Prevent button from gaining focus: button.setfocusable(false); or Use KeyBindings class instead of KeyListener or Add KeyListener to the Button as well!4

Input and Output This Week Briefly: working with files Readers, Writers, and streams Networking Connecting with sockets Sending and Receiving!5

Input and Output What are examples of: Input? Output?!6

Input and Output Inputs command line arguments, files, network, gamepads, keyboard, mouse, temperature sensor, webcam, other processes, etc Outputs files, network, gamepad rumble, monitor, LEDs, speakers, robot motor, etc!7

Reminder: Reading a File BufferedReader freader = new BufferedReader(new FileReader("data.txt")); String line = freader.readline(); while(line!= null) { System.out.println(line); line = freader.readline(); }!8

Readers and Streams We prepared to read a file with: BufferedReader freader = new BufferedReader(new FileReader("data.txt")); BufferedReader FileReader file!9

Readers and Streams We prepared to read a file with: BufferedReader freader = new BufferedReader(new FileReader("data.txt")); BufferedReader FileReader file Reads lines from an input stream Turns bytes in a file into an input stream Holds data or text as a series of bytes!10

Design Patterns Basic principle: wrapping one class inside another to provide additional functionality This applies to lots of situations! We call principles like this Design Patterns Here we have an example of the Decorator design pattern BufferedReader is taking a simple data stream and BufferedReader FileReader "decorating" it with more advanced functionality file!11

Decorator Pattern Can take this principle even further to flexibly add more functionality This combination: gets 1 byte input from file buffers bytes for efficiency uncompresses zipped bytes converts raw bytes into objects of a particular class ObjectInputStream GZipInputStream BufferedInputStream FileInputStream file!12

Finding a random line Start with fileio.randomline.java Goal: store all the lines into an ArrayList and then print out a random entry Ignore lines with zero length You do not know ahead of time how many lines are in the file "Out, you green-sickness carrion..."!13

IO is unpredictable Try / Catch What if the file is not there or the disk is full? What if the server crashes? Java supports exception handling with try / catch Code inside the try block is run Java run time monitors for errors If something goes wrong, runs the catch block Can have multiple catch blocks, one for each exception type Optional: run a finally block at end Happens whether or not an error occurred!14

Writing to files I'll bet you can figure it out...!15

Files What does this code do? //imports public class Mystery { public static void main(string[] args) throws IOException { BufferedReader inputstream = null; PrintWriter outputstream = null; try { inputstream = new BufferedReader(new FileReader("file1.txt")); outputstream = new PrintWriter(new FileWriter("file2.txt")); } } } String line; while ((line = inputstream.readline())!= null) { outputstream.println(line); }!16

Line Reader + Writer Read in a file, then write it back out to a second file import java.io.filereader; import java.io.filewriter; import java.io.bufferedreader; import java.io.printwriter; import java.io.ioexception; public class CopyLines { public static void main(string[] args) throws IOException { BufferedReader inputstream = null; PrintWriter outputstream = null; try { inputstream = new BufferedReader(new FileReader("file1.txt")); outputstream = new PrintWriter(new FileWriter("file2.txt")); } } String line; while ((line = inputstream.readline())!= null) { outputstream.println(line); } } finally { if (inputstream!= null) { inputstream.close(); } if (outputstream!= null) { outputstream.close(); } }!17

From Files to Networking What if we want to read data over the network instead of from a file? We need a different data source But we are still just trying to read lines BufferedReader FileReader BufferedReader InputStreamReader file network connection!18

Interface Design Pattern What can you tell me about a file and a network connection? BufferedReader FileReader BufferedReader InputStreamReader file network connection!19

Networking Basics Clients and servers Client initiates communication with a server Server listens for incoming requests Who is the client/server in...? Browser connecting to a web site Database returning a result to an application Bit torrent file sharing Skype video call between two people Networking is done with sockets An endpoint of the communication channel between the client and server Allows two way communication Can also be used for applications running on same computer!20

Network Protocols Socket represents one end of a TCP connection TCP = Transmission Control Protocol TCP makes sending messages reliable, ordered, and fair Alternative: UDP = User Datagram Protocol Does not provide reliability or ordering guarantees Has lower overhead, so can make network sends faster What protocol would you use for? Connecting to a web site? A multiplayer shooter game? Making a voice call over the Internet? Accessing a database?!21

Network Protocols What protocol would you use for? Connecting to a web site? TCP: want to guarantee that client requests reach the server and client gets whatever response it produces A multiplayer shooter game? UDP: minimizing latency is more important than being sure that game clients get all updates Streaming online video/audio? UDP: missing every other frame of video or audio is better than having every frame take twice as long to be displayed In general, TCP is the most popular protocol Accessing a database? TCP: need to guarantee that connections are reliable and messages reach the server in order!22

Opening a Connection What do you need to know to make a connection??!23

Opening a Connection What do you need to know to make a connection? address of server hostname (google.com) or IP address port number to connect to common ports: 80 for web, 22 for ssh, 3306 for mysql database Try going to: http://209.85.201.102:80 in a browser host IP port number!24

Opening a Connection What do you need to know to make a connection? address of server hostname (google.com) or IP address port number to connect to common ports: 80 for web, 22 for ssh, 3306 for mysql database Create a new socket using the host and port Socket s = new Socket(host, portnum); Work with its input and output streams: BufferedReader in = new BufferedReader(new InputStreamReader (s.getinputstream())); PrintWriter out = new PrintWriter(s.getOutputStream(), true);!25

Readers and Writers BufferedReader in = new BufferedReader(new InputStreamReader (s.getinputstream())); PrintWriter out = new PrintWriter(s.getOutputStream(), true); Client BufferedReader InputStreamReader socket PrintWriter Input Output Server socket!26

Read and Write Just like working with files! // Set things up Socket s = new Socket(host, portnum); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter out = new PrintWriter(s.getOutputStream(), true); // Receive a line String s = in.readline(); // Send a line out.println(s.touppercase());!27

Election Day! Oops, we are a bit late #1: Connect to my server Create a new Socket, Buffered Writer, and PrintWriter #2: Vote for your favorite language: Send a string: "C", "Java", "Python", "PHP", or "Assembly" #3: Read a confirmation message from server Print it to the console Socket s = new Socket(host, portnum); BufferedReader in = new BufferedReader(new InputStreamReader (s.getinputstream())); PrintWriter out = new PrintWriter(s.getOutputStream(), true);!28

PrintWriters Why did we use the true? Remember this! PrintWriter out = new PrintWriter(s.getOutputStream(), true); The second argument sets whether autoflushing is enabled Enables autoflush If autoflush = true, then calling println() will immediately send the line over data stream If autoflush = false, then it will wait until you call out.flush() or it runs out of buffer space!29

When to autoflush? If you are writing War and Peace to a file? If you are sending messages over the network and want an immediate response? If you are writing out entries to a database file? What is the drawback of autoflush?!30

When to autoflush? If you are writing War and Peace to a file? nope: grouping lines together makes the writes more efficient If you are sending messages over the network and want an immediate response? yes: we want a message to be sent immediately If you are writing out entries to a database file? yes: we want to be sure that if we print out a record that it will immediately be written to the database What is the drawback of autoflush? Autoflush can be inefficient if it leads to many small writes. This is true for both network data streams and file writers!31

The Server The basic server loop: ServerSocket server = new ServerSocket(portnum); // needs try/catch block while (true) { try { Socket sock = server.accept(); // wait for a call BufferedReader in = new BufferedReader(new InputStreamReader (sock.getinputstream())); PrintWriter out = new PrintWriter(sock.getOutputStream(), true); } String input = in.readline(); // read a message out.println("message received"); out.close(); in.close(); sock.close(); // hang up } catch (IOException e) { e.printstacktrace(); }!32

Server Steps Create a ServerSocket on a specific port serversocket server = new ServerSocket(portnum); Call accept on the socket to wait for a connection This creates a new socket, specifically for this client Socket sock = serversocket.accept(); Setup reader and writer streams using the new client specific socket BufferedReader in = new BufferedReader(new InputStreamReader (sock.getinputstream())); PrintWriter out = new PrintWriter(sock.getOutputStream(), true);!33

Knock Knock Work with a neighbor or two in the knockknock package one group writes client, the other writes the server Write a Knock-Knock joke server and client The client says: "Knock Knock" The server says: "Who is there?" The client says: "Something" The server says: "Something who?" The client says: "Something wittier than this" (print all messages to screen at both client and server) You can run netclient.findmyip.java to get your own IP!34

Client Server Protocol The client and server must agree on a set ordering of how they will exchange information What happens if client calls readline() but the server doesn't call println()? What about the reverse?!35

Blocking Calls Receive calls such as readline() are blocking The function call will not return until something is read from the data source (file or network) If you are writing network code and your program freezes, it is probably because of this kind of issue Or your PrintWriter isn't flushing!!36

Mixed Up Split into pairs and look at netmismatch package One of you will be client, one will be server Edit the client file to have the IP of the server You can run netclient.findmyip.java to get your own IP What happens when you start the server and then run the client? How can you fix this?!37

Sending something else What if we want to send something more interesting? An int? A float? Use DataOutputStream and DataInputStream http://download.oracle.com/javase/6/docs/api/java/io/dataoutputstream.html DataOutputStream out=new DataOutputStream(sock.getOutputStream()); DataInputStream in =new DataInputStream(sock.getInputStream()); out.writefloat(math.pi); out.writeint(42); int x = in.readint(); long y = in.readlong();!38

Summary File and Network IO are very similar in Java Abstraction! Code Reuse! Use different types of input and output streams depending on what you need to send Clients and Servers need to agree ahead of time on the protocol Be careful of unmatched sends and receives!!39

Sending something else ints and floats aren't interesting enough... I want to send a Zombie Zombie int x int y Color c int direction!40

Sending Objects We just need a different type of data stream*! To send an object: ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); Zombie z = new Zombie(); out.writeobject(z); To receive an object: a connected socket ObjectInputStream in = new ObjectInputStream(s.getInputStream()); Object o = in.readobject(); Zombie z = (Zombie) o; *and to do what is on the next slide!41

Serializable Java can read and write objects over the network or to disk using Object*putStreams But, first the object class needs to tell the compiler that it is allowed to be sent in this way! Need to make the class implement Serializable public class Zombie implements Serializable What is inside the interface? Nothing! It only acts as a marker for the compiler!42

Bonus! Object streams and serializable can also be used to write or read objects to disk! FileInputStream freader = new FileInputStream("date.out"); ObjectInputStream in = new ObjectInputStream(freader); Object o = in.readobject(); Zombie z = (Zombie) o; This is why Java uses streams wrapped around streams! Hooray object oriented programming!!43

It seems so easy... But it's actually pretty complicated What are we really sending with a Zombie? Zombie int x int y Color c int direction!44

It seems so easy... But it's actually pretty complicated What are we really sending with a Zombie? Zombie int x int y Color c int direction Color int red; int blue; int green;!45

Sending Object Graphs The object being sent may have references to many other objects All need to be sent! All need to be Serializable starting with the object being serialized. Everything has to be saved in order to restore the Kennel back to this state. Example from Head First Java!46

Serialization Challenges What happens if the server is running a newer version of the code than the client The fields inside a Zombie may have changed The compiler assigns a version number to each class and runtime will detect if different What if an object has a reference to a class which does not implement serializable? May cause a java.io.notserializableexception Solution: mark the variable as transient (will be treated as null) public class Zombie implements Serializable { public int x, y; public transient DotPanel dp; // do not send!47