Answer the question
In order to leave comments, you need to log in
Passing through a Socket object?
I am writing a messenger (only as a practice) and a problem arose - now it is implemented to transfer only string data, but at this stage this is no longer suitable. At least I would like to transfer entire objects to the server and back - for example, an object responsible for registration (a certain class that will store the required amount of information for this registration), responsible for logging in, for sending messages to all clients, for sending a message to a specific user (Need to transfer , at least the message and the id of the user to whom I will transfer) and so on. My first thought is the objects of the "Messege" class heirs that will perform these functions, respectively, an array of bytes on the client, decryption on the server side, but I really would not want to do it anyhow, I would like to do it the way it should be done, regardless of the degree of difficulty. Perhaps it’s worth sending JSONs at all and parsing them on the server? Or something else? I will be glad to any answers, instructions, recommendations and advice. Actually, everything dances from the fact that the method below no longer provides the necessary functionality and instead of public synchronized void sendMessege(String messege) at least public synchronized void sendMessege(Object messege) is needed, and accordingly a new implementation, it remains to find out how bad this solution is and/ Or what is the best way to replace it. Thank you all in advance! that the method below no longer provides the desired functionality and instead of synchronize publicd void sendMessege(String messege) at least public synchronized void sendMessege(Object messege) is needed, and accordingly a new implementation, it remains to find out how bad this solution is and / or how it is better to replace it. Thank you all in advance! that the method below no longer provides the desired functionality and instead of synchronize publicd void sendMessege(String messege) at least public synchronized void sendMessege(Object messege) is needed, and accordingly a new implementation, it remains to find out how bad this solution is and / or how it is better to replace it. Thank you all in advance!
public synchronized void sendMessege(String messege){
/**
* Метод для отправки сообщений
*/
try {
out.write(messege+ "\r\n");
out.flush();
} catch (IOException e) {
listener.sExeption(e);
}
}
Answer the question
In order to leave comments, you need to log in
1) If you want your own collective farm, then implement the Serializable interface in the required class, there will be something on the transmission side
ObjectOutputStream out ...;
out.writeObject(object);
ObjectInputStream in ...;
YouClass object = (YouClass)in.readObject();
public static class Entity {
int id;
String name;
//другие типы и данные, если класс то аналогичным образом описываете
public Entity(int id, String name) {
this.id = id;
this.name = name;
}
}String json = gson.toJson(entity); // {"id":100,"name":"name"}
Entity read = gson.fromJson(json, Entity.class);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question