Advertisement
krot

Connect timeout

Sep 21st, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. //connect to the server
  2.     iResult = connect(client, (sockaddr*)&server, sizeof(server));
  3.    
  4.     if(iResult == SOCKET_ERROR)
  5.     {
  6.         iError = WSAGetLastError();
  7.         //check if error was WSAEWOULDBLOCK, where we'll wait
  8.         if(iError == WSAEWOULDBLOCK)
  9.         {
  10.             cout << "Attempting to connect.\n";
  11.             fd_set Write, Err;
  12.             TIMEVAL Timeout;
  13.             int TimeoutSec = 10; // timeout after 10 seconds
  14.  
  15.             FD_ZERO(&Write);
  16.             FD_ZERO(&Err);
  17.             FD_SET(client, &Write);
  18.             FD_SET(client, &Err);
  19.  
  20.             Timeout.tv_sec = TimeoutSec;
  21.             Timeout.tv_usec = 0;
  22.  
  23.             iResult = select(0,         //ignored
  24.                              NULL,      //read
  25.                              &Write,    //Write Check
  26.                              &Err,      //Error Check
  27.                              &Timeout);
  28.             if(iResult == 0)
  29.             {
  30.                 cout << "Connect Timeout (" << TimeoutSec << " Sec).\n";
  31.                 system("pause");
  32.                 return 1;
  33.                
  34.             }
  35.             else
  36.             {
  37.                 if(FD_ISSET(client, &Write))
  38.                 {
  39.                     cout << "Connected!\n";
  40.                 }
  41.                 if(FD_ISSET(client, &Err))
  42.                 {
  43.                     cout << "Select error.\n";
  44.                     system("pause");
  45.                     return 1;
  46.                 }
  47.             }
  48.         }
  49.         else
  50.         {
  51.             cout << "Failed to connect to server.\n";
  52.             cout << "Error: " << WSAGetLastError() << endl;
  53.             WSACleanup();
  54.             system("pause");
  55.             return 1;
  56.         }
  57.     }
  58.     else
  59.     {
  60.         //connected without waiting (will never execute)
  61.         cout << "Connected!\n";
  62.     }
  63. ///////////////////////
  64. bool connect(char *host,int port, int timeout)
  65. {
  66.     TIMEVAL Timeout;
  67.     Timeout.tv_sec = timeout;
  68.     Timeout.tv_usec = 0;
  69.     struct sockaddr_in address;  /* the libc network address data structure */  
  70.  
  71.     sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  72.  
  73.     address.sin_addr.s_addr = inet_addr(host); /* assign the address */
  74.     address.sin_port = htons(port);            /* translate int2port num */
  75.     address.sin_family = AF_INET;
  76.  
  77.     //set the socket in non-blocking
  78.     unsigned long iMode = 1;
  79.     int iResult = ioctlsocket(sock, FIONBIO, &iMode);
  80.     if (iResult != NO_ERROR)
  81.     {  
  82.         printf("ioctlsocket failed with error: %ld\n", iResult);
  83.     }
  84.        
  85.     if(connect(sock,(struct sockaddr *)&address,sizeof(address))==false)
  86.     {  
  87.         return false;
  88.     }  
  89.  
  90.     // restart the socket mode
  91.     iMode = 0;
  92.     iResult = ioctlsocket(sock, FIONBIO, &iMode);
  93.     if (iResult != NO_ERROR)
  94.     {  
  95.         printf("ioctlsocket failed with error: %ld\n", iResult);
  96.     }
  97.  
  98.     fd_set Write, Err;
  99.     FD_ZERO(&Write);
  100.     FD_ZERO(&Err);
  101.     FD_SET(sock, &Write);
  102.     FD_SET(sock, &Err);
  103.  
  104.     // check if the socket is ready
  105.     select(0,NULL,&Write,&Err,&Timeout);           
  106.     if(FD_ISSET(sock, &Write))
  107.     {  
  108.         return true;
  109.     }
  110.  
  111.     return false;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement