简体   繁体   English

如何通过套接字连接发送实例?

[英]How can I send an instance over a socket connection?

I'm attempting to write two Java programs. 我正在尝试编写两个Java程序。 One to simulate a server, and one to simulate a client. 一个模拟服务器,一个模拟客户端。

How could I go about sending an instance of a Response class over a socket? 我怎样才能通过套接字发送Response类的实例?

The Response class represents status codes of the server connection. Response类表示服务器连接的状态代码。 eg 404 Not Found etc 例如404 Not Found等

I'm not allowed to use Serialisation unfortunately. 不幸的是,我不允许使用序列化。

Any advice would be greatly appreciated. 任何建议将不胜感激。

At some level Serialization must occur in order to send an object across a connection. 在某种程度上,必须进行序列化才能通过连接发送对象。 I can only assume your comment about being not allowed to use serialization refers to not being able to use Serializable instead of a blanket prohibition of serialization(which makes no sense). 我只能假设您对不允许使用序列化的评论指的是无法使用Serializable而不是全面禁止序列化(这没有任何意义)。 A very simple method to accomplish this would be the use of a external serialization library such as gson . 实现此目的的一种非常简单的方法是使用外部序列化库,例如gson Gson serializes an object into a JSON string that you can transmit over your socket and then using the same library deserialize it back into an object on the other side. Gson将对象序列化为JSON字符串,您可以通过套接字传输该字符串,然后使用相同的库将其反序列化为另一端的对象。 You can of course use any of your preferred serialization libraries with your favorite format eg. 您当然可以使用您喜欢的格式的任何首选序列化库,例如。 XML, json, YAML,... XML,json,YAML,......

You wouldn't be sending an instance of the Response class itself. 您不会发送Response类本身的实例。 When sending things over a network, client and server machines understand bytes. 通过网络发送内容时,客户端和服务器计算机可以理解字节。 Your application can understand more than bytes, it can understand specific representations. 您的应用程序可以理解多于字节,它可以理解特定的表示。 For example, your server might send a JSON representation of your Response class like: 例如,您的服务器可能会发送您的Response类的JSON表示,如:

{
   "response" : {
       "code":404
   }
}

Then your client must be able to understand what this sequence of bytes means. 然后您的客户端必须能够理解这个字节序列的含义。 That's basically what a protocol is: how two machines can communicate. 这基本上就是一个协议:两台机器如何通信。

Regardless of what language the server or clients are written in, the Response is an Entity. 无论服务器或客户端使用何种语言编写,响应都是实体。 In Java you might use a Class to represent it, in C++ you might use a struct . 在Java中,您可以使用Class来表示它,在C ++中您可以使用struct However, both would need to know that when you are communicating with an external application.system, they would have to put it in a format that everyone understands, be it json, xml, or any other. 但是,两者都需要知道,当您与外部应用程序系统进行通信时,他们必须将其置于每个人都能理解的格式中,无论是json,xml还是其他任何格式。

As for sending this through sockets, Oracle has a nice tutorial here . 至于通过套接字发送这个,Oracle 在这里有一个很好的教程 You get the OutputStream from the socket and start writing your representation. 您从套接字获取OutputStream并开始编写表示。

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

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