【发布时间】:2011-09-02 17:51:09
【问题描述】:
我的代码失败了。我以 root 身份运行(与普通用户相同的行为)
首先我想设置 TOS,然后获取值。
int tos_local = 0x28;
if (setsockopt(sockfd, IPPROTO_TCP, IP_TOS, &tos_local, sizeof(tos_local))) {
error("error at socket option");
} else {
int tos=0;
int toslen=0;
if (getsockopt(sockfd, IPPROTO_TCP, IP_TOS, &tos, &toslen) < 0) {
error("error to get option");
}else {
printf ("changing tos opt = %d\n",tos);
}
}
printf 打印出来
更改为 opt = 0
我希望打印 0x28 (40)。
有什么问题?
正确答案:
if (setsockopt(sockfd, **IPPROTO_IP**, IP_TOS, &tos_local, sizeof(tos_local))) {
int tos=0;
int toslen=sizeof(tos); //that line here
if (getsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, &toslen) < 0) {
【问题讨论】:
-
getsockopt 的第 5 个参数是第 4 个参数指向的缓冲区的大小。因此,您需要删除示例代码中的 & 。或者更好的是,把 sizeof(tos) 放在那里,就像 Seth 说的那样。
标签: c linux sockets network-programming