我试图通过Java中的套接字连接到POP服务器。我做了下面的代码来运行LIST命令来列出来自服务器的所有电子邮件。但我不知道为什么在第二行readLine()读取第二行和之后,我的应用程序挂在那里。套接字在执行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()会给欢迎信息。所以readLine()似乎可能被阻塞,因为LIST无法正常工作。也许你的POP服务器严格遵守RFC? out.print(“LIST \ r \ n”)是否改变了行为? –
不幸的是,添加\ r \ n仍然不起作用。 LIST命令可以工作。我可以看到答复的第一行。但在此之后,当我想要阅读第二行和之后的应用程序时,应用程序会挂起。 – Carven
发送'LIST'后,您作为第一行收到了什么? –