210 likes | 231 Views
Learn how to use object serialization and low-level sockets to transport objects between client and server processes over stream sockets. Examples include reading/writing objects from/to files and sending objects over sockets.
E N D
Advanced (Stream) Socket Programming Jin Sa Client server programming
Outline • Object serialization • Overview of object serialization • Examples of object serialization • Sending objects over stream sockets Client server programming
Introduction • Low-level sockets can be used to develop client/server systems using the I/O streams, but most Java I/O classes are not object friendly. • Object serialization is the mechanism that allows you to read/write objects to byte streams. • Combine low-level sockets and object serialization to enables us to transport objects over sockets. Client server programming
Object I/O • We have seen InputStream and OutputStream which enable us to read and write Strings • ObjectInputStream and ObjectOutputStream enable us to perform I/O for objects. • The methods are readObject() and writeObject(…) Client server programming
Example of Object I/O-- Writing object to file (save)-- reading object from file (open/load) • TestWriteObject • Create an ObjectOutputStream which is associated with an output file “dateFile” • Create a Date object • Write the current state of the date object to the file “dateFile” • TestReadObject • Create an ObjectInputStream which is associated with an input file • Read the date object from the file • find out the state of the date object when it was written to the file. • Run programs in objectserial package Client server programming
Fragment of TestWriteObject ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("dateFile")); Date date= new Date(); oos.writeObject(date); Client server programming
Fragment of TestReadObject ObjectInputStream ois=new ObjectInputStream(new FileInputStream("dateFile")); Date date=(Date)ois.readObject(); System.out.println(date.toString()); Run program in objectserial Client server programming
Object serialization • Not all objects can be written to an output stream. Objects that can be written to an output stream are said to be serializable. • Object serialization is a mechanism that is useful in any program that wants to save the state of objects to a file and later read those objects to reconstruct the state of the program, or to send an object over the network using sockets. • Serializing a class can be easily done by having the class implement the java.io.Serializable interface. • Implementing this interface enables the Java serialization mechanism to automate the process of storing objects. • The Date is serializable. Hence we can store its objects to an output stream • We can define our own class to implement the Serializable interface. Client server programming
Student record example • Define a StudentRecord class (serialisable) • Define a WriteStudentRecord class that create an instance of StudentRecord, makes some changes to the instance and saves the instance to a file. • Define a ReadStudentRecord class that reads the instance from the file, and displays the information of the instance. Client server programming
StudentRecord class public class StudentRecord implements Serializable{ private String name, subject; private int grade; public StudentRecord(String n) { name=n; } public void setSubject(String s){ subject=s; } public void setGrade(int g){ grade=g; } public String toString(){ return (name + " is doing "+ subject +" and has got "+ grade); } } Client server programming
WriteStudentRecord class • Create a StudentRecord object • Set the subject and the grade • Create an instance of an ObjectOutputStream for a file • Write the StudentRecord object to the ObjetOutputStream Client server programming
Fragment of WriteStudentRecord StudentRecord s=new StudentRecord("Jo"); s.setSubject(“client Server”); s.setGrade(79); String fileName="myFile"; … ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(fileName)); oos.writeObject(s); … Client server programming
Fragment of ReadStudentRecord class String fileName="myFile"; … ObjectInputStream ois= new ObjectInputStream( new FileInputStream(fileName)); StudentRecord record = (StudentRecord)ois.readObject(); System.out.print(record.toString()); … Run program in objectserial package Client server programming
Sending Objects over Sockets Now we have seen how to write and read objects to and from I/O streams, let’s see how to transport objects between different processes over stream sockets. Client server programming
StudentRecord application RecordServer StudentClient Request to connect Wait for and establish connection Set up object input and output streams Set up object input and output streams Create a student record object (Jo) Receive a record from the server Send the record object Student select the subject, change the record and send to the server Receive the record from the student Set the grade, update the record and send back to student Receive the record from the server Client server programming
Process for sending objects (1) • For the server • Create a ServerSocket for handling connection request • Issue an accept method to accept a connection request, create a stream socket once a connection is established • From the socket, create an object input stream and an object output stream. • To send an object to a client, use the writeObject method for the objectOutputStream • To receive an object, use the readObject method for the ObjectInputSream. • Note that the objects being sent must be serialisable. Client server programming
Process for sending objects (2) • For the client • Make a connection request by creating a socket • Set up the object input and output streams connected to the server. • To send an object to the server, use the writeObject method for the objectOutputStream • To receive an object, use the readObject method for the ObjectInoutSream. • Note that the objects being transported must be serialisable. Client server programming
Fragment of RecordsServer ServerSocket dateServer = new ServerSocket(3000); … while(true) { Socket client = dateServer.accept(); … ois = new ObjectInputStream(client.getInputStream()); oos = new ObjectOutputStream(client.getOutputStream()); StudentRecord record1= new StudentRecord(“Jin"); oos.writeObject(record1); record1=(StudentRecord)ois.readObject(); record1.setGrade(90); oos.writeObject(record1); } Client server programming
Fragment of StudentClient socket = new Socket("localhost",3000); oos = new ObjectOutputStream(socket.getOutputStream()); ois = new ObjectInputStream(socket.getInputStream()); StudentRecord record = (StudentRecord) ois.readObject(); record.setSubject("Java"); oos.writeObject(record); record=(StudentRecord)ois.readObject(); System.out.print(record.toString()); Client server programming
files • 2007/8 client server programming /sending Object package • Server set up a record for James • Client select subject Java • Server update the grade to be 90, send it to client • Client reads the updated record and display the information Client server programming
Summary • Object serialisation • Sending objects using sockets Client server programming