[英]Socket gets stuck when doing readLine()
我正在尝试通过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);
在第二个in.readLine()
,应用程序卡在此处,无法从此处继续。 当我在telnet上运行LIST
命令时,我会收到整个电子邮件列表。 因此,我应该从套接字获得相同的响应,但事实并非如此。 我应该如何从服务器逐行读取整个响应?
readLine()在读取回车符或换行符之前不会返回,这是从终端或文本文件读取时通常会得到的结果。
如果POP服务器没有在其消息末尾真正添加\\ r \\ n,我不会感到惊讶。 尝试改为read()。
您应该在每个命令之后发送\\ r \\ n,也不要尝试使用BufferedInputStream,尝试直接从InputStream逐字节读取以查看其实际挂起的位置。 BufferedInputStream可能正在挂起,等待读取更多内容,然后返回已读取的内容。
您可以尝试关注-
try {
String line = inn.readLine();
while(***input.ready()***)
{
System.out.println(line);
line=inn.readLine();
}
inn.close();
} catch (IOException e) {
e.printStackTrace();
}
inn是您的bufferedReader对象,用于存储inputstreamdata
尝试使用in.read
并一次打印一个字符。 也许,服务器正在发送的换行符存在问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.