So far we discussed about the fundamentals of networking with Java.We have seen sample codes for TCP and UDP communications.If there is a provision to transfer a Java object between two jVMs through sockets ,it would be a greater advantage to developers.Fortunately , Java allows transferring of objects through sockets. The only thing the developer needs to be taken care is : the class whose object needs to be transferred should implement the Serializable interface . We already discussed the importance of Serializable interface when we discussed the object serialization . In this section , we are discussing how the transferring of Java objects through sockets is happening , with a suitable example.Here , we are explaining with TCP sockets.
Transferring of Java Objects through sockets
Assume we have a client application in one machine and a server application in another machine.And we need to transfer a Student object from client to server.So our Student.java should implement the Serializable interface.
Student.java
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 5950169519310163575L;
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (id != student.id) return false;
if (name != null ? !name.equals(student.name) : student.name != null) return false;
return true;
}
public int hashCode() {
return id;
}
public String toString() {
return "Id = " + getId() + " ; Name = " + getName();
}
}
Now , lets see the Client .java
Client.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
public class Client {
private Socket socket = null;
private ObjectInputStream inputStream = null;
private ObjectOutputStream outputStream = null;
private boolean isConnected = false;
public Client() {
}
public void communicate() {
while (!isConnected) {
try {
socket = new Socket("localHost", 4445);
System.out.println("Connected");
isConnected = true;
outputStream = new ObjectOutputStream(socket.getOutputStream());
Student student = new Student(1, "Bijoy");
System.out.println("Object to be written = " + student);
outputStream.writeObject(student);
} catch (SocketException se) {
se.printStackTrace();
// System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client();
client.communicate();
}
}
The Client.java simply creates a socket channel on port 4445.If the server is running in another machine , then just replace the ip address of that machine in place of local host.Client creates a Student object and writing to socket .Now lets see the Server.java
Server.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class Server {
private ServerSocket serverSocket = null;
private Socket socket = null;
private ObjectInputStream inStream = null;
public Server() {
}
public void communicate() {
try {
serverSocket = new ServerSocket(4445);
socket = serverSocket.accept();
System.out.println("Connected");
inStream = new ObjectInputStream(socket.getInputStream());
Student student = (Student) inStream.readObject();
System.out.println("Object received = " + student);
socket.close();
} catch (SocketException se) {
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cn) {
cn.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server();
server.communicate();
}
}
Server.java simply creates a ServerSocket object on port 4445.Once a new client connection comes, a new Socket object will be created.It is receiving the object and then closing the socket channel.
Output
Run Server.java and Client.java
output of Client.java
Connected
Object to be written = Id = 1 ; Name = Bijoy
output of Server.java
Connected
Object received = Id = 1 ; Name = Bijoy
Significance of serialVersionUID
While performing the operation of sending serialized object through socket , the serialVersionUID is very important. Our client and server applications should have the class with same serialVersionUID.In other words , the correct version of class should be there in client and server.Otherwise java.io.InvalidClassException will be thrown.We can verify the result by simply changing the serialVersionUID of Student.java in client side and server side with unmatched values.