动态多线程服务器问题

发布于 2024-10-24 19:48:23 字数 1892 浏览 1 评论 0原文

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2024-10-31 19:48:23

根据 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:

  • positive: The number of bytes received;
  • zero: connection was closed;
  • negative: an error has occurred.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文