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

时间:2021-06-21 15:34:40

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 okout.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 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?

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

4 个解决方案

#1


5  

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()。

#2


1  

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可能会挂起等待阅读更多内容。

#3


0  

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

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

#4


0  

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是存储inputstreamdata的bufferedReader对象

#1


5  

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()。

#2


1  

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可能会挂起等待阅读更多内容。

#3


0  

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

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

#4


0  

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是存储inputstreamdata的bufferedReader对象