In a client-server environment, after client connects
sock = new Socket(serverName, SERVER_PORT);
fromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
toServer = new PrintStream(sock.getOutputStream());
toServer.println(json.toString());
I’d like to send a server a bean, which would contain information about client’s state.
The only thing that comes to mind is create a bean populate it with data, use Xstream to transform the bean to json format, send json to server and reassemble it on server side.
Is there a simpler way?
Yes a simpler way would be use to use Java’s serialization framework to do all that for you by simply having your object implement Serializable. You would wrap your sockets outputstream with an ObjectOutputStream and simply tell it to write your object. There a lot of gotchas when using Java’s framework that you should be aware of, but it’s still a good fit for many projects.