1

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.

0

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for you reply but unfortunately I am forced to use the different directory for the object to transfer from one program to another and here is the specification. I appreciate any comment: tinypic.com/r/26279dt/6
In that case you cannot use the builtin Java Serialisation. You need to use another form of serialisation or write your own. If they have specified you need to use Java Serialization then you need to clarify you understand what they are asking for.
@Bernard There is nothing in that specification that says you need two Student classes.
4

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).

4 Comments

Thanks for you reply but unfortunately I am forced to use the different directory for the object to transfer from one program to another and here is the specification. I appreciate any comment: tinypic.com/r/26279dt/6
There's nothing about class Student on that image.
Anyways, that is how Java serialization works. If you cannot abide by these rules, you cannot use it.
Err, well, you can,, using readResolve() and writeReplace(), but I doubt it's in scope for his assignment. More likely the assignment is wrong.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.