Can not read number send with DataOutputStream

前端 未结 2 1242
时光说笑
时光说笑 2021-01-29 03:55

this is my Client code

Random rand = new Random();
int  n = rand.nextInt(50) + 1;
DataInputStream dis = new DataInputStream(_socket.getInputStream());
DataOutput         


        
相关标签:
2条回答
  • 2021-01-29 04:18

    The client needs to either call dos.flush() or dos.close() to cause buffered data to be pushed to the server. (If you intend to write more data, then flush, otherwise close.)

    The server side needs to read the data using readInt on the DataInputStream instance. Data that is written using a DataOutputStream should be read using a DataInputStream.

    Finally, get rid of BufferedReader/InputStreamReader. It is just wrong.

    0 讨论(0)
  • 2021-01-29 04:32

    If you're using writeInt() on the write side, you should be using readInt() on the read side (all the method names correlate between DataOutputStream and DataInputStream, please read the javadoc).

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