我正在使用等待客户端连接的 Winsock 编写服务器程序。代码是这样的:
#include <winsock.h>
#pragma comment(lib, "wsock32.lib")
WSADATA wsaData;
WSAStartup(0x101, &wsaData);
SOCKET listeningSocket;
listeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(80);
bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
listen(listeningSocket, 5);
SOCKET theClient;
while(true)
{
theClient = accept(listeningSocket,NULL,NULL);
....
recv(theClient,Message,strlen(Message),0);
....
....
}
我遇到的问题是服务器不等待客户端并在接受()函数后继续执行。具体来说,accept() 函数不会阻塞执行。