0

SimpleClient.java

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

class testobject implements Serializable {

    int value;
    String id;

    public testobject(int v, String s) {
        this.value = v;
        this.id = s;
    }
}

public class SimpleClient {

    public static void main(String args[]) {
        try {
            Socket s = new Socket("localhost", 2002);
            OutputStream os = s.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            testobject to = new testobject(1, "object from client");
            oos.writeObject(to);
            oos.writeObject(new String("another object from the client"));
            oos.close();
            os.close();
            s.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

SimpleServer.java

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

class testobject implements Serializable {

    int value;
    String id;

    public testobject(int v, String s) {
        this.value = v;
        this.id = s;
    }
}

public class SimpleServer {

    public static void main(String args[]) {
        int port = 2002;
        try {
            ServerSocket ss = new ServerSocket(port);
            Socket s = ss.accept();
            InputStream is = s.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            testobject to = (testobject) ois.readObject();
            if (to != null) {
                System.out.println(to.id);
            }
            System.out.println((String) ois.readObject());
            is.close();
            s.close();
            ss.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
2
  • 3
    Are both classes in exactly the same package? Given that you have not declared a serialVersionUID, do both classes exactly the same content? More to the point; why not compile TestObject as a separate unit and use it in both the other projects? Finally classes in Java are always in PascalCase, no excuses. Commented Jul 30, 2016 at 12:41
  • You should make a JAR file for your "core" objects and use that as a library between your two projects. Don't make two separate Java classes that "look" the same Commented Jul 30, 2016 at 12:47

2 Answers 2

2

From the fact that you've repeated the testobject class in your post, it is evident that you think you can use two copies of it. You can't. You have to use the same one at both ends, in the same package. Preferably the same binary.

NB readObject() only returns null if you sent a null.

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

Comments

0

As @EJP mentioned, the client and server should use same testobject class file.

To verify it, add System.out.println("From client: " + to) and System.out.println("From server: " + to) to print full path name of object combined both package name and Class name.

Also, you can use keyword instance of to make sure the class type of object is what you want before using it.

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.