Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6697003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:24:31+00:00 2026-05-26T06:24:31+00:00

I am reading character by character of a socket response in a BufferedReader written

  • 0

I am reading character by character of a socket response in a BufferedReader written from a socket that I am connecting to a SMTP and POP server. But how do I know if I have read until the end of the response to avoid having the read() to hang for reading beyond what the server responded? The following code is how I am reading from the BufferedReader:

boolean endOfResponse = false;
while(!endOfResponse) {
    c = in.read();
    if ((c >= 32 && c < 127) || c == '\t' || c == '\r' || c == '\n') { 
        response.append((char)c);
        //The following check has a problem: If the response has multiple lines, the other lines will not be read.
        if(c == '\n') endOfResponse = true;
    }
}

For instance, in POP, when I do a LIST, the response ends with a .. But in SMTP, when I do a EHLO, it doesn’t give clear indication of when the response has ended.

The use of in.read() == -1, sadly, doesn’t work as it will still attempt to read beyond the responded data and causes the socket to hang.

So, how do I determine the end of a SMTP and POP response? Better still, is it even possible to know before hand how many bytes I should read from the buffer so that I won’t even have to check for end of response?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:24:31+00:00Added an answer on May 26, 2026 at 6:24 am

    The 2 protocols are different but both are sending messages that end with a newline. With SMTP, you read 1 line at a time (ending with \n), each line you read should start with 3 numbers, then either a space or a ‘-‘, if it’s a space, then that’s the end of the server’s response, if it’s a ‘-‘ (like 250-VRFY which is what you’d get if you opened up with an EHLO, prompting the server to respond with a list of extended ESMTP commands) then you need to read another line.

    With POP3, I believe it depends on the command that you issue. Something like USER or PASS gives you a ‘+’ or ‘-‘ response (followed by a code and some text). A command like LIST gives you a ‘+’ or ‘-‘, and if ‘+’, you get your list of emails, which will end with a single ‘.’ by itself. Each of these responses (as well as each index of an email) will be a line.

    You should take a look at RFC 821 for SMTP and RFC 1939 for POP3 to get a detailed description of the protocols

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading data from a file that has, unfortunately, two types of character
I have a TCP socket client receiving messages (data) from a server. messages are
My understanding from reading the specification ( http://www.ietf.org/rfc/rfc2445.txt ) is that the : character
I am reading unicode character from XML like \u09A8\u09AC\u09AE . I have used <?xml
I'm reading from a socket into a char array and I want to know
As far I know, the two most common methods of reading character-based data from
I'm trying to read the response from a server using a socket and the
Is there a way of reading one single character from the user input? For
When reading lines from a text file using python, the end-line character often needs
From reading the RFC it appears that CID can/must only contain characters from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.