博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Typcical code to enable nonblocking I/O

Posted on 2011-04-20 10:48  天地玄黄  阅读(313)  评论(0编辑  收藏  举报

The following is the code coming from Uinx Network Programming on page 235:

int flags;

        /* set a socket as nonblocking */
if( (flags = fcntl(fd, F_GETFL, 0)) < 0)
        err_sys("F_GETFL error");
flags |= O_NONBLOCK;        /* This is very inportant or you will clear all other status flags about this file */
if(fcntl(fd, F_SETFL, flags) < 0)
        err_sys("F_SETFL error");

turn off the nonblocking:

flags &= ~O_NONBLOCK;
if(fcntl(fd, F_SETFL, flags) < 0)
        err_sys("F_SETFL error");