我编程客户端服务器应用程序。 简单地说,服务器可以使用UDP或TCP为每个客户端。
我想无论是TCP客户端和UDP客户端打印默认值SO_SNDBUF
和SO_RCVBUF
,但我很惊讶,当我连接建立的值是0之前打印的sndbuf和rcvbuf默认值。
注 :我使用Ubuntu 12.10
这里是我的代码:
//assume all variables initialized correctly.
/* SO_RCVBUF options */
len = sizeof(rcvbuf);
getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len);
len = sizeof(mss);
getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &len);
printf("defaults: SO_RCVBUF = %d, MSS = %d\n", rcvbuf, mss);
rcvbuf = 9973; /* a prime number */
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
len = sizeof(rcvbuf);
getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len);
printf("SO_RCVBUF = %d (after setting it to 9973)\n\n\n\n", rcvbuf);
/* SO_RCVBUF options */
/* SO_SNDBUF options */
len2 = sizeof(sndbuf);
getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &len2);
//len2 = sizeof(mss2);
getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &mss2, &len2);
printf("defaults: SO_SNDBUF = %d, MSS = %d\n", sndbuf, mss2);
sndbuf = 9979; /* a prime number */
setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
len2 = sizeof(sndbuf);
getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &len2);
printf("SO_SNDBUF = %d (after setting it to 9979)\n", sndbuf);
/* SO_SNDBUF options */
输出示例:
默认值:SO_RCVBUF = 0,MSS = 0
SO_RCVBUF = 9973(其设置为9973之后)
默认值:SO_SNDBUF = 0,MSS = 0
SO_SNDBUF = 9979(其设置为9979之后)
tcp_time nes411_server
当前时间是(TCP):周一04月22日二十三时40分46秒2013
连接后:
SO_SNDBUF = 170840,MSS = 16384