转自: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. Mysql学习记录点

    order by 数字,表示按照第几列来排序,可以从1开始,不能是0,也不能超过列数.

  2. spring-quartz定时任务使用小结

    在实际项目中,通常须要用到定时任务(定时作业).spring框架提供了非常好的实现. 1.  下载spring-quartz插件包 这里默认当前系统中是集成了spring框架的基本功能的.去网上下载s ...

  3. [React] Persist Form Data in React and Formik with formik-persist

    It can be incredibly frustrating to spend a few minutes filling out a form only to accidentally lose ...

  4. 【分享】利用Apache的Htaccess Files命令限制訪问文件类型,Files正则

    假设你在你的模板目录中有非常多PSD HTML模板,那么用接下来这个htaccess文件能够保护限制訪问: 文件D:\WebSite\ZBPHP.COM\www\Tpl\.htaccess 所有源代码 ...

  5. C++中的链式操作

    代码编译环境:Windows7 32bits+VS2012. 1.什么是链式操作 链式操作是利用运算符进行的连续运算(操作).它的特点是在一条语句中出现两个或者两个以上相同的操作符,如连续的赋值操作. ...

  6. Mesa (computer graphics)

    http://en.wikipedia.org/wiki/Mesa_(computer_graphics) Mesa (computer graphics) From Wikipedia, the f ...

  7. ExtJs学习笔记(1)---ExtJs安装及其使用

    从官网下载了ExtJs的3.2版本号的SDK,包括了代码依赖的具体说明.文档.范例和其它文件.当中,adapter和resources文件是Ext正常执行所必须的,其它的仅在开发过程中使用到. Ada ...

  8. Android Baseline小tip

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

  9. 【bzoj2761】【JLOI2011】【不反复数字】【平衡树】

    Description 给出N个数,要求把当中反复的去掉.仅仅保留第一次出现的数. 比如,给出的数为1 2 18 3 3 19 2 3 6 5 4.当中2和3有反复.去除后的结果为1 2 18 3 1 ...

  10. Django之cookie 和 session

    一. 1.cookie的由来!!! 由于HTTP协议是无状态的,既每一次的请求都是独立的,他不会因为你之前来过,就记住你,所以每次浏览器去访问服务器的时候,都是一个全新的过程,之前的数据也不会保留,所 ...