简体   繁体   English

如何通过TCP连接发送类?

[英]How to send class over TCP connection?

I am trying to send a class over a TCP connection using java. 我正在尝试使用java通过TCP连接发送一个类。 When the client receives the class it should invoke the methods of that class and return the result to the server. 当客户端收到类时,它应该调用该类的方法并将结果返回给服务器。

I have serialized the class to a byte array to send to the client, but I don't know with it once the client receives it. 我已经将类序列化为一个字节数组发送给客户端,但是一旦客户端收到它,我就不知道了。

Thank you. 谢谢。

Your question is a bit ambiguous. 你的问题有点含糊不清。 Are you sending a *.class file or an instance of the class? 您是在发送*.class文件还是该类的实例 I'll bet that you actually mean an instance of the class since you literally said that you want to send it back. 我敢打赌,你实际上是指一个类的实例,因为你确实说要将它发回去。 This makes only sense if it were an instance. 只有它是一个实例才有意义。 The other side should then have the class file in its classpath as well. 另一方也应该在其类路径中包含类文件。 Then you can just import the class and cast to the desired class on readObject() . 然后,你可以import类和转换为所需类上readObject() Finally you'll be able to invoke methods on it according the class' contract. 最后,您将能够根据类合同调用其上的方法。

See also: 也可以看看:


If you're actually sending a *.class file, using a ClassLoader to load it would indeed be the answer. 如果您实际上正在发送*.class文件,那么使用ClassLoader加载它确实是答案。

您可能需要使用ClassLoader

如果可能,您可能需要查看RMI,客户端可以在其中提供要调用的服务器的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM