Sending and Receiving 2D Arrays java

前端 未结 2 1919
名媛妹妹
名媛妹妹 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);
    

提交回复
热议问题