简体   繁体   中英

How to send an OBJECT over TCP in java?

I'm writing a program to send an object from one class to another class. Here is a short sample example of my program to represent the problem. As you can see the object to send from server to client is Student class which has been defined separately in each class(Server/Client). I have examined this code by sending an ArrayList which works fine but when it comes to a class type which defined by myself i'm receiving this error:

Exception in thread "main" java.lang.ClassCastException: ServerSide$1Student cannot be cast to ClientSide$1Student
    at ClientSide.main(ClientSide.java:29)

Here is the code for Server side:

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

public class ServerSide {

    public static void main(String[] args) {
        class Student implements Serializable
        {
            int id;
            public Student(int num){id=num;}
            public void setID(int num){id=num;}
            public void Print(){System.out.println("id = " + id);}
        }
        try
        {
            Student a = new Student(3);
            ServerSocket myServerSocket = new ServerSocket(9999);
            Socket skt = myServerSocket.accept();   
            try 
            {
                ObjectOutputStream objectOutput = new ObjectOutputStream(skt.getOutputStream());
                objectOutput.writeObject(a);                
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            } 
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

And for the client side is:

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientSide {

    public static void main(String[] args)
    {
        class Student implements Serializable
        {
            int id;
            public Student(int num){id=num;}        
            public void setID(int num){id=num;}
            public void Print(){System.out.println("id = " + id);}
        }
        try {       
            Socket socket = new Socket("10.1.1.2",9999);
            try {
                ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream());
                try {
                    Object object =(Student) objectInput.readObject();
                    Student tmp = (Student) object;
                    tmp.Print();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }           
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }   
}

Edit:

I moved them to same file and added serialise ID. It works fine.

Having two classes with the same name is not enough. The two classes need to have

  • the same package
  • be in the same outer class if any
  • have the same serialVersionUID.

I suggest you have a single, stand alone class which is common for both the client and the server. In a more complex project you might consider building these common components in a module which both the client and server modules depend on.

You cannot ever deserialize a stream representing an instance of class X into an instance of class Y.

To solve your problem, you need to move the code for the Student class to another file, say Student.java, and use that single class on your client code and on your server code.

Note that if you modify your class, in most cases you will need to redeploy the server (otherwise the client would send the server an stream representing an instance of class that is known only to the client).

You are referencing two different classes. Student which is an inner of ClientSide and Student which is an inner class of ServerSide . You should move the Student class to a different file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM