您的位置:首页 > 运维架构 > Linux

code to solve "SOCK_NONBLOCK" and "O_NONBLOCK" undefined problem in linux

2011-01-18 05:09 811 查看
#if defined(SOCK_NONBLOCK)

return ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);

/* If they have O_NONBLOCK, use the Posix way to do it */

#elif defined(O_NONBLOCK)

/* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */

int sock = socket(PF_INET, SOCK_STREAM, 0);

int flags;

if (-1 == (flags = fcntl(sock, F_GETFL, 0)))

flags = 0;

fcntl(sock, F_SETFL, flags | O_NONBLOCK);

return sock;

#else

int sock = socket(PF_INET, SOCK_STREAM, 0);

/* Otherwise, use the old way of doing it */

int flags = 1;

ioctl(sock, FIOBIO, &flags);

return sock;

#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐