【问题标题】:How can I send socket id through pthread?如何通过 pthread 发送套接字 id?
【发布时间】:2019-07-05 10:59:41
【问题描述】:

我在树莓派中有一个服务器,并希望允许多线程。服务器正在工作。客户端在 Windows 中。 我相信我需要通过 pthread_create 发送套接字 id,但还没有找到如何。还有什么我需要寄的吗? 最好的方法是什么?

我已经搜索了互联网,包括 stackoverflow,并尝试了一些解决方案,但它们不起作用。

const int PORT = 12000;
TCPServer tcp;
pthread_t my_thread[MAXCLIENTQUEUE];
int clientID = 0;

int main()
{

    tcp.setup(PORT);    

    int clientQueueSize = 0, threadJoin = 0;
    void *res;

    do {
        socklen_t sosize = sizeof(tcp.clientAddress);
        //realizar o accept
        tcp.newsockfd[clientQueueSize] = accept(tcp.sockfd, (struct sockaddr*) & tcp.clientAddress, &sosize);
        if (tcp.newsockfd[clientQueueSize] == -1)
        {
            cout << "Error accepting -> " << tcp.newsockfd[clientQueueSize] << endl;
            tcp.detach();
        }
        cout << ">- accept: " << strerror(errno) << " / codigo: " << tcp.newsockfd[clientQueueSize] << " - Endereco: " << inet_ntoa(tcp.clientAddress.sin_addr) << endl;
        clientID++;
        cout << ">>> client accepted" << " | Client ID: " << clientID << endl;
        // criar threads
        int ret = pthread_create(&my_thread[clientQueueSize], NULL, messenger, &tcp.newsockfd[clientQueueSize]);
        cout << ">- pthread: " << strerror(errno) << " / codigo: " << ret << endl;
        if (ret != 0) {
            cout << "Error: pthread_create() failed\n" << "thread_n " << my_thread[clientQueueSize] << endl;
            exit(EXIT_FAILURE);
        }
        cout << "thread n " << my_thread[clientQueueSize] << endl;
        clientQueueSize++;
        }
    while (clientQueueSize < MAXCLIENTQUEUE);

    pthread_exit(NULL);

    return 0;
}

服务器接受多个连接,但只向第一个客户端发送消息,其他客户端连接成功,但从不接收消息。 我希望服务器能够向所有客户端发送消息。

【问题讨论】:

    标签: c++ server pthreads


    【解决方案1】:

    您必须为所有套接字创建线程。 或者,使用依赖于 Windows 的异步选择方法。

    附:忘记 pthreads 并使用标准的std::thread

       map<SOCKET,std::string> clients;
       void newclient(SOCKET x)
       {
           for(;;)
           {
           int r = recv(x,...);
           if (r == 0 || r == -1) 
               break;
           }
         // remove it from clients, ensure proper synchronization
    
       }
    
       void server()
       {
          SOCKET x = socket(...);
          bind(x,...);
          listen(x,...);
          for(;;)
           {
               auto s = accept(x,...);
               if (s == INVALID_SOCKET)
                   break;
    
               // save to a map, for example, after ensuring sync with a mutex and a lock guard
    
               m[s] = "some_id";
    
               std::thread t(newclient,s);
               s.detach();
           }
       }
    
       int main() // 
       {
              // WSAStartup and other init
    
              std::thread t(server);
              t.detach();
    
              // Message Loop or other GUI
    
       }
    

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 1970-01-01
      • 2023-04-02
      • 2021-09-15
      • 2018-01-22
      • 2014-04-29
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多