Hello,

I'm trying to write a small POP3 mail client, but although the basic mechanism works message retrieval is haphazard at best. It usually gets through about 50 or so messages before hitting one that it can't fully read through. Here's a generalised version of the algorithm I'm using, which scans through each line:-
Code:
// First packet that's received for a message, reset the counter
g_ulMsgRead = 0;

szLine = g_szBuffer;
while (szLine)
{
    // Find the end of the current line
    szLineEnd = strstr(szLine, "\r\n");
    if (szLineEnd)
    {
        szLineEnd += 2;
        // Add line end - line start to the counter
        g_ulMsgRead += (szLineEnd - szLine);
    }
    else // Add the end of the buffer - line start to counter
        g_ulMsgRead += ((g_szBuffer + nBytesRead) - szLine);

    // If we've read the length of the message, finish
    if (g_ulMsgRead >= g_ulSize)
    {
        bFinished = 1;
        break;
    }

    szLine = szLineEnd;
}
Either packet splitting is causing this to go wrong somewhere or I need a better reference than the oddly-light RFC 1939.