1

I have a server and client running on local server.

I read from the server this way:

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

public static String readResponse() throws IOException{
    String response = "";
    String line;
    while((line = br.readLine()) != ""){
        System.out.println("s: " + line);
    }
    return response;

}

And I get the response from server but the program stops and doesn't go anywhere from there:

Please enter option number: 1 
c: MSGGET
s: 200 OK
s: Go for it now. The future is promised to no one.

And it just hangs here, when it is suppose to continue.

I also tried:

while((line = br.readLine()) != null){

It just keeps waiting. Is there anything maybe on the server that I have to change to tell the client that I am done transmitting data.

Please help! Thank you!

Nazariy
  • 717
  • 6
  • 23

3 Answers3

1
while((line = br.readLine()) != ""){
        System.out.println("s: " + line);
    }
    return response;

}

Unless your peer is planning to transmit a blank line as an end-of-message sentinel, this loop is pointless, and it also compares Strings incorrectly.

And I get the response from server but the program stops and doesn't go anywhere from there.

It is waiting for an empty line that never arrives. And it is ignoring the end of stream condition.

while((line = br.readLine()) != null){

Now this loop is correct, but it won't exit until end of stream, which won't happen until the peer closes the connection.

It just keeps waiting.

That's what it's supposed to do.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • In that case should the server break the connection? and I just do while(br.ready()) ? – Nazariy Feb 04 '16 at 04:14
  • 1
    The first question depends entirely on your application protocol. If you're only expecting one response, the server should close the connection after sending it. Otherwise you need to adjust your loop according to your protocol: maybe your server only sends one line per response? I don't know. Do *not* use `ready()` or `available()`. That's not what they're for. – user207421 Feb 04 '16 at 05:27
-1

Try this one :

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    public static String readResponse() throws IOException{
        String response = "";
        String line;
        while((line = br.readLine()) != "" || (line = br.readLine()) != null)){
            System.out.println("s: " + line);
        }
        return response;

    }

if this is not working then you need to check if you are sending some special character on end of stream from server. Then apply check for that character.

  • Testing it for `!= ""` is completely pointless, and the second `readLine()` call throws away a line of input. – user207421 Feb 04 '16 at 04:04
  • edited my answer for bracket mistake. please restore. I checked for != "" just as a sample. I have written below that if there is a special character to denote the end of stream then we should apply check for that – Damanpreet Singh Feb 04 '16 at 04:32
  • You haven't corrected either of the problems I mentioned. – user207421 Feb 04 '16 at 05:28
-1

I ended up using this:

while(!(line = br.readLine()).equals("exit")){

And I asked the person responsible for the server to print the following when he is done writing a response:

//this was initiated at the beginning of the program
out = new PrintWriter(socket.getOutputStream(), true);

out.println("exit");

This seems to works well for me so far! Thank you everyone for your help!

Nazariy
  • 717
  • 6
  • 23
  • This will loop indefinitely if end of stream arrives before "exit". You have to test for null before anything else. – user207421 Feb 04 '16 at 20:07