Socket gets stuck while doing readLine ()

I am trying to connect to a POP server through Sockets in Java. I executed the following code to run the LIST command to display all emails from the server. But I do not know why, on the second readLine (), to read the second line and on, my application hangs 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() application is stuck here and does not come from here. When I run the LIST command on telnet, I get the entire list of letters. So I have to get the same answer from the socket, but I don’t know. How should I read the entire answer line by line from the server?

+1
source share
4 answers

readLine () will not return until it reads a carriage return or line, which you usually get when reading from a terminal or text file.

I would not be surprised if the POP server does not actually stop at the end of its messages. Try read () instead.

+5
source

You should send \ r \ n after each command, also do not try to use BufferedInputStream, try reading directly from the InputStream by byte byte to see at what point it actually hangs. BufferedInputStream may be hanging, waiting to read more before returning what it has already read.

+1
source

Try reading it one character at a time using in.read and printing it. There may be a problem with the newline that the server sends.

0
source

You can try the 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 in which inputstreamdata data is stored p>

0
source

All Articles