Making a Nonblocking socket for WinSocks and *nix

不羁的心 提交于 2019-12-07 03:50:42

问题


In C/C++, how would I turn a blocking socket into a non blocking socket in both WinSocks and *nix; so that select() would work correctly. You can use the pre-processor for the platform specific code.


回答1:


On linux:

fcntl(fd, F_SETFL, O_NONBLOCK);

Windows:

u_long on = 1;
ioctlsocket(fd, FIONBIO, &on);



回答2:


select() is supposed to work on blocking sockets. It returns when a read() would return immediately, which is always the case with non-blocking sockets.



来源:https://stackoverflow.com/questions/170909/making-a-nonblocking-socket-for-winsocks-and-nix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!