如何在 FreeBSD 中使用 TCP_NOPUSH 调用 sendfile 之前添加 header

How to prepend header before calling sendfile using TCP_NOPUSH in FreeBSD

我在技术上知道 FreeBSD 中的 TCP_NOPUSH 和 Linux 中的 TCP_CORK 应该是相似的。

在Linux中,我可以设置TCP_CORK,发送一个header,sendfile()并取消设置TCP_CORK以在前面插入header文件。在 FreeBSD 中,我尝试用 TCP_NOPUSH 做同样的事情,但似乎 header 与文件内容分开了。一定是我太笨了,没搞错。

这是我的代码(Linux 和 BSD):

[共享]:

int yes = 1;
int no = 0;
char buf[30] = "Hello World!\n";

[Linux]

setsockopt(sockfd, IPPROTO_TCP, TCP_CORK, &yes, sizeof(int)); 
write(sockfd, buf, 30);
sendfile(sockfd, filefd, NULL, SIZE_OF_FILE);
setsockopt(sockfd, IPPROTO_TCP, TCP_CORK, &no, sizeof(int));

结果: 世界您好!\n 文字....文字.....

但是在 FreeBSD 中:

setsockopt(sockfd, IPPROTO_TCP, TCP_NOPUSH, &yes, sizeof(int));
write(sockfd, buf, 30);
sendfile(filefd, sockfd, 0, SIZE_OF_FILE, NULL, NULL, 0);
setsockopt(sockfd, IPPROTO_TCP, TCP_NOPUSH, &new, sizeof(int));

结果: 世界您好!\n

我应该怎么做才能获得与 Linux 相同的行为???

你不需要这样做;与 Linux 不同,FreeBSD sendfile(2) 支持发送 headers.