Networking Serialization

Slides:



Advertisements
Similar presentations
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
Advertisements

Serialization Sending Complex Java Data Structures to Files or Over the Network.
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
Java Iterators interface Collection { … Iterator iterator(); Iterator iterator(); …} interface Set extends Collection { … Iterator iterator(); Iterator.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Java Review 2. The Agenda The following topics were highlighted to me as issues: –File IO (Rem) –Wrappers (Rem) –Interfaces (Rem) –Asymptotic Notation.
import java.io.*; class Tree implements Serializable { static private class Node implements Serializable { int _data; Node _ls, _rs; Node(int data) {_data.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
Greg Jernegan Brandon Simmons. The Beginning…  The problem Huge demand for internet enabled applications and programs that communicate over a network.
CSE 219 Computer Science III Network Programming (Application Layer) Reference:
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Java I/O Writing and Reading Objects to File Serialization.
Object Persistence and Object serialization CSNB534 Asma Shakil.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Files and Serialization. Files Used to transfer data to and from secondary storage.
Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.
XSLT in Practice. Exercises  download Apache Xalan - install it - try the example in Xalan-Java Overview  ZVON XSLT Tutorial.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Operating Systems {week 11a}
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Echo Networking COMP
Principles of Software Development
Lecture 21 Sockets 1 (Not in D&D) Date.
CSE 341, S. Tanimoto Java networking-
Object Serialization in Java
Remote Method Invokation
Memento Design Pattern
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Accessing Files in Java
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Client server programming
Programming in Java Files and I/O Streams
CS 116 Object Oriented Programming II
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
UNIT-6.
Object Serialization Explanation + Example of file + network
Exceptions Complicate Code
Exceptions Complicate Code
CS18000: Problem Solving and Object-Oriented Programming
Command Invocation Protocol
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Exceptions Complicate Code
Podcast Ch23f Title: Serialization
Java Basics Introduction to Streams.
The command invocation protocol
Java Socket Programming and Java RMI CSE 291
CS18000: Problem Solving and Object-Oriented Programming
OO Java Programming Input Output.
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Networking Serialization CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu

Outline Network Serialization Program USC CSCI 201L

Serialization Recall that serialization allows us to write objects to an output stream and read objects from an input stream We have already seen serialization with file I/O This is shown on the following slides USC CSCI 201L

Serialization with File I/O 1 import java.io.Serializable; 2 3 public class Employee implements Serializable { 4 public static final long serialVersionUID = 1; 5 private String fname, lname; 6 private transient String password; 7 public Employee(String fname, String lname, String password) { 8 this.fname = fname; 9 this.lname = lname; 10 this.password = password; 11 } 12 public void printEmployee() { 13 System.out.println(fname + " " + lname + ": " + password); 14 } 15 } USC CSCI 201L

Serialization with File I/O 1 import java.io.*; // to save space 2 public class EmployeeMain { 3 public static void main(String [] args) { 4 Employee emp = new Employee("donald", "trump", "billionaire"); 5 ObjectOutputStream oos = null; 6 ObjectInputStream ois = null; 7 try { 8 oos = new ObjectOutputStream(new FileOutputStream("out.txt")); 9 oos.writeObject(emp); 10 oos.flush(); 11 ois = new ObjectInputStream(new FileInputStream("out.txt")); 12 Employee emp1 = (Employee)ois.readObject(); 13 emp.printEmployee(); 14 emp1.printEmployee(); 15 } catch (FileNotFoundException fnfe) { 16 System.out.println("fnfe: " + fnfe.getMessage()); 17 } catch (IOException ioe) { 18 System.out.println("ioe: " + ioe.getMessage()); 19 } catch (ClassNotFoundException cnfe) { 20 System.out.println("cnfe: " + cnfe.getMessage()); 21 } finally { 22 try { 23 if (oos != null) 24 oos.close(); 25 if (ois != null) 26 ois.close(); 27 } 28 catch (IOException ioe) { 29 System.out.println(“ioe: “ + ioe.getMessage()); 30 } 31 } // ends finally 32 } 33 } USC CSCI 201L

Network Serialization With network communication, we are also able to transmit objects back and forth between two different programs over a Socket This works almost exactly the same as serialization with file I/O, but instead of a FileInputStream and FileOutputStream, we will use the input stream and output stream associated with the Socket USC CSCI 201L

Serialization with Server Networking 1 import java.io.*; // shortened for space 2 import java.net.*; // shortened for space 3 public class NetworkSerializationServer { 4 private ServerSocket ss = null; 5 private Socket s = null; 6 public NetworkSerializationServer() { 7 ObjectInputStream ois = null; 8 ObjectOutputStream oos = null; 9 try { 10 ss = new ServerSocket(6789); 11 s = ss.accept(); 12 ois = new ObjectInputStream(s.getInputStream()); 13 Employee emp1 = (Employee)ois.readObject(); 14 oos = new ObjectOutputStream(s.getOutputStream()); 15 oos.writeObject(emp1); 16 oos.flush(); 17 } catch (IOException ioe) { 18 System.out.println("ioe: " + ioe.getMessage()); 19 } catch (ClassNotFoundException cnfe) { 20 System.out.println("cnfe: " + cnfe.getMessage()); 21 } finally { 22 try { 23 if (ois != null) 24 ois.close(); 25 if (oos != null) 26 oos.close(); 27 if (s != null) 28 s.close(); 29 if (ss != null) 30 ss.close(); 31 } catch (IOException ioe) { 32 System.out.println("IOE closing streams: " + ioe.getMessage()); 33 } 34 } // ends finally 35 } 36 public static void main(String [] args) { 37 new NetworkSerializationServer(); 38 } 39 } USC CSCI 201L

Serialization with Client Networking 1 import java.io.*; // shortened for space 2 import java.net.Socket; 3 public class NetworkSerializationClient { 4 private Socket s = null; 5 public NetworkSerializationClient() { 6 ObjectOutputStream oos = null; 7 ObjectInputStream ois = null; 8 Employee emp = new Employee("donald", "trump", "billionaire"); 9 try { 10 s = new Socket("localhost", 6789); 11 ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); 12 oos.writeObject(emp); 13 oos.flush(); 14 ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); 15 Employee emp1 = (Employee)ois.readObject(); 16 oos.close(); 17 ois.close(); 18 System.out.println("Employee sent"); 19 emp.printEmployee(); 20 System.out.println("Employee received"); 21 emp1.printEmployee(); 22 } catch (IOException ioe) { 23 System.out.println("ioe: " + ioe.getMessage()); 24 } catch (ClassNotFoundException cnfe) { 25 System.out.println("cnfe: " + cnfe.getMessage()); 26 } finally { 27 try { 28 if (ois != null) 29 ois.close(); 30 if (oos != null) 31 oos.close(); 32 if (s != null) 33 s.close(); 34 } catch (IOException ioe) { 35 System.out.println(ioe.getMessage()); 36 } 37 } // ends finally 38 } 39 public static void main(String [] args) { 40 new NetworkSerializationClient(); 41 } 42 } USC CSCI 201L

Outline Network Serialization Program USC CSCI 201L

Program Modify the chat client and chat server program to pass serialized objects instead of strings. The serialized object should contain the IP address, port, and String that is being sent. The output should then contain the IP and port. C:>java ChatClient localhost 6789 hello 192.168.1.2:56450: how are you? fine, and you? 192.168.1.2:56450: good, thanks 192.168.1.3:56451: hello how are you? 192.168.1.3:56451: fine, and you? good, thanks USC CSCI 201L