V
V
Vladislav Vinokurov2018-02-12 01:40:49
Java
Vladislav Vinokurov, 2018-02-12 01:40:49

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

2 answer(s)
D
Dmitry Alexandrov, 2018-02-12
@KDoV

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);

on the receiving side something like this
ObjectInputStream in ...;
YouClass object = (YouClass)in.readObject();

2) If you want to take something ready, then look towards kryonet, everything is quick and easy.
3) If you still want your own but don’t really want to fool around with serialization, then see protobuf.
4) You can still go the classic way. In the class you want to pass, create a constructor with the required fields, then transfer this class to json\xml\ with a regular array of delimited text and pass it as a string, parse this string on the receiving line and create an object through the constructor. For automation, you can take for example Gson in which everything will be reduced to this form
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);

A
alfss, 2018-02-12
@alfss

I suggest looking towards protobuf. In general, you can exchange using json + base64.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question