我定义了一个类,然后我实现了该类类型的对象.我想将此对象透明地发送到另一台计算机上运行的另一个Java应用程序.实现这一目标的最佳技术是什么?
您可以使用java API创建对象流并发送任何可序列化的对象.但你必须要记住,这些通过网络未加密:
在发件人方面:
CustomObject objectToSend=new CustomObject(); Socket s = new Socket("yourhostname", 1234); ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream()); out.writeObject(objectToSend); out.flush();
在接收端:
ServerSocket server = new ServerSocket(1234); Socket s = server.accept(); ObjectInputStream in = new ObjectInputStream(s.getInputStream()); CustomObject objectReceived = (CustomObject) in.readObject();
翻译自:https://stackoverflow.com/questions/1931466/sending-an-object-over-the-internet