Socket gets stuck when doing readLine()

后端 未结 4 2053
死守一世寂寞
死守一世寂寞 2020-12-07 05:10

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 wh

相关标签:
4条回答
  • 2020-12-07 05:46

    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.

    0 讨论(0)
  • 2020-12-07 05:46

    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

    0 讨论(0)
  • 2020-12-07 05:55

    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.

    0 讨论(0)
  • 2020-12-07 05:59

    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.

    0 讨论(0)
提交回复
热议问题