How to send object to server in java

I create my class in the client and use the server to communicate between the client and server, but I have a problem converting this class. I send an object of my class from the client to the server, but the server notices this object from the same class.

My class implements Serializable, and I use ObjectInputStream and ObjectOutputStream to send and receive this object.

How can I tell the server that this object belongs to the same class as on the client?

//Client site.
toServer = new ObjectOutputStream(myClient.getOutputStream());
fromServer = new ObjectInputStream(myClient.getInputStream());
toServer.writeObject(new MyClass("12345",12345));


//Server site.
fromClient = new ObjectInputStream(connectFromClient.getInputStream());
toClient = new ObjectOutputStream(connectFromClient.getOutputStream());
MyClass message = (MyClass) fromClient.readObject();
+3
source share
2 answers

Create a third project in which there are classes used by both the client and the server, and create a jar. Then add this jar to the server class path and client application.

+3
source

, , . , Demo demo.bin , . , , , .

+3

Source: https://habr.com/ru/post/1791865/


All Articles