网路编程时有时需要给建立连接过程添加时间控制,通常的过程是
1)建立套接字,调用fcntl设置非阻塞(O_NONBLOCK)选项
2)调用connect函数发起连接,判断errno是否是EINPROGRESS(也有可能直接建立连接成功),如果不是,说明出错
3)调用select函数监听套接字是否可写来判断连接是否建立
由于在第一步中,将套接字设置为非阻塞状态,可能有些人会疑惑,是否需要在select之前将套接字设置为阻塞状态?答案是不需要!因为select函数它只会检查监听套接字或文件描述符的可读、可写或异常的状态,套接字是否阻塞不会影响select函数的功能(现在还没看过select函数的源码,个人认为select应该不会检查是否阻塞的选项)。
为了验证上面的结论,写了下面一个测试程序,仅供参考:
#include <stdio.h>
#include <fcntl.h>
#include <sys/select.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
int main(void)
{
int