Socket send and receive byte array

后端 未结 4 1777
名媛妹妹
名媛妹妹 2020-12-05 04:44

In server, I have send a byte array to client through Java socket

byte[] message = ... ;

DataOutputStream dout = new DataOutputStream(client.getOutputStream         


        
4条回答
  •  情话喂你
    2020-12-05 05:27

    You need to either have the message be a fixed size, or you need to send the size or you need to use some separator characters.

    This is the easiest case for a known size (100 bytes):

    in = new DataInputStream(server.getInputStream());
    byte[] message = new byte[100]; // the well known size
    in.readFully(message);
    

    In this case DataInputStream makes sense as it offers readFully(). If you don't use it, you need to loop yourself until the expected number of bytes is read.

提交回复
热议问题