c++ 中套接字连接的缓冲区大小

发布于 2024-08-31 15:57:52 字数 179 浏览 5 评论 0原文

我正在尝试用 C/++ 构建一个基本的 POP3 邮件客户端,但遇到了一些问题。由于在构建程序时必须定义缓冲区大小,但消息可以任意大,那么如何让邮件服务器将其分批发送给您呢?如果这不是解决问题的正确方法,那什么才是呢?

当我在这里时,有人可以帮我确认 RFC 2822 仍然是定义电子邮件布局的当前文档吗?

谢谢

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 arbitrarily 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-09-07 15:57:52

由于大多数电子邮件都是使用 TCP/IP 完成的,因此如果您确实愿意,可以一次读取一个字节。底层实现将为您缓冲流。在脱离网络的情况下,它一次会收到大约 1,400 个字节。一般来说,我使用 std::vectorstd::string 作为缓冲区,一次读取一个字节,然后 push_backselect() 循环中以较短的超时时间访问缓冲区。

我不记得 POP 是否包含最大行长度。如果是,那么您可以使用它作为缓冲区大小并在向量上调用 reserve() 。这将最大限度地减少可能发生的内存重新分配和复制。

至于最新的标准,https://www.rfc-editor.org/rfc/ rfc2822 说它已被 https://www.rfc-editor.org 废弃/rfc/rfc5322。我通常检查 https://www.rfc-editor.org/rfc/rfcXXXX,其中 XXXX 是 RFC 编号。如果它已过时,则顶部有一个指向最合适的 RFC 的链接。

最后提一下,如果没有充分的理由,也不要构建用于部署的 POP 客户端。各种 RFC 中隐藏着许多陷阱。不过,这确实是一次很好的学习经历。

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.

南笙 2024-09-07 15:57:52

如果您正在从套接字读取,您可以指定您想要读取的字节数 读取< /a>.此外,您还可以使用 new 在运行时动态分配缓冲区。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文