getsockopt返回setsockopt中设置的不同IP_TOS值

我正在尝试使用setsockopt将IPTOS值设置为IPTOS_THROUGHPUT. setsockopt调用返回0.但是,getsockopt显示IP_TOS值设置为1,这与IPTOS_THROUGHPUT(0x8)不同.有谁知道什么可能导致setsockopt和getsockopt中的IPTOS值不匹配?

这是日志输出:

Set DSCP Marking on socket 26

setsockopt on socket 26 to 8 returns 0

DSCP marking on socket 26 is 1, different from expected 8

以下是代码:

int iptos = IPTOS_THROUGHPUT;

log(debug, 10, "Set DSCP Marking on socket %d\n", sockfd);

retval = setsockopt(sockfd, IPPROTO_TCP, IP_TOS,  &iptos, sizeof(iptos));
if (retval<0) {
   log(error, 99, "Failed to set DSCP marking on socket %d with error %d\n",
      sockfd, retval);
} else {
   log(debug, 10, "setsockopt on socket %d to %d returns %d\n", sockfd, iptos,
      retval);
   int tos=0;
   socklen_t toslen=sizeof(tos);

   retval = getsockopt(sockfd, IPPROTO_TCP, IP_TOS,  &tos, &toslen);
   if(retval<0) {
      log(warning, 99, "Failed to get DSCP marking on socket %d with error %d\n",
         sockfd, retval);
   }else {
      if( tos != iptos ) {
         log(warning, 99, "DSCP marking on socket %d is %d, different from expected %d\n",
            sockfd, tos, iptos);
         retval = 9999;
      }
      else {
         log(debug, 10, "Success: Set DSCP Marking on socket %d to %d\n",
            sockfd, iptos);
         retval = 0;
      }
   }
}

解决方法:

您应该使用级别选项IPPROTO_IP而不是IPPROTO_TCP

上一篇:TCP端口复用引发的异常,用setsockopt来解决


下一篇:记-python中socket服务器设置中的setsockopt