How to send an OBJECT over TCP in java?

前端 未结 3 1730
耶瑟儿~
耶瑟儿~ 2020-12-12 03:45

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

相关标签:
3条回答
  • 2020-12-12 04:15

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

    0 讨论(0)
  • 2020-12-12 04:18

    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.

    0 讨论(0)
  • 2020-12-12 04:24

    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.

    0 讨论(0)
提交回复
热议问题