简体   繁体   English

如何在Java中通过TCP / IP网络连接发送用户定义的类对象?

[英]How do you send a User defined class object over a tcp/ip network connection in java?

This is an example of a user defined class I'd like to send from a client application to a server application: 这是我想从客户端应用程序发送到服务器应用程序的用户定义类的示例:

class dataStruct implements Serializable{
    byte data;
    int messageNum;
    public void setData(byte datum, int messageNumber){
        data=datum;
        messageNum=messageNumber;
    }
}

How do you send a user defined class over a tcp/ip connection in java? 如何在Java中通过TCP / IP连接发送用户定义的类?

What types of streams can I use to accomplish this (if I'm sending more than just text)? 我可以使用什么类型的流来完成此操作(如果我发送的不仅仅是文本)?

Can I pass a full object via a socket stream, or will I always have to cast it after it has been passed via a stream? 我可以通过套接字流传递完整的对象,还是在通过流传递对象后始终将其强制转换?

I'm writing a server/client application, and I've only been able to find tutorials with examples of primitive types or strings being passed over a network connection - not user defined types. 我正在编写服务器/客户端应用程序,但我只能找到包含通过网络连接传递的原始类型或字符串示例的教程,而不是用户定义的类型。

Your help and direction are greatly appreciated. 非常感谢您的帮助和指导。

Use an ObjectOutputStream on the sending side and an ObjectInputStream on the receiving side. 在发送方使用ObjectOutputStream,在接收方使用ObjectInputStream。

To be a bit more clear, here is an example (without any exception handling). 为了更清楚一点,这是一个示例(没有任何异常处理)。

sending side: 发送方:

dataStruct ds = ...;
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(ds);
oos.close();

receiving side: 接收方:

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
Object o = ois.readObject();
if(o instanceof dataStruct) {
   dataStruct ds = (dataStruct)o;
   // do something with ds
}
else {
   // something gone wrong - this should not happen if your
   // socket is connected to the sending side above.
}

So yes, you have to cast at the receiving side so the compiler knows the right class. 所以是的,您必须在接收方进行强制转换,以便编译器知道正确的类。 (The casting does not change the class of the object, only changes the compiler's knowledge of it.) (强制转换不会更改对象的类,只会更改编译器对其的了解。)

This Serialization is also usable to save objects to a file. 此序列化还可用于将对象保存到文件。 Of course, this gives only interoperability to Java, if you have a non-Java partner, you might want to use a custom serialization protocol, or some XML-based format. 当然,这仅给Java提供了互操作性,如果您有非Java合作伙伴,则可能要使用自定义序列化协议或某些基于XML的格式。

Let the objects implement the Serializable marker interface, and then transfer the objects using ObjectOutputStream and ObjectInputStream . 让对象实现Serializable标记接口,然后使用ObjectOutputStreamObjectInputStream传输对象。 When the object comes out on the other end, it will be via the readObject() method on ObjectInputStream , which returns Object , so yes, you will need to cast it to the proper type. 当对象在另一端出现时,将通过ObjectInputStream上的readObject()方法返回Object ,因此,是的,您需要将其转换为正确的类型。

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

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