定时器

来源:互联网 发布:java 代理模式详解 编辑:程序博客网 时间:2024/05/29 16:47

linux三种定时方式

1.socket选项SO_REVTIMEO,SO_SNDTIMEO

用在send、sendmsg、recv、recvmsg、accept、connect上:

a. send 、sendmsg 、connect 选项是SO_SNDTIMEO,超时返回-1,send、sendmsg的errnon设置为EAGAIN 或者 EWOULDBLOCK,connect的errno设置为EINPROGRESS

b. recv、recvmsg、accept 选项都是SO_REVTIMEO,超时返回-1,errno设置为EAGAIN 或者 EWOULDBLOCK


……

if ( setsockopt(sockfd,SOL_SOCKET,SO_SNDTIMEO,&timeout,sizeof(timeout))!=-1 )

{

     if( ( ret=connect(sockfd,(struct sockaddr*)&address,sizeof(address)) ) ==-1 )

     {

          if(errno==EINPROGRESS)

          {

                printf("time out\n");

                return -1;

           }

          else

                 ruturn -1

     }

     return sockfd;

}


2.SIGALRM信号

3.I/O复用系统调用(select epoll poll)超时参数

0 0
原创粉丝点击