Download presentation
Presentation is loading. Please wait.
1
Client server programming
Sockets Recap Sockets Stream sockets Datagram sockets Multicast Sockets (datagram sockets) 08/11/2018 Client server programming
2
Advanced (Stream) Socket Programming
Jin Sa 08/11/2018 Client server programming
3
Client server programming
Outline Object serialization Overview of object serialization Examples of object serialization Transport objects over stream sockets 08/11/2018 Client server programming
4
Client server programming
Rationale Compared with socket, Java Remote Method Invocation (RMI) provides a simple way to distribute objects across networks. However, there is a network communication overhead. Low-level sockets can be used to develop client/server systems, 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 and overcome the overhead incurred in using RMI. 08/11/2018 Client server programming
5
Client server programming
Object I/O InputStream and OutputStream can be further divided to more specialised I/O streams. DataInputStream and DataOutputStream enable us to perform I/O for primitive type values and strings. ObjectInputStream and ObjectOutputStream enable us to perform I/O for objects. The methods are readObject() and writeObject(…) 08/11/2018 Client server programming
6
Client server programming
Example of Object I/O TestWriteObject Create an ObjectOutputStream which associated with an output file Create a Date object Write the current state of the date object to the file “test1” 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 08/11/2018 Client server programming
7
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. 08/11/2018 Client server programming
8
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. 08/11/2018 Client server programming
9
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); 08/11/2018 Client server programming
10
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 08/11/2018 Client server programming
11
Fragment of WriteStudentRecord
StudentRecord s=new StudentRecord("Jin"); s.setSubject("Chinese"); s.setGrade(99); String fileName="myFile"; … ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(fileName)); oos.writeObject(s); 08/11/2018 Client server programming
12
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 08/11/2018 Client server programming
13
Transporting 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. 08/11/2018 Client server programming
14
StudentRecord application
RecordServer StudentClient Wait for and establish connection Request to connect Set up object input and output streams Set up object input and output streams Create a student record (for Jin) 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 08/11/2018 Client server programming
15
Process for transporting objects
For the server Create a ServerSocket to listen for 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 ObjectInoutSream. Note that the objects being transported must be serialisable. 08/11/2018 Client server programming
16
Process for transporting objects
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. 08/11/2018 Client server programming
17
Client server programming
files 2006/7 code/clientserver2/sending objects Or in two separate projects: ClientProject ServerProject 08/11/2018 Client server programming
18
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); } 08/11/2018 Client server programming
19
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()); 08/11/2018 Client server programming
20
Client server programming
Summary Object serialisation Transporting objects using sockets 08/11/2018 Client server programming
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.