简体   繁体   English

如何从客户端向服务器发送对象?

[英]How send an object to server from client?

i must send an object person of class Person from client to server, but in to the server there isn't the class Person, how can make? 我必须从客户端向服务器发送一个类Person的对象person,但是在服务器中没有Person类,该怎么做? The attribute of Person are nome and cognome. Person的属性是nome和cognome。

//CLIENT //客户

      Socket sock = new Socket("localhost",10000);
      ObjectOutputStream outToClient = new ObjectOutputStream(sock.getOutputStream());
      String nome = "Mario";
      String cognome = "Rossi";
      Giocatore giocatore = new Giocatore(nome,cognome);
      outToClient.writeObject(giocatore);

//SERVER //服务器

    ServerSocket ser = new ServerSocket(10000);
    Socket sock = ser.accept();
    ObjectInputStream inFromClient = new ObjectInputStream(sock.getInputStream());
    ?????

Because the server side has no class Person, so it can not create the object of Person. 因为服务器端没有Person类,所以它不能创建Person对象。

You can serialize the object to XML or JSON text and send it to server, on the server side you can deserialize the XML or JSON to map (eg HashMap). 您可以将对象序列化为XML或JSON文本,然后将其发送到服务器,在服务器端,您可以反序列化XML或JSON以进行映射(例如HashMap)。

For JSON please try Jackson a High-performance JSON processor . 对于JSON,请尝试使用Jackson高性能JSON处理器

For XML please check this stackoverflow link best-xml-parser-for-java . 对于XML,请检查此stackoverflow链接best-xml-parser-for-java

To transmit an object over the wire, both ends must know how to deal with the data. 为了通过电线传输对象,两端必须知道如何处理数据。 One will create a representation of the data and send it, the other end must know what to expect and how to interpret the data. 一个将创建数据的表示形式并将其发送,另一端必须知道期望什么以及如何解释数据。

That's the very basic notion of what we call a "protocol". 这就是我们所谓的“协议”的最基本概念。 It's an agreement with both ends. 这是双方的协议。

In Java, we used to say that you can serialize an object, which means, roughly speaking, that if that object is a set of attributes, you can just send these attributes over the wire and the other side can retrieve these attributes and, knowing the object structure, create a another one and restore its "state". 在Java中,我们曾经说过您可以序列化一个对象,也就是说,粗略地说,如果该对象是一组属性,则可以通过网络发送这些属性,而另一端可以检索这些属性,并且知道对象结构,创建另一个对象并恢复其“状态”。

Notice that, if you have the same object in both ends classpaths, it does not really matter (sometimes or for some people) the static final constant. 请注意,如果两端类路径中的对象相同,则静态最终常量(有时或对某些人而言)并不重要。 There's a whole discussion about serializing or not static attributes here so be careful. 有一个关于序列化或不是静态属性的整个讨论在这里 ,所以要小心。

If you want the very same object in both sides, both must have the same Java class in their classpaths, so the end that will restore it can know where to put each attribute. 如果双方都希望使用相同的对象,则它们的类路径中都必须具有相同的Java类,因此将其还原的末尾可以知道将每个属性放在何处。

Please notice that some data types are not serializable. 请注意,某些数据类型不可序列化。 For example, a data Stream is basically a handler that reads/writes data, but you can't magically serialize it and send it over the wire (or you could in theory send, but it could just not work, if it's a FileReader that is reading from a file that is only available in the sender side). 例如,数据流本质上是一个读取/写入数据的处理程序,但是您无法神奇地对其进行串行化并通过有线发送(或者理论上可以发送,但是如果它是FileReader则无法工作)正在从仅在发送方可用的文件中读取)。 For more details on this, please see this . 有关此的更多详细信息,请参阅

If you have the same object in both ends, and if it's serializable, one way is to use RMI to transfer the object from one end to another, example here . 如果两端都有相同的对象,并且可以序列化,则一种方法是使用RMI将对象从一端转移到另一端, 例如此处 Other protocols that will deal with this "data transport" are XML-based or JSON-based protocols. 将处理此“数据传输”的其他协议是基于XML或基于JSON的协议。 These are text-based protocols that are popular, easy to deal with and that have several nice libraries available and can deal with most serialization needs out there. 这些是基于文本的协议,很流行,易于处理,并且有几个不错的库可供使用,并且可以处理大多数序列化需求。 For JSON you have flexjson, gson and jackson. 对于JSON,您可以使用flexjson,gson和jackson。 For XML, you have Java native support, xerces2, and so on. 对于XML,您具有Java本机支持,xerces2等。

If you don't care about having the same object in the other end (for example, you're going to read the object data but you don't necessarily need to restore another similar object, or you're interested only in some specific fields), you can still use these text-based serialization formats and write your own parser/reader and extract just the data you need too. 如果您不希望在另一端拥有相同的对象(例如,您将要读取对象数据,但不必还原另一个类似的对象,或者您只对某些特定对象感兴趣)字段),您仍然可以使用这些基于文本的序列化格式,并编写自己的解析器/阅读器,也仅提取所需的数据。

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

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