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

Car*_*ven 1 java sockets

我试图通过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);
Run Code Online (Sandbox Code Playgroud)

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

Uff*_*ffe 6

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

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