您的位置:首页 > 其它

iphone设置非阻塞模式socket两种方法

2010-01-25 13:06 609 查看
1. ioctl()

/* set socket to non-blocking i/o */
sts = ioctl(ccp->main_sock, FIONBIO, (char *)&one);
if (sts)
{
setproderr(PE_TCPERROR, GEL_FATAL);
sprintf(line,"ioctl (main) failed - %s",strerror(errno));
tcpabort();
}
 

 

2.fcntl()

/* Set socket to non-blocking */
if ((flags = fcntl(sock_descriptor, F_GETFL, 0)) < 0)
{
/* Handle error */
}

if (fcntl(socket_descriptor, F_SETFL, flags | O_NONBLOCK) < 0)
{
/* Handle error */
}
/* Set socket to blocking */
if ((flags = fcntl(sock_descriptor, F_GETFL, 0)) < 0)
{
/* Handle error */
}

if (fcntl(socket_descriptor, F_SETFL, flags & (~O_NONBLOCK)) < 0)
{
/* Handle error */
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  socket iphone descriptor