执行readLine()时套接字卡住了 [英] Socket gets stuck when doing readLine()

查看:136
本文介绍了执行readLine()时套接字卡住了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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 命令时,我得到了完整的电子邮件列表。所以我应该从套接字得到相同的响应,但我不是。我该如何从服务器逐行读取整个响应?

On the second in.readLine(), the application gets stuck at here and doesn't proceed from here. When I run the LIST command 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?

推荐答案

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

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.

如果POP服务器没有,我不会感到惊讶实际上它在消息的末尾标记了\\ n。请尝试使用read()。

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

这篇关于执行readLine()时套接字卡住了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆