通过 Internet 发送对象 [英] Sending an object over the Internet

查看:65
本文介绍了通过 Internet 发送对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个类,然后我设置了一个该类类型的对象.我想将此对象透明地发送到在另一台计算机上运行的另一个 Java 应用程序.实现这一目标的最佳技术是什么?

I define a class, and then I instate an object of that class type. I want to send this object to another Java application running on a different computer transparently. What is the best technology to accomplish this?

推荐答案

您可以使用 Java API 创建对象流并发送任何可序列化的对象.但您必须注意这些通过网络未加密:

you can create object streams using the java API and send any serializable object. but you'll have to mind that these go unencrypted through the network:

在发送方:

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();

这篇关于通过 Internet 发送对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆