开发者

C++ socket select() function returns 0 with many connections

I have a code where the server sends a UDP message which contains a TCP port number to clients. Then the server waits for incoming TCP connections with clients. However, the select() function returns timeout with many connections. I can figure out what might be the problem.

Any help and comments on the code below are very much appreciated. Thanks in advance for help.

void initialise( void ) 
{

   m_tcp_listensocket = getlistensocket();
  highsock = m_tcp_listensocket;

  setnonblocking(m_tcp_listensocket);
  struct timeval timeout;
  timeout.tv_sec = m_timeout;
  timeout.tv_usec = 0;


  // Send UDP messages to clients not connected
  for (int i = 0; i < UDP_Connections.size(); i++)
  {  
      // Only to clients not connected
      if ( TCP_Connections[i]->IsConnected() )
      {
        continue;
      }

      // making the message: 'server_address:server_port'.
      char l_str_server_port_number[6];
      sprintf ( l_str_server_port_number, "%d",  TCP_Connections[i]->get_server_port_number ());

      struct hostent  *host;

     if( (host = gethostbyname( m_host_name )) == NULL)
     {

       // gethostbyname() made error
     }    
      struct in_addr  MyAddress;
      memcpy( &MyAddress.s_addr, host->h_addr_list[0], sizeof( MyAddress.s_addr ) );

      std::string l_str_init_message = std::string(l_str_server_port_number) + "\0";

      UDP_Connections[i]->sendUDPMessage(l_str_init_message.c_str());
   }

  for (int i = 0; i < TCP_Connections.size(); i++)
    {
      struct sockaddr_in clientaddr;  
      bzero(&clientaddr, sizeof(clientaddr));
      clientaddr.sin_family = AF_INET;
      clientaddr.sin_addr.s_addr = htonl(INADDR_ANY);

      fd_set readSet;
      FD_ZERO(&readSet);
      FD_SET(m_tcp_listensocket, &readSet);

      int readsocks = select(highsock+1, &readSet, NULL, NULL, &time开发者_运维百科out);

      switch (readsocks)
    {
    case 0:
      /* timeout */   
      break;

    case -1:
      /* error */
      break;

    default:
      if (FD_ISSET (m_tcp_listensocket, &readSet))
        {
          socklen_t tempo = (socklen_t)sizeof(clientaddr);

          int l_Socket = accept(m_tcp_listensocket, (struct sockaddr *)&clientaddr, &tempo);
          if ( l_Socket > highsock )
        highsock =  l_Socket;

          if ( l_Socket >= 0 )
        {
          // connection accepted

          char adr[16] ;
          inet_ntop(PF_INET,&clientaddr.sin_addr, adr, sizeof(adr));

          for (int j= 0; j < UDP_Connections.size(); j++)
            {

              if ( UDP_Connections[j]->get_clientIP() == (std::string)adr )
            {     
              TCP_Connections[j]->set_TCPsocket( l_Socket );
              break;
            }
            }

        }
          else 
        {
          // socket error
        }
        }
    }
    }
}


Some implementations of select() update their timeout argument, you may need to reset this before each call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜