Hello. Wrote chat on sockets. Everything works well. There, the entire transmission is based on the lines. The question is, is it possible to transfer objects or arrays using a socket in Java?
2 answers
A common approach is to serialize data into a textual representation on the transmitting side and deserialization on the receiving side. As the format, you can choose XML, JSON, BSON, etc.
class Sample { private int value = 0; Sample() { } } // Сериализация Sample sample = new Sample(); Gson gson = new Gson(); String json = gson.toJson(sample); // json содержит {"value":0} // Десериализация Sample sample = gson.fromJson(json, Sample.class);
|
Java has built-in wrappers for binary input / output streams - ObjectInputStream and ObjectOutputStream. Perhaps they will help you.
|