> 文章列表 > C语言网络属性设置、超时检测

C语言网络属性设置、超时检测

C语言网络属性设置、超时检测

1、gethostbyname()将www.baidu.com域名解析成ip地址

struct hostent *gethostbyname(const char *name);
/*
ret:The gethostbyname()functions  return  the  hostentstructure  or a NULL pointer if an         error occurs.  On error, the h_errno variable holds an error number. struct hostent {char  *h_name;            /* official name of host */char h_aliases;         /* alias list */int    h_addrtype;        /* host address type */int    h_length;          /* length of address */char h_addr_list;       /* list of addresses */}#define h_addr h_addr_list[0] /* for backward compatibility */
*/

        enhostent()

void endhostent(void);
/*The endhostent() function ends the use of a  TCP  connection  for  nameserver queries.
*/

2、

int getsockopt(int sockfd,int level,int optname,void *optval,socklen_t *optlen)
int setsockopt(int sockfd,int level,int optname,const void *optval,socklen_t *optlen)
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
========================================================== 

3、网络超时背景:

          在网络通信中,很多操作会使得进程阻塞。

           TCP套接字中的recv/accept/connect

            UDP套接字中的recvfrom

           超时检测必要性:

                避免进程在没有数据时,无限阻塞

                当设定的时间到时,进程从原操作返回继续运行。

4、网络超时优化:

        第一种方式:设置socket属性SO_RCVTIMEO

        参考代码:

struct timeval  tv;tv.tv_sec = 5;   //  设置5秒时间
tv.tv_usec = 0;setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO,  &tv,  sizeof(tv));   //  设置接收超时recv() / recvfrom()    //   从socket读取数据 

  第二种方式:用select()函数检测数据是否准备好了

    参考代码:

    struct fd_set rdfs;while(1){struct timeval  tv = {5 , 0};   // 设置5秒时间FD_ZERO(&rdfs);FD_SET(sockfd, &rdfs);if (select(sockfd+1, &rdfs, NULL, NULL, &tv) > 0)   // socket就绪{recv() /  recvfrom()    //  从socket读取数据}}

第三种方式:信号处理方式,设置定时器timer,捕捉SIGALRM信号

  参考代码如下:

void  handler(int signo)     {   return;  }struct sigaction  act;sigaction(SIGALRM, NULL, &act);act.sa_handler = handler;
//清理掉SIGALRM的sa_restart快速重启信号。系统设置过sa_restart        会直接recv,所以去掉这个标识。act.sa_flags &= ~SA_RESTART; sigaction(SIGALRM, &act, NULL);alarm(5); //设置5s钟超时,如果5s钟还没数据来的时候,发送alarm信号,打算recvif (recv(,,,) < 0) ……

思考:

        在linux中动态检查到是否有网络以及网络中的掉线/连接的检查?

                1、提示:

                        应用层:心跳检测

        (1)方法一

                数据交互双方间隔一段时间,一方发送一点数据给对方,对方做出特定的应答,如果超过设定次数大小的时间内还是没有应答,这时候认为异常       

       (2)方法二 

                改变套接字的属性             

函数定义:
void setKeepAlive(int sockfd, int attr_on, socklen_t idle_time, socklen_t interval, socklen_t cnt){setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (const char*)&attr_on,sizeof(attr_on));setsockopt(sockfd, SOL_TCP, SO_KEEPIDLE, (const char*)&attr_time, sizeof(attr_time));setsockopt(sockfd, SOL_TCP, SO_KEEPINTVL, (const char*)&interval, sizeof(interval));setsockopt(sockfd, SOL_TCP, SO_KEEPCNT, (const char*)&cnt, sizeof(cnt));}int keepAlive = 1; //设定keepAlive
int keepIdle = 5;  //开始首次KeepAlive探测前Tcp的空闲时间
int keepInterval = 5; //两次keepalive探测时间间隔
int keepConut = 3;   //判断断开前keepAlive探测次数setKeepAlive(newfd, keepAlive, keepIdle, keepIntervale, keepCount);

                2、内核中

                        网卡驱动中2.6内核里面,使能1s的周期性检查定时器。

                        网卡硬件或者我们通过GPIO,插拔网线时产生中断;处理应用中断//立即检测