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

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)