动态多线程服务器问题
while(true)
{
Klient temp;
SOCKET client = accept(sock,(struct sockaddr *)&ich,&sin_size);
if (client!= INVALID_SOCKET)
{
int ID = wszyscy.size(); //wszyscy is a vector of Klient structure; ID increments like: 0,1,2,3 etc
memset(&temp,0,sizeof(Klient)); //zero structure
temp.Gniazdo = client; //fill temporary structure
temp.Identyfikator = ID;
temp.Nick = "";
temp.Watek = NULL;
wszyscy.push_back(temp);//Adding
std::cout << ID << std::endl; //Showing ID for test
std::cout << inet_ntoa(ich.sin_addr) << std::endl;//IP
wszyscy[ID].Watek = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Watek,&wszyscy[ID], 0 ,NULL);//Creating thread
}
}
主题:
DWORD Watek(LPVOID Dane)
{
const char* tekst_serwera = "Halo, tu server :D\n\0";
Klient *temp = (Klient*) Dane;
send(temp->Gniazdo,tekst_serwera,strlen(tekst_serwera),0);
std::cout << "Podlaczyl sie klient o ID: " << temp->Identyfikator << std::endl;
char bufor[500];
while(true)
{
memset(bufor,0,500);
recv(temp->Gniazdo,bufor,500,0);
std::cout << bufor;
}
}
问题是当我启动 2 个或更多客户端时,服务器会向它们发送欢迎消息(tekst_servera),但是当我在已作为第二个启动的客户端上写一些内容,然后我尝试在已启动的客户端上写一些内容时已启动,因为第一台服务器的 CPU 使用率为 80%,并且不接收数据,只有第一个服务器(仅发生在作为第一个服务器启动的客户端上)当我执行 temp.watek = CreateThread 然后 Push_back 服务器仍然没有响应致 1 号客户消息,但并没有像地狱一样滞后。
任何帮助表示赞赏
while(true)
{
Klient temp;
SOCKET client = accept(sock,(struct sockaddr *)&ich,&sin_size);
if (client!= INVALID_SOCKET)
{
int ID = wszyscy.size(); //wszyscy is a vector of Klient structure; ID increments like: 0,1,2,3 etc
memset(&temp,0,sizeof(Klient)); //zero structure
temp.Gniazdo = client; //fill temporary structure
temp.Identyfikator = ID;
temp.Nick = "";
temp.Watek = NULL;
wszyscy.push_back(temp);//Adding
std::cout << ID << std::endl; //Showing ID for test
std::cout << inet_ntoa(ich.sin_addr) << std::endl;//IP
wszyscy[ID].Watek = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Watek,&wszyscy[ID], 0 ,NULL);//Creating thread
}
}
Thread:
DWORD Watek(LPVOID Dane)
{
const char* tekst_serwera = "Halo, tu server :D\n\0";
Klient *temp = (Klient*) Dane;
send(temp->Gniazdo,tekst_serwera,strlen(tekst_serwera),0);
std::cout << "Podlaczyl sie klient o ID: " << temp->Identyfikator << std::endl;
char bufor[500];
while(true)
{
memset(bufor,0,500);
recv(temp->Gniazdo,bufor,500,0);
std::cout << bufor;
}
}
The problem is when I launch 2 or more clients, server send to them as it suppose to welcome messages(tekst_servera) but when I write something on client who has been launched as second and then I try to write something on client who has been launched as first server is getting 80%CPU usage and does not receive data, only the first one(It happens only on client who has been launched as first) When I did temp.watek = CreateThread and then push_back server was still not responding to client number 1 messages but was not lagging like hell.
Any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 winsock 文档 (msdn. microsoft.com),recv 函数将返回以下值之一:
我强烈建议检查
recv()
的返回值,并在必要时终止循环,因为在零和负数情况下,您都会陷入循环,调用返回的 recv()而不立即阻塞。这将显示为高 CPU 使用率和无响应的服务器线程。无论如何,丢弃您调用的函数的返回值都是不好的形式,并且只应该在特殊情况下这样做。
According to winsock documentation (msdn.microsoft.com), the recv function will return one of the following values:
I strongly suggest checking the return value of
recv()
, and terminating the loop when necessary, because in both the zero and negative cases, you'll be stuck in a loop, calling recv() which returns without blocking immediately. This would show up as high CPU usage and non-responsive server threads.In any case, throwing away the return value from a function you call is bad form, and should only be done in exceptional situations.