0

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());
    ?????
  • possible duplicate of [Sending an object through a socket in java](http://stackoverflow.com/questions/19217420/sending-an-object-through-a-socket-in-java) – plalx May 10 '14 at 13:02
  • 1
    You can't drive a car without having a car. You can't take the plane without plane. You can't read an object of class Person without having the Person class. – JB Nizet May 10 '14 at 13:04

2 Answers2

0

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 (e.g. HashMap).

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

For XML please check this stackoverflow link best-xml-parser-for-java.

Community
  • 1
  • 1
xfeep
  • 1,091
  • 11
  • 12
  • Server and client are in different projects.. can I serialize the object?? – user3608898 May 10 '14 at 14:36
  • p.s. On the server there must be only database (the object Person it will insert into db) – user3608898 May 10 '14 at 14:39
  • use Jackson, on the client side you can serialize java object to JSON text. e.g. {"name" : "Tom" , "age" 12}, on the server side you can deserialize the JSON text to java map. – xfeep May 10 '14 at 15:23
  • If you shared the same class Person between Server side and client side, Jackson can be used to serialize Person objects to JSON text and deserialize JSON text to Person objects. – xfeep May 10 '14 at 15:29
  • I don't think use JSON, because from server I can't get from file. – user3608898 May 11 '14 at 10:29
  • You can't do this , you can't do that . So you can't do anything but ask question. :) – xfeep May 11 '14 at 11:55
  • you're right:) One last thing, I am using JSON, from client I have write on file txt (converted from object Person to JSON). Now, from server how get this file? I make a cast HashMap? Thank you for your answer :) – user3608898 May 12 '14 at 07:36
  • You need not use file, you can convert object Person to JSON (java String) by Jackson, then convert it to bytes, write to socket outputstream, then flush or close. From the server you just read the bytes and convert them to string , then convert string to map by Jackson. – xfeep May 12 '14 at 09:09
0

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.

Community
  • 1
  • 1
Leo
  • 6,480
  • 4
  • 37
  • 52
  • Use RMI, I must implement a interface remote, but I must have only server and client in two different projects (and different workspace). So, how can I do? – user3608898 May 11 '14 at 10:34
  • You may find this tutorial somewhat useful http://code.nomad-labs.com/2010/03/26/an-improved-rmi-tutorial-with-eclipse/ – Leo May 11 '14 at 10:43
  • I can't use 3 project because, I must use only client and server in two different workspace. So, if I put a interface remote on client, the server it can't see this interface, right? – user3608898 May 11 '14 at 11:03