开发者

buffer size for socket connection in c++

I'm trying to build a basic POP3 mail client in C/++, but I've run into a bit of an issue. Since you have to define the buffer size when building the program, but a message can be arbitraril开发者_开发问答y large, how do you, say, get the mail server to send it to you in parts? And if this isn't the correct means of solving the problem, what is?

And while I'm here, can anyone confirm for me that RFC 2822 is still the current document defining email layout?

Thanks


Since most email is done using TCP/IP, you can read one byte at a time if you really want to. The underlying implementation will buffer the stream for you. It is received approximately 1,400 bytes at a time off of the network. Generally, I using either std::vector<char> or std::string as a buffer and read one byte at a time and push_back on to the buffer in a select() loop with a short timeout.

I can't remember if POP includes a maximum line length or not. If it does, then you can use that as your buffer size and call reserve() on the vector. That will minimize memory reallocations and copies that might otherwise occur.

As for which standard is most recent, https://www.rfc-editor.org/rfc/rfc2822 says that it was obsoleted by https://www.rfc-editor.org/rfc/rfc5322. I usually check https://www.rfc-editor.org/rfc/rfcXXXX where XXXX is the RFC number. If it is obsolete, then there is a link to the most appropriate RFC at the top.

And as a final mention, don't build a POP client for deployment without a good reason too. There are a lot of gotcha's buried in the various RFCs. It is a really good learning experience though.


If you are reading from a socket, you can specify the number of bytes you wish to read. Also, you can allocate a buffer dynamically at run-time using new.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜