/
CS18000: Problem Solving and Object-Oriented Programming CS18000: Problem Solving and Object-Oriented Programming

CS18000: Problem Solving and Object-Oriented Programming - PowerPoint Presentation

carny
carny . @carny
Follow
70 views
Uploaded On 2023-07-22

CS18000: Problem Solving and Object-Oriented Programming - PPT Presentation

External Communication File IO Persistence of File Storage RAM comes and goes Programs crash Systems reboot Files last well comparatively speaking Programs save data to files to ID: 1010584

file socket client server socket file server client string system object close java public data read serversocket printf class

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CS18000: Problem Solving and Object-Orie..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. CS18000: Problem Solving and Object-Oriented Programming

2. External CommunicationFile I/O

3. Persistence of File StorageRAM comes and goesPrograms crashSystems rebootFiles last (well … comparatively speaking…)Programs save data to files torecover from program crashes and system rebootsprovide as input to other programsFile I/O operations extend naturally to communication between programs3

4. Files and JavaJava is (or tries to be) platform independentProvides abstractions for files and file systemsFile classBut, file name is operating system (OS) dependentAnd, file directory conventions are OS-dependent (e.g., path name of user home directory)So, there are limits to OS independenceThree layers of abstraction in Java for file I/OUltimately, all data stored as a stream of bytes4or bits

5. Example: DirectoryList // proxy method to setup recursion... ArrayList<File> directoryList(File root) { ArrayList<File> list = new ArrayList<File>(); return directoryList(list, root); } // recursive method to walk the directory tree (preorder traversal) ArrayList<File> directoryList(ArrayList<File> list, File root) { list.add(root); if (root.isDirectory()) { for (File f : root.listFiles()) { directoryList(list, f); } } return list; }5proxy methodvisit noderecursive call

6. Generic File Operations (1)Open: Files must be opened before they can be usedOpen method indicates “for reading”, “for writing”, or “both”May also indicate “append” modeAllows operating system to establish “buffers” and other state information about the file being read or writtenReadTransfers data from the file (or input stream) to the user processSpecific method signatures indicate the type of data being transferred (byte, int, String, Tree, etc.)WriteTransfers data from the user process to the file (or output stream)Specific method signatures indicate the type of data being transferred (byte, int, String, Tree, etc.)6

7. Generic File Operations (2)File positionSets the “current input position” to a specific byte address in the fileCan be used to skip over data in the file; or back up to read data againCan be used to “rewind” the file to start reading from the beginning againCloseEnsures that any “queued data” is “flushed” from the operating system buffersFrees any operating system resources being dedicated to managing the file7

8. The Importance of BufferingWithout buffering, each read or write may generate physical disk accessCan be extremely slow for large volumes of dataBuffering has OS create internal arrayOS reads “more than needed” on input, keeps rest for next call to read methodOS doesn’t send output “right away” to disk drive, waits a while in case another write comes alongImportant to close file (or flush buffers) when done8

9. File I/O Layers in JavaLow-Level“Raw” data transfer: byte-orientedClasses: FileOutputStream, FileInputStreamHigh-LevelJava primitive typesClasses: DataOutputStream, DataInputStreamObject I/OJava object typesClasses: ObjectOutputStream, ObjectInputStreamUltimately, all data stored as a sequence of bytes9

10. Example: Low-Level I/Oimport java.io.*;public class LowLevelIO { public static void main(String[] args) throws IOException { File f = new File("lowlevel"); FileOutputStream fos = new FileOutputStream(f); fos.write(42); fos.close(); FileInputStream fis = new FileInputStream(f); int i = fis.read(); System.out.printf("Read %d\n", i); fis.close(); }}10

11. Example: High-Level I/Oimport java.io.*;public class HighLevelIO { public static void main(String[] args) throws IOException { File f = new File("highlevel"); FileOutputStream fos = new FileOutputStream(f); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(1000); dos.close(); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); int i = dis.readInt(); System.out.printf("Read %d\n", i); dis.close(); }}11dos builds on fosdis builds on fis

12. Tricky BitsYou must keep track of what you’re doing!Data values must be read in the same order in which they were writtenwrite int, long, long, boolean, double, float, charread int, long, long, boolean, double, float, charIf you try to read an int, but a double is next in the stream, you’ll get garbage12

13. Example: Object I/O (1)import java.io.*;public class ObjectIO { public static void main(String[] args) throws Exception { File f = new File("object"); FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream oos = new ObjectOutputStream(fos); Tree tree1 = new Tree(42, "elm"); oos.writeObject(tree1); // write the object out oos.close(); FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis); Tree tree2 = (Tree) ois.readObject(); // read the object back ois.close(); System.out.printf("tree1 = %s\n", tree1); System.out.printf("tree2 = %s\n", tree2); }}13oos builds on fosois builds on fis

14. Example: Object I/O (2)class Tree implements Serializable { long circumference; String species; Tree(long circumference, String species) { this.circumference = circumference; this.species = species; } public String toString() { return String.format("%x: circumference = %d, species = %s", hashCode(), circumference, species); }}14

15. Object I/O: NotesFields declared static or transient are not written outstatic: logical, since doesn’t apply to specific objecttransient: useful for fields that need to be regenerated anywayGraphs of objects can be writtenwriting one object also writes all serializable objects connected to itonly writes one copy of each object, even when there are multiple references to it15

16. File Content TypesCan consider file contents in two categoriesText (e.g., *.java, *.txt)Store human-readable, character dataMostly platform independent (except EOL)Binary (e.g., *.class, *.exe)Not (generally) human readableStore any kind of dataRequires specific programs to “make sense” of it16

17. Writing and Reading TextJava handles translation from internal primitive format to human-readable textWritingClass: PrintWriter (favored, more platform independent)Class: PrintStream for System.out (but out of favor)ReadingClasses: FileReader and BufferedReaderAlso, ScannerNote: BufferedReader is more efficient than Scanner (only important for high volumes of I/O)17

18. Example: TextIO (1)import java.io.*;public class TextIO { public static void main(String[] args) throws IOException { File f = new File("textio.txt"); // open FileOutputStream in append mode (true) FileOutputStream fos = new FileOutputStream(f, true); // use PrintWriter--similar to PrintStream (like System.out)... PrintWriter pw = new PrintWriter(fos); pw.println("our old friend"); pw.close();// continued...18

19. Example: TextIO (2)// ... continued // read what we just wrote... FileReader fr = new FileReader(f); BufferedReader bfr = new BufferedReader(fr); while (true) { String s = bfr.readLine(); if (s == null) break; System.out.println(s); } bfr.close(); }}19

20. External CommunicationNetwork Communication

21. Some (Simplified) DefinitionsInternet Protocol (IP):Identifies hosts (servers, workstations, laptops, etc.) with a unique address (e.g., 128.10.2.21)Domain Name System (DNS):Maps domain names (e.g., galahad.cs.purdue.edu) to IP addresses (e.g., 128.10.9.143)Transmission Control Protocol (TCP):Identifies ports on hosts for a network connectionSocket: IP address plus TCP portTwo sockets makes a network connection21

22. Client-ServerA Server is a process that waits for a connectionA Client is a process that connects to a serverAt different times, a process may be both a client and a serverNeed not be associated with a specific computer: Any computer can have both client and server processes running on itOnce connected, the client and server can both read and write data to one another asynchronously (“a bi-directional byte pipe”)22

23. Use of SocketsClients and Servers communicate via SocketsSocket: IP address plus TCP portThink: street name plus house numberIP addressesIdentifies a computer on the InternetPublic addresses are globally uniqueRepresented using dotted-decimal (byte) notation: 128.10.9.143Some firewalls translate addresses to internal ones (e.g., PAL)Port number0-65535 (16 bits)Low-valued port numbers are reserved for privileged processes23

24. Objects and Networking in JavaYou know that Java objects can be written to and read from filesJava objects can also be exchanged over network connectionsUses ObjectOutputStream and ObjectInputStreamTricky bits…ObjectOutputStream generates a “header” of information that must be readRequires “flush” to ensure ObjectInputStream reader is not blocked24

25. ObjectStream Client-Server Timeline25Server starts: opens ServerSocket and blocks (waiting for client) Client starts: opens Socket to ServerServer receives connectionServer opens ObjectOutputStream, sends header with flushClient receives object stream headerClient opens ObjectOutputStream, sends header with flushServer receives object stream headerClient and Server exchange objects in agreed upon orderServer ProcessClient Process

26. Java Networking Class: SocketModels a TCP/IP socketUsed by Client to identify ServerIP address (or DNS name)Port numbernew Socket("pc.cs.purdue.edu", 12190)Used by Server to identify connected ClientProvides streams for communications:getOutputStream()getInputStream()26

27. Java Networking Class: ServerSocketUsed by Server to wait for a Client to connectConstructor specifies TCP port number to use:ServerSocket ss = new ServerSocket(4242);Method accept() blocks waiting for connectionSocket socket = ss.accept();27

28. Example: Object Serverimport java.io.*;import java.net.*;public class Server { public static void main(String[] args) throws IOException, ClassNotFoundException { // create socket on agreed-upon port... ServerSocket serverSocket = new ServerSocket(4242); // wait for client to connect, get socket connection... Socket socket = serverSocket.accept(); // open output stream to client, flush send header, then input stream... ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.flush(); // ensure data is sent to the client ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); // send object(s) to client... String s1 = "hello there"; oos.writeObject(s1); oos.flush(); // ensure data is sent to the client System.out.printf("sent to client: %s\n", s1); // read object(s) from client... String s2 = (String) ois.readObject(); System.out.printf("received from client: %s\n", s2); // close streams... oos.close(); ois.close(); }}28

29. Example: Object Clientimport java.io.*;import java.net.*;public class Client { public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException { // create socket on agreed upon port (and local host for this example)... Socket socket = new Socket("data.cs.purdue.edu", 4242); // open input stream first, gets header from server... ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); // open output stream second, sends header to server... ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); // read object(s) from server... String s1 = (String) ois.readObject(); System.out.printf("received from server: %s\n", s1); // write object(s) to server... String s2 = s1.toUpperCase(); oos.writeObject(s2); oos.flush(); // ensure data is sent to the server System.out.printf("sent to server: %s\n", s2); // close streams... oos.close(); ois.close(); }}29

30. Client-Server with ThreadsIn many (most?) cases, a single server is connected to by multiple clientsServer must be able to communicate with all clients simultaneously—no blockingTechnique: server creates a separate thread to handle each client as it connectsClient and server may also each create separate thread for reading and writing30

31. Example: Echo ServerSimple server that accepts connections from multiple clientsSpawns a thread for each clientReads lines from the connection, logs information, echoes lines backUseful for debugging network and code31

32. Echo Server Timeline32Server starts: opens ServerSocket and blocks (waiting for client) Client 1 starts: opens Socket to ServerServer receives connectionServer creates threadClient 1 sends lineServer thread 1 reads/echoes lineClient 1 reads lineClient 2 starts: opens Socket to ServerClient 3 starts: opens Socket to ServerServer ProcessClient Process

33. Example: Echo Server (1)import java.io.IOException;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;public class EchoServer implements Runnable { Socket socket; public EchoServer(Socket socket) { this.socket = socket; } // continued…33

34. Example: Echo Server (2)// run method for thread... public void run() { System.out.printf("connection received from %s\n", socket); try { // socket open: make PrinterWriter and Scanner from it... PrintWriter pw = new PrintWriter(socket.getOutputStream()); Scanner in = new Scanner(socket.getInputStream()); // read from input, and echo output... while (in.hasNextLine()) { String line = in.nextLine(); System.out.printf("%s says: %s\n", socket, line); pw.printf("echo: %s\n", line); pw.flush(); } // input done, close connections... pw.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } 34

35. Example: Echo Server (3)// main method... public static void main(String[] args) throws IOException { // allocate server socket at given port... ServerSocket serverSocket = new ServerSocket(4343); System.out.printf("socket open, waiting for connections on %s\n", serverSocket); // infinite server loop: accept connection, // spawn thread to handle... while (true) { Socket socket = serverSocket.accept(); EchoServer server = new EchoServer(socket); new Thread(server).start(); } }}35

36. Network Communication in JavaUses standard file I/O classes: low-level, high-level, object, and textAdds abstractions to deal with network connectionsServerSocket to wait for connectionsSocket abstracts a TCP socket (IP address + port)Uses threads to improve responsiveness and avoid blocking36