简体   繁体   中英

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? The attribute of Person are nome and 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.

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).

For JSON please try Jackson a High-performance JSON processor .

For XML please check this stackoverflow link 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".

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.

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). 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 . Other protocols that will deal with this "data transport" are XML-based or JSON-based protocols. 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. For XML, you have Java native support, xerces2, and so on.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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