Tuesday, 9 July 2019

Sending objects over sockets Java example | How to send serialized object over network in Java

                    In this post, we will see how to send serialized object over network in java (sending objects over sockets java example).

Watch following video:


Watch on YouTube: https://www.youtube.com/watch?v=GJTxQBU0iPg

                    In previous post https://www.comrevo.com/2017/08/serialization-in-java-with-example.html, we have seen
What is Serialization?  
What is the need of Serialization?             
What is Deserialization?              
How to achieve Serialization?

                   Here, we will see how to send object over network using socket programming. We have to mark class as Serializable by implementing Serializable interface. For Serialization, we have to use class ObjectOutputStream while for Deserialization, we have to use ObjectInputStream.                  
                      
Example:
                 Go through the following example: 
(Here, we are running Client and Server program on the same system. Hence, we are using host name localhost. If, we are running Client and Server on different machines, then replace localhost with IP address of Server machine.)

         
SerializationClient.java 
import java.io.*;
import java.net.*;

class Sample implements Serializable
{
String name;
String city;
String contactnum;
}

public class SerializationClient
{
    public static void main(String[] args) throws Exception
    {
        Sample obj=new Sample();
        obj.name="Ramesh";
        obj.city="Pune";
        obj.contactnum="9090909090";
      
        Socket socket = new Socket("localhost", 7000);
        System.out.println("Connected");

        //Serialization
        OutputStream os = socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        System.out.println("Sending values to the ServerSocket");
        oos.writeObject(obj);

        System.out.println("Closing socket and terminating program.");
        socket.close();
    }
}
  

Output (SerializationClient.java):
parag@parag-Inspiron-N4010:~/Desktop/programs/socket$ javac SerializationClient.java 
parag@parag-Inspiron-N4010:~/Desktop/programs/socket$ java SerializationClient
Connected
Sending values to the ServerSocket
Closing socket and terminating program.

SerializationServer.java 
import java.io.*;
import java.net.*;

class Sample implements Serializable
{
String name;
String city;
String contactnum;
}

public class SerializationServer
{
    public static void main(String[] args) throws Exception
     {
        ServerSocket ss = new ServerSocket(7000);
        System.out.println("ServerSocket awaiting connections...");
        Socket socket = ss.accept();
        System.out.println("Connection from " + socket);

        //Deserialization
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        Sample obj1=(Sample)ois.readObject();
        
        System.out.println("Values received from Client are:-");
        System.out.println("Name:"+obj1.name);
        System.out.println("City:"+obj1.city);
        System.out.println("Contact No.:"+obj1.contactnum);

        System.out.println("Closing sockets.");
        ss.close();
        socket.close();
    }
}
 
  

Output (SerializationServer.java):
parag@parag-Inspiron-N4010:~/Desktop/programs/socket$ javac SerializationServer.java 
parag@parag-Inspiron-N4010:~/Desktop/programs/socket$ java SerializationServer
ServerSocket awaiting connections...
Connection from Socket[addr=/127.0.0.1,port=41350,localport=7000]
Values received from Client are:-
Name:Ramesh
City:Pune
Contact No.:9090909090
Closing sockets.


Check other posts on Multi-Threading in this link http://www.comrevo.com/2016/09/multi-threading.html




No comments:

Post a comment