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);
        }
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Vignesh VRT
  • 399
  • 3
  • 15
  • 3
    Are both classes in **exactly the same** package? Given that you have not declared a [serialVersionUID](https://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it), 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**. – Boris the Spider Jul 30 '16 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 – OneCricketeer Jul 30 '16 at 12:47

2 Answers2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
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.

Eugene
  • 10,627
  • 5
  • 49
  • 67