1

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);

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?

Ren
  • 1,111
  • 5
  • 15
  • 24
Carven
  • 14,988
  • 29
  • 118
  • 161
  • Well, your first readLine() will give the welcome message. So it seems likely that readLine() is blocking because the LIST didn't work right. Maybe your POP server is strictly RFC compliant? Does out.print("LIST\r\n") change the behavior? – Edward Thomson Oct 13 '11 at 16:57
  • Unfortunately, adding \r\n still doesn't work. The LIST command does work. I could see the first line of the response. But after which, the application hangs when I want to read the second line and onward. – Carven Oct 13 '11 at 17:01
  • what did you receive as first line after sending `LIST`? – ratchet freak Oct 13 '11 at 17:10
  • I receive this: +OK 3545 62967125. So it means that the `LIST` works. But there should more lines after this one, which is the whole list of emails. – Carven Oct 13 '11 at 17:15
  • any reason not to use a java pop3 library? – artbristol Oct 13 '11 at 17:59
  • It's a requirement for a school assignment that I must use the Socket to implement. – Carven Oct 13 '11 at 18:01
  • Please add the homework tag for school assignments. – Uffe Oct 13 '11 at 18:11

4 Answers4

6

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.

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

Uffe
  • 10,396
  • 1
  • 33
  • 40
  • It appears that that the readLine() is still waiting for a response from the server and therefore hangs at there. A check with `while (String tmp = in.readLine()) != null)` doesn't work. It still go beyond the lines of the response. Why doesn't the check in the while loop work? – Carven Oct 14 '11 at 16:32
  • any example please for implementing read() instead of readLine() calls ? – gumuruh Apr 02 '22 at 19:24
2

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.

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106
-1

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

-1

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.

Jagat
  • 1,392
  • 2
  • 15
  • 25