S
S
Stavr Strelov2020-07-14 08:38:20
Java
Stavr Strelov, 2020-07-14 08:38:20

How to transfer data of type Object from the server to the client?

I'm making a PC game and recently started making a server for it. Everything was conceived as follows: the client turns on the game on his computer and registers his account, the program sends an object of the String type to the server, the server accepts it and sends the client a ready registered and saved account. But the trouble is, only data of String and int types can be transferred via Socket!

The server sends an object of type Account to the client:

ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.println(new Account);
writer.close();

The client tries to get it, but problems arise:
Socket socket = new Socket(host, port);
InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
BufferedReader bufferedReader = new BufferedReader(streamReader);
Account account = (Account) bufferedReader.read(); //Не работает, так как является типом int
bufferedReader.close();


What to do with it? Are there any other ways to transfer data from the server to the client? I heard that serialization can somehow help with this, but I did not understand how.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-07-14
@IAmNoob

Serialization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question