Sending and Receiving 2D Arrays java

前端 未结 2 1918
名媛妹妹
名媛妹妹 2021-01-15 12:00

What I would like to achieve by asking this question is to learn how to send and receive 2-Dimensional Arrays to another computer.

The context is that the <

相关标签:
2条回答
  • 2021-01-15 12:20

    This will work with any primitive type arrays and Object arrays if object type is instanceof Serializable

    server

        ServerSocket ss = new ServerSocket(port);
        Socket s = ss.accept();
        ObjectInputStream is = new ObjectInputStream(s.getInputStream());
        byte[][] array = (byte[][])is.readObject();
    

    client

        byte[][] array = new byte[10][10];
        Socket s = new Socket("host", port);
        ObjectOutputStream os = new ObjectOutputStream(s.getOutputStream());
        os.writeObject(array);
    
    0 讨论(0)
  • 2021-01-15 12:25

    You should create an object out of it. It´s easier to handle afterwards.
    After that you serialize it, then you split it in smaller byte[] (if it´s bigger then 64 kb). You add some bytes to each packet, I prefer
    byte[0] = is splited?
    byte[1] + byte[2] = sendNumber (just for controlling, it increases after every sent object)
    byte[3] = how many parts your object has
    byte[4] = what part is it
    What you need else.
    Afterwards you can easily put the parts together.
    I have never used it but i also found a library know as kryonet (while googling).
    Have a look... google it properly.

    0 讨论(0)
提交回复
热议问题