注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

守望

路越远,心越近。有多远,走多远!

 
 
 

日志

 
 

设置socket IP_TOS选项  

2011-08-21 10:47:06|  分类: 编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
在IP头中,有一Type-of-Service字段,该字段描述了IP包的
优先级和QoS选项,使用IP_TOS可以来设定该字段的值:

使用setsockopt设置IP_TOS代码如下:
unsigned char  service_type = 0xe0 | IPTOS_LOWDELAY | IPTOS_RELIABILITY;
if(setsockopt(sock, SOL_IP/*IPPROTO_IP*/, IP_TOS, (void *)&service_type, sizeof(service_type)) < 0)
        perror("setsockopt(IP_TOS) failed:");

使用getsockopt读取IP_TOS代码如下:
int optval = 0;
int optlen = sizeof(optval);
if(getsockopt(sock, SOL_IP, IP_TOS, (void *)&optval, &optlen) < 0)
        perror("getsockopt(IP_TOS) failed:");
else
        printf("OPTVAL=%x.\n", optval)

在socket设置IP_TOS后,用wireshark抓取该socket上发送的数据,查看IP头部可以看到设置的值。

附man手册中IP_TOS、SO_PRIORITY说明:
IP_TOS:  SOL_IP / IPPROTO_IP(BSD);  IP_TOS Set  or  receive  the  Type-Of-Service (TOS)  field 
                that is sent with every IP packet originating from this socket.  It is used to prioritize packets 
                on the network.  TOS is a byte. There are some standard TOS flags defined: 
                IPTOS_LOWDELAY to minimize delays for interactive traffic, IPTOS_THROUGHPUT to 
                optimize throughput, IPTOS_RELIABILITY to optimize for reliability, IPTOS_MINCOST 
                should be used for "filler data"  where  slow transmission doesn't matter.  At most one of these 
                TOS values can be specified.  Other bits are invalid and shall be cleared.  Linux sends 
                IPTOS_LOWDELAY datagrams first by default, but the exact behaviour depends on the 
                configured queueing discipline.  Some high priority levels may require superuser 
                privileges (the CAP_NET_ADMIN capability).  The priority can also be set in a protocol 
                independent way by the (SOL_SOCKET,  SO_PRIORITY) socket option (see socket(7)).
 
SO_PRIORITY:  SOL_SOCKET;  SO_PRIORITY Set the protocol-defined priority for all packets to be
                sent on this socket.  Linux uses this value to order the networking queues: packets with a 
                higher priority may be processed  first  depending on  the  selected device queueing 
                discipline. For ip(7), this also sets the IP type-of-service (TOS) field for outgoing packets.  
                Setting a priority  outside the range 0 to 6 requires the CAP_NET_ADMIN capability.
  评论这张
 
阅读(9354)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017