none
connect, socket API, is sending the connection request after long delay RRS feed

  • Pergunta

  • Hello All,

    In our application we need to communication with multiple devices simultaneously. We're creating threads to communicate with individual devices. We're are using non-blocking sockets. After making "connect", we wait for 5 seconds for application to get connected with device. Problem we're facing is that last device (10th) is online but we got it as offline. Reason- till the last moment of wait "connect" has not sent the connection request to device. After the 5 seconds device is receiving the connection request. As as solution we can-

    1. increase the wait time
    2. submit the job only in 7-8 threads at a time.
    3.  combination of above both

    But I'm just surprise on performance of socket API and wondering if we're missing the settings, which can helps to boost the performance.

    I'd appreciate any help. Please do let me know in case I need to give more information.

    Here is code we're using (it is legacy code)-


    int Connect_To_Port(SOCKET win_socket, char *host, unsigned short port, int connTimeout)
    {

     int    retval, err;
     SOCKADDR_IN  server;
     SOCKADDR_IN  client;
     unsigned long l;
     WSAEVENT  connEvent;

     

     memset(&server, 0, sizeof(server));
     server.sin_family  = AF_INET;
     server.sin_port   = htons(port);
     server.sin_addr.s_addr = htonl(INADDR_ANY); 

     

     l = inet_addr(host);
     memset(&client, 0, sizeof(client));
     client.sin_family = AF_INET;
     client.sin_port = htons(port);
     memcpy(&(client.sin_addr),&l,4);

     

     char szBuf[MAX_PATH];
     memset(szBuf,0,sizeof(szBuf));

     

     if (connTimeout > 0 && connTimeout < 180000)
     {
      // Mark our socket non-blocking, and ask for asynch notifications.
      connEvent = WSACreateEvent();
      if(WSAEventSelect(win_socket,connEvent,FD_CONNECT) == SOCKET_ERROR)
      {
          return BII_ERR_OPENSOCKET;
      }
     }
     
     DWORD dwStatus = 0;
     timeval readTimeout;
     fd_set fs;

     

     

     // connect to remote host
      retval = connect(win_socket,(sockaddr*)&client,sizeof(client));
     if (retval == SOCKET_ERROR)
     {
      err = WSAGetLastError();
      switch (err)
      {
       case (WSAEISCONN):   // already connected! no problem man

        return TRUE;
       case (WSAEWOULDBLOCK):     // not ready yet...return so that we don't block
        {

         
         DWORD dwStatus = 0;
         dwStatus = WSAWaitForMultipleEvents(1,&connEvent,FALSE,connTimeout,TRUE);
         
         if(dwStatus == WSA_WAIT_EVENT_0)
         {
          WSANETWORKEVENTS lpNetEvents;
          WSAEnumNetworkEvents(win_socket,connEvent,&lpNetEvents);
          if((lpNetEvents.lNetworkEvents & FD_CONNECT) && (lpNetEvents.iErrorCode[FD_CONNECT_BIT] == 0))
          {
           
           return TRUE;
          }
          else
          {
           
           return BII_ERR_OPENSOCKET;
          }
         }
         else
         {
          
          return BII_ERR_OPENSOCKET;
         }
        }
       default:
        
        return BII_ERR_OPENSOCKET; // real error
      }
     }
     return TRUE;
    }




    quarta-feira, 18 de fevereiro de 2009 10:46