1

I am sorry because I can't think of any better title. I has a server-client program written in java that server send a string that end with new line character \n the client read from socket.getInputStream() if input has next line by using Scanner

on server-side

ServerSocket serverSocket = new ServerSocket(PORT);
Socket socket = serverSocket.accept();
PrintWrite send = new PrintWriter(socket.getOutputStream);
send.print("Server has succeeded to proccess your request \n" + show(ID));
send.flush();

The show(ID) is a function that return a string which already contain next line character in it. on client side

Socket socket = new Socket(HOST,PORT);
Scanner read = new Scanner(socket.getInputStream);
public Boolean ServerResponse(){
    System.out.println("please wait");
    Boolean result=false;
    while(true){
        if(read.hasNextLine()){
            System.out.println("has next");
            String msg = read.nextLine();
            System.out.println(msg);
            if(msg.contains("succeeded")){
                result = true;
            }

        }else{
            System.out.println("finished reading server respone");
            return result;
        }

    }
}

when I tried to run this. "finished reading server respone" is never printed and there is no has next after the last line send by Server is printed which assume that there is no next line character. However the else block is never executed. Strangely when I terminate the Server, it is printed. Can anyone please explain this to me and what I must do that I don't need to terminate the server for that line to be printed?

2 Answers 2

2

From Scanner#hasNextLine documentation:

Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.

Meaning that it will return true if next line is available, not that it will return false when no next line is available for processing. It might be waiting for another line to show up, though.

Sign up to request clarification or add additional context in comments.

1 Comment

So that mean it only return false if there is input but without next line. For no input it will block? Thank you
1

You are probably not closing the PrintWriter in the server side.

As Socket and PrintWriter implements autoclosable interface, you can use a try with resources block to close the resources when the server has finished with them.

try (Socket socket = serverSocket.accept();
     PrintWriter send = new PrintWriter(socket.getOutputStream);) {
    send.print("Server has succeeded to proccess your request \n" + show(ID));
}

When you call the method hasNextLine in your scanner in the client side, it blocks waiting for input.

If you shutdown your server, the stream is closed and the method hasNextLine return false and the finished reading server response is displayed in the client.

2 Comments

So that mean after each time Server sending something, it must close the PrintWriter and the next time it must create new PrintWriter. Is closing PrintWriter close the OuputStream as well?
Your client is being blocked in the hasNextLine method waiting for input from the server. If the server is not going to send anything more and close the stream, the client will print finished reading server respone. If your server is going to send more data, you shouldn't close the stream and I guess your client is OK waiting for more input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.