简体   繁体   English

Java-使用Scanner从Socket InputStream读取

[英]Java - Using Scanner reading from Socket InputStream

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 我有一个用Java编写的服务器-客户端程序,该服务器发送一个以换行符\\n结尾的字符串,如果使用Scanner输入下一行,则客户端将从socket.getInputStream()读取

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. show(ID)是一个返回字符串的函数,该字符串中已经包含下一行字符。 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. "finished reading server respone"从不打印,并且没有has next通过发送最后的行后Server打印即假定不存在下一行字。 However the else block is never executed. 但是, else块永远不会执行。 Strangely when I terminate the Server , it is printed. 奇怪的是,当我终止Server ,它被打印了。 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? 任何人都可以向我解释一下,我不需要终止服务器就可以打印该行吗?

You are probably not closing the PrintWriter in the server side. 您可能没有关闭服务器端的PrintWriter。

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. 由于SocketPrintWriter实现了自动关闭的接口,因此在服务器使用完资源后,您可以使用try with resources块来关闭资源。

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. 当您在客户端的扫描器中调用hasNextLine方法时,它将阻止等待输入。

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. 如果关闭服务器,则流将关闭,方法hasNextLine返回false,并且已finished reading server response将显示在客户端中。

From Scanner#hasNextLine documentation: Scanner#hasNextLine文档中:

Returns true if there is another line in the input of this scanner. 如果此扫描仪的输入中还有另一行,则返回true。 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. 这意味着它会返回true ,如果下一行是可用的, 而不是它会返回false时,没有下一行可用于处理。 It might be waiting for another line to show up, though. 不过,它可能正在等待另一行显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM