转自:http://zhangjunxin520.blog.163.com/blog/static/305037032011721102857609/

在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.

设置socket IP_TOS选项 (转载)的更多相关文章

  1. java Socket(详解)转载

    在客户/服务器通信模式中, 客户端需要主动创建与服务器连接的 Socket(套接字), 服务器端收到了客户端的连接请求, 也会创建与客户连接的 Socket. Socket可看做是通信连接两端的收发器 ...

  2. ZMQ设置socket选项

    Name zmq_setsockopt –设置ZMQ socket的属性 Synopsis int zmq_setsockopt (void *socket, int option_name, con ...

  3. setsockopt 设置socket 详细用法(转载)

    转自:http://www.cppblog.com/killsound/archive/2009/01/16/72138.html 1.closesocket(一般不会立即关闭而经历TIME_WAIT ...

  4. 转:Socket常用选项

    功能描述 获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套接字选项时,选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 将层的值指 ...

  5. IP_TOS选项

    voip IP_tos 选项 在IP头中,有一Type-of-Service字段,该字段描述了IP包的优先级和QoS选项,使用IP_TOS可以来设定该字段的值,以区分不同服务的优先级,Linux 中可 ...

  6. Socket/ServerSocket 选项

    在网络编程中,Socket/ServerSocket有一些选项用来自定义一些行为,现在分享一下.     Socket选项 1.TCP_NODELAY 在Socket发送数据时,默认情况下,数据会先进 ...

  7. setsockopt设置socket状态

    setsockopt设置socket状态 1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket:BOOL bReuseaddr=TRUE;setsoc ...

  8. setsockopt 设置TCP的选项SO_LINGER

    SO_LINGER选项用来设置延迟关闭的时间,等待套接字发送缓冲区中的数据发送完成. 没有设置该选项时,在调用close()后,在发送完FIN后会立即进行一些清理工作并返回.如果设置了SO_LINGE ...

  9. setsockopt 设置socket 详细用法

    1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket:BOOL bReuseaddr=TRUE;setsockopt(s,SOL_SOCKET ,SO ...

随机推荐

  1. 【effective c++】定制new和delete

    条款49: 了解new-handler的行为 operator new 和 operator delete只适合用来分配单一对象.array所用的内存由operator new[]分配出来,并由ope ...

  2. nginx配置初步

    nginx配置初步 1,切换至nginx目录,找到配置文件目录 cd /etc/nginx/conf.d 2,拷贝一份conf文件 sudo cp default.conf head.conf 3,进 ...

  3. Solidworks如何绘制文字

    1 新建草图,并点击工具-草图绘制实体,文本   2 在弹出的窗口中输入文字,并设置文字的字体和样式   3 画好之后效果如下图所示   4 使用拉伸或者拉升切除来得到凸面的文字或者凹面的文字.   ...

  4. 说说怎样管理软件日常执行的server

    大家应该都有这种情况.就是软件开发时都是全力以赴的把软件开发出来.一旦软件上线执行起来我们就能松口气.从而放松了对server及数据的管理.往往这个时候.server的一个小故障都能让我们忙上好一阵. ...

  5. hud 1465、2049、2045 (递推)[含简单C(n,m) n!的写法]

    C - 不容易系列之一 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit  ...

  6. openwrt 模拟i2c驱动(一)

    一:加载i2c driver kmod-i2c-core................................................ I2C support kmod-i2c-al ...

  7. Android Baseline小tip

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40709353 Baseline Alignment

  8. HDU 6044 Limited Permutation 读入挂+组合数学

    Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...

  9. 如何解决Windows的端口占用问题?

    已知某应用在启动时会创建服务套接字,并将其绑定至端口8888,然而端口8888已被占用,如何解除占用? 以下为解决方案: 在cmd中运行netstat -ano|findstr 8888,其中的参数8 ...

  10. ASHX入门教程

    新建web应用程序 其中添加的ashx包含ashx.CS 普通的web网站只包含ashx 新建webapplication应用 新建SampleHandler public class SampleH ...