accept函数为什么不阻塞
程序代码:
// sockSvr.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <WINSOCK2.H> #pragma comment(lib,"ws2_32.lib") //TCP服务器 void TCPServer() { SOCKET hSockSvr = socket(AF_INET,SOCK_STREAM,0); SOCKADDR_IN addrSvr = {0}; addrSvr.sin_family = AF_INET; addrSvr.sin_port = htons(5678); addrSvr.sin_addr.S_un.S_addr = INADDR_ANY; bind(hSockSvr,(SOCKADDR*)&addrSvr,sizeof(addrSvr)); listen(hSockSvr,5); SOCKADDR_IN addClient = {0}; int nLen = sizeof(addClient); SOCKET hSockClient = accept(hSockSvr, (SOCKADDR*)&addClient,&nLen); //数据收发 char szRecv[100] = {0}; recv(hSockClient,szRecv,100,0); printf("Client Data:%s\n",szRecv); //关闭套接字 closesocket(hSockClient); closesocket(hSockSvr); } int main(int argc, char* argv[]) { //初始化socket库 WSADATA ws = {0}; WSAStartup(MAKEWORD(2,2),&ws); TCPServer(); return 0; }