DWORD WINAPI createClientThread(void* lpParam)
{
Server* server = (Server*)lpParam;
while (TRUE)
{
server->resultsValue = send(server->socketclient, "欢迎登录服务器", strlen("欢迎登录服务器") + 1, 0);
if (SOCKET_ERROR == server->resultsValue)
{
server->resultsValue = WSAGetLastError();
if (WSAEWOULDBLOCK == server->resultsValue)
continue;
else if (WSAENETDOWN == server->resultsValue || WSAETIMEDOUT == server->resultsValue || WSAECONNRESET == server->resultsValue)
{
char bufIp[IP_BUF_SIZE];
inet_ntop(server->addressClient.sin_family, &server->addressClient, bufIp, IP_BUF_SIZE);
cout << bufIp << ": " << ntohs(server->addressClient.sin_port) << " the client closes the connection..." << endl;
closesocket(server->socketclient);
}
}
Sleep(10000);
}
return 0;
}
BOOL Server::createAcceptThread()
{
while (TRUE)
{
dataLength = sizeof(SOCKADDR_IN);
socketclient = accept(socketServer, (SOCKADDR*)&addressClient, &dataLength);
if (INVALID_SOCKET == socketclient)
return FALSE;
char bufIp[IP_BUF_SIZE];
inet_ntop(addressClient.sin_family, &addressClient, bufIp, IP_BUF_SIZE);
cout << "A new client connected...IP address: " << bufIp << ", port number: " << ntohs(addressClient.sin_port) << endl;
unsigned long ul;
handleThread = CreateThread(nullptr, 0, createClientThread, (void*)this, 0, &ul);
}
return TRUE;
}
有多个客户都的话只能处理一个客户端,在代码中是每一个连接成功的客户端都创建一个线程处理不同客户端的,为什么只有一个客户端可以处理呢