套接字选项—修改套接字缓冲区大小

有时候我们需要控制套接字的行为(如修改缓冲区的大小),这个时候我们就要学习套接字选项。

 

 

level指定控制套接字的层次.可以取三种值:

1)SOL_SOCKET:通用套接字选项. 

2)IPPROTO_IP:IP选项. 

3)IPPROTO_TCP:TCP选项. 


optname指定控制的方式(选项的名称),我们下面详细解释 

optval获得或者是设置套接字选项.根据选项名称的数据类型进行转换
 


选项名称                说明                                    数据类型
========================================================================
                        SOL_SOCKET
------------------------------------------------------------------------
SO_BROADCAST        允许发送广播数据                    int
SO_DEBUG                允许调试                                int
SO_DONTROUTE        不查找路由                             int
SO_ERROR                 获得套接字错误                       int
SO_KEEPALIVE          保持连接                                 int
SO_LINGER               延迟关闭连接                           struct linger
SO_OOBINLINE         带外数据放入正常数据流            int
SO_RCVBUF               接收缓冲区大小                        int
SO_SNDBUF               发送缓冲区大小                        int
SO_RCVLOWAT          接收缓冲区下限                        int
SO_SNDLOWAT          发送缓冲区下限                        int
SO_RCVTIMEO           接收超时                                 struct timeval
SO_SNDTIMEO           发送超时                                 struct timeval
SO_REUSERADDR       允许重用本地地址和端口            int
SO_TYPE                    获得套接字类型                         int
SO_BSDCOMPAT        与BSD系统兼容                         int
==========================================================================
                        IPPROTO_IP
--------------------------------------------------------------------------
IP_HDRINCL              在数据包中包含IP首部                    int
IP_OPTINOS              IP首部选项                                   int
IP_TOS                      服务类型
IP_TTL                       生存时间                                      int
==========================================================================
                        IPPRO_TCP
--------------------------------------------------------------------------
TCP_MAXSEG              TCP最大数据段的大小                     int
TCP_NODELAY            不使用Nagle算法                           int
=========================================================================

 

大致介绍这么多,还是看源码吧:

 

 

 

程序运行效果:

套接字选项

 

 

源码下载地址:

 

http://download.csdn.net/source/1676947

没有更多推荐了,返回首页