简体   繁体   中英

De-serializing object using TCP

I am trying to code a basic TCP server that has an Object called Person sent to it from a single client. I have gotten the connection working and an Object sent to the server. However, whenever I try to convert the read Object back into a Person (person class exists for client and server), the server throws a ClassNotFoundException . The problem is on the following line of code

Person person = (Person)in.readObject();

Here is the Server Class:

public class ConnectionTestServer extends Thread {

private ServerSocket server;
private Socket client;
private BufferedReader reader;


@Override
public void run() {
    try {
        server = new ServerSocket( 25565 );
        client = server.accept();
        ObjectInputStream in = new ObjectInputStream( client.getInputStream() );

        while ( true ){ 
            System.out.println(in.toString());//This was for debug
            if(in.readObject() instanceof Person){//and this
            System.out.println("Working?");
            }
            Person person = (Person)in.readObject();//Here is the ClassNotFoundException
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {

        new ConnectionTestServer().start();

}
}

Here is the Client Class:

public class ConnectionTestClient {

private static Socket server;

public static void main(String[] args) {
    try {
        server = new Socket("localhost", 25565);

        ObjectOutputStream stream = new ObjectOutputStream( server.getOutputStream() );
        stream.writeObject( new Person() );
        stream.flush();

        server.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}
}

And finally the Person Class:

public class Person implements Serializable {
 public int id = 10;
}

If anyone can help me with this problem, I'll appreciate it. Also, some general advice over my (probably) bad coding would be nice. Thanks!

I have this problem before when deserializing a object from TCP;

solution:

You need to put the Person class on the same name package in server and in client

example:

server com.utilities -> person class in that folder

client com.utilities -> person class in that folder

also generate a serialVersionUID .

I Agree to Rod_Algonquin. I would like to add to it, for your specific problem.

There are few things you need to do,

  1. Like Rob said, Both server's and client's Person objects must be in the same package and with similar serialvesrionUID .
  2. Your code for the server should be as follows: (Observe the lines commented, in your code you are reading the object twice while sending it only once )

     public class Server extends Thread { private ServerSocket server; private Socket client; @Override public void run() { try { server = new ServerSocket(25565); client = server.accept(); while (true) { BufferedInputStream bis = new BufferedInputStream(client.getInputStream()); ObjectInputStream in = new ObjectInputStream(bis); System.out.println(in.toString());// This was for debug /*if (in.readObject() instanceof Person) {// and this System.out.println("Working?"); }*/ Person person = (Person) in.readObject(); System.out.println(person.id); // Here is the // ClassNotFoundException } } catch (Exception e) { e.printStackTrace(); } } 
  3. Plus a suggestion, in java socket programming you should use BufferInputStream / BufferedOutputStream while reading/ writing multiple objects. It leads to better performance.

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