我能够从以下位置获得 mss 值getsockopt
:
tcpmss.c:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <stdio.h>
int main()
{
int sockfd, mss;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("sockfd");
return 1;
}
socklen_t len = sizeof(mss);
if (getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &len) < 0)
{
perror("getsockopt");
return 1;
}
printf("maximum segment size: %d\n", mss);
}
输出:
maximum segment size: 536
其他消息来源说,默认 mss 是1460
. 但是如果我尝试从客户端检查它:
客户端.c:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#define GET_CMD "GET %s HTTP/1.0\r\n\r\n"
#define SERV "80"
#define HOST "google.com"
#define HOMEPG "/"
//BUFSIZ = 8192, defined in <stdio.h>
int main()
{
int sockfd, nbytes;
struct addrinfo hints, *res;
char buf[BUFSIZ];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(HOST, SERV, &hints, &res) != 0)
{
perror("getaddrinfo");
return 1;
}
if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
{
perror("socket");
return 1;
}
if (connect(sockfd, res->ai_addr, res->ai_addrlen) < 0)
{
perror("connect");
return 1;
}
nbytes = snprintf(buf, 256, GET_CMD, HOMEPG);
if (write(sockfd, buf, nbytes) < 0)
{
perror("write");
return 1;
}
while ((nbytes = read(sockfd, buf, BUFSIZ)) > 0)
{
printf("read %d bytes of home page of %s\n", nbytes, HOST);
}
if (nbytes == 0)
{
printf("got EOF from google.com");
}
}
输出:
read 8192 bytes of home page of google.com
read 3888 bytes of home page of google.com
read 7248 bytes of home page of google.com
read 4832 bytes of home page of google.com
read 6040 bytes of home page of google.com
read 6040 bytes of home page of google.com
read 6040 bytes of home page of google.com
read 4832 bytes of home page of google.com
read 2229 bytes of home page of google.com
got EOF from google.com
这些值都不是真的。所以我对最大段大小有点困惑。我知道该read()
块并将更多段提取tcp
到内核接收缓冲区中,因此我无法从read()
系统调用中看到真实的段大小,但是,如何确定window
应对应于 MSS 的对等方之间的约定。在第一个read()
,我得到了完整的缓冲区,(BUFSIZE == 8192),然后甚至没有一半,等等。
如何确定(全部来自我的示例)
- MSS
- 对等点之间的传播窗口(及其与 MSS 的关系)
- 每个发送操作之间有多少段大小变化(以及为什么)