Cannot send and receive data using BufferedReader/Writer on sockets

后端 未结 2 1511
[愿得一人]
[愿得一人] 2021-01-21 13:06

I am making a client-server application and am using the following code for the exchange of data b/w the server and clients.

Server\'s send and receive code:

<         


        
2条回答
  •  醉话见心
    2021-01-21 13:38

    Don't keep creating new readers and writers. Use the same ones for the life of the socket. You're losing data in the old BufferedReader that is being discarded. These buffered things, err, buffer.

    Also this:

    String message=input.readLine();
    while(message.length()!=0)
    

    is, err, nonsense. Firstly you're not checking message for null, so you're going to get a NullPointerException at end of stream. Secondly, you aren't doing any further input inside the loop so if it doesn't throw NPE and the message isn't zero length it will loop forever.

提交回复
热议问题