java 执行 readLine() 时套接字卡住

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7757544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:17:39  来源:igfitidea点击:

Socket gets stuck when doing readLine()

javasockets

提问by Carven

I am trying to connect to the POP server through Sockets in Java. I did the following code to run a LIST command to list all the emails from the server. But I don't know why on the second readLine() to read the second line and onwards, my application hangs at there.

我正在尝试通过 Java 中的套接字连接到 POP 服务器。我执行了以下代码来运行 LIST 命令以列出来自服务器的所有电子邮件。但我不知道为什么在第二个 readLine() 读取第二行及以后,我的应用程序挂在那里。

popSock = new Socket(mailHost, pop_PORT);
inn = popSock.getInputStream();
outt = popSock.getOutputStream();
in = new BufferedReader(new InputStreamReader(inn));
out = new PrintWriter(new OutputStreamWriter(outt), true);

//USER and PASS commands to auth the server are ok

out.println("LIST");
String response = in.readLine();
System.out.println(response);

//Attempt to read the second line from the buffer but it hangs at here.
response = in.readLine();
System.out.println(response);

On the second in.readLine(), the application gets stuck at here and doesn't proceed from here. When I run the LISTcommand on telnet, I get the whole list of emails. So I should get the same response from the socket but I am not. How should I read the whole response line by line from the server?

在第二个in.readLine(),应用程序卡在这里,不能从这里继续。当我LIST在 telnet 上运行命令时,我得到了整个电子邮件列表。所以我应该从套接字得到相同的响应,但我不是。我应该如何从服务器逐行读取整个响应?

回答by Uffe

readLine() won't return until it's read a carriage return or a line feed, which is what you normally get when you read from a terminal or a text file.

readLine() 在读取回车或换行之前不会返回,这是您从终端或文本文件读取时通常得到的。

I wouldn't be surprised if the POP server doesn't actually tack \r\n on the end of its messages. Try read() instead.

如果 POP 服务器实际上没有在其消息的末尾加上 \r\n,我不会感到惊讶。改为尝试 read() 。

回答by Triton Man

You should be sending \r\n after each command, also, try not using a BufferedInputStream, try reading directly from the InputStream byte by byte to see at which point it actually hangs. The BufferedInputStream may be hanging waiting to read more before returning what it has already read.

您应该在每个命令之后发送 \r\n,此外,请尝试不使用 BufferedInputStream,尝试直接从 InputStream 逐字节读取以查看它实际挂起的时间点。BufferedInputStream 可能会挂起等待读取更多内容,然后再返回它已经读取的内容。

回答by Jagat

Try reading it one character at a time using in.readand printing it. Perhaps, there's an issue with the newline character that the server is sending.

尝试使用in.read并打印它一次读取一个字符。也许,服务器发送的换行符存在问题。

回答by Snehal Indurkar

You can try following--

你可以试试以下——

    try {
        String line = inn.readLine();
        while(***input.ready()***)
        {
            System.out.println(line);
            line=inn.readLine();

        }
        inn.close();


    } catch (IOException e) {

        e.printStackTrace();
    }

where inn is your bufferedReader object whih stores the inputstreamdata

其中 inn 是您的 bufferedReader 对象,其中存储输入流数据