1. windows平台上无论利用socket()函数还是WSASocket()函数创建的socket都是阻塞模式的:
SOCKET WSAAPI socket(
_In_ int af,
_In_ int type,
_In_ int protocol
);
SOCKET WSASocket(
_In_ int af,
_In_ int type,
_In_ int protocol,
_In_ LPWSAPROTOCOL_INFO lpProtocolInfo,
_In_ GROUP g,
_In_ DWORD dwFlags
);
linux平台上可以在利用socket()函数创建socket时指定创建的socket是异步的:
int socket(int domain, int type, int protocol);
在type的参数中设置SOCK_NONBLOCK标志即可,例如:
int s = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
2. 另外,windows和linux平台上accept()函数返回的socekt也是阻塞的,linux另外提供了一个accept4()函数,可以直接将返回的socket设置为非阻塞模式:
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int fla