在socket编程中需要设置套接字为非阻塞时,可以使用fcntl函数设置。
函数原型:
#incude <fcntl.h>
int fcntl(int fd, int cmd, ... /*int arg*/);
实现代码如下:
int flags;
if( (flags = fcntl(fd, F_GETFL, 0)) < 0 )
{
perror(F_GETFL error);
exit(1);
}
flags |= O_NONBLOCK;
if( fcntl(fd, F_SETFL, flags) < 0 )
{
perror(F_SETFL error);
exit(1);
}