title: Connect() functionality of socket communication
body: <p>A question on the wallclock time of socket communication.
  I am having a function, which finds the servers registered at a central server.
  I am adding a layer of network check over this function by extracting the URL and port number of the servers and trying to connect to them by behaving like a simple TCP client.
  If the return value is greater than 0, then it means that the network is working fine; if -1, then the network is broken.</p>
  <pre><code>printf(&quot;--Checking for network connectivity--\n&quot;);
          for(size_t i = 0; i &lt; serverOnNetworkSize; i++) {
             UA_ServerOnNetwork *server = &amp;serverOnNetwork[i];
             A[i] = (char *)UA_malloc(server-&gt;discoveryUrl.length+1);
             memcpy(A[i],server-&gt;discoveryUrl.data,server-&gt;discoveryUrl.length);
             A[i][server-&gt;discoveryUrl.length] = 0;
             int length = strlen(A[i]);
            
            //discovery URLs are of the form : opc.tcp://hostname:port
            
            //new addition to extract port
              B[i] = A[i] + 10;
            //printf(&quot;Hostname: %s\n&quot;, B[i]);
              char *p = strrchr(B[i], ':');
              int port = strtoul(p+1, NULL, 10);
            //printf(&quot;%d\n&quot;,port);
              B[i][length-5]='\0';
            //printf(&quot;Hostname: %s\n&quot;, B[i]);
           
  
           
             //removing the port
             A[i][length-5]='\0';
             //without initial tcp binding
             C[i] = A[i] + 10;
            //printf(&quot;Hostname: %s\n&quot;, C[i]);
  
            // FIND IP OF THAT HOST
             if(i!=0){
              char ip_address[50];
  
              find_ip_address(C[i],ip_address);  
              socketCommunication(ip_address,C[i],port);
         }
  }
            printf(&quot;--Checks done!--\n&quot;);
  
  </code></pre>
  <p>Global Funcitons:</p>
  <pre><code>int find_ip_address(char *hostname, char *ip_address)
  {
        struct hostent *host_name;
        struct in_addr **ipaddress;
        int count;
        if((host_name = gethostbyname(hostname)) == NULL)
        {
              herror(&quot;\nIP Address Not Found\n&quot;);
              return 1;
        }
        else
        {
              ipaddress = (struct in_addr **) host_name-&gt;h_addr_list;
              for(count = 0; ipaddress[count] != NULL; count++)
              {
                    strcpy(ip_address, inet_ntoa(*ipaddress[count]));
                    return 0;
              }
        }
        return 1;
  }
  
  void socketCommunication(char *ip_address,char *hostname, int port){
       int clientSocket,ret;
      struct  sockaddr_in serverAddr;
      char buffer[1024];
   
      
      clientSocket = socket(AF_INET,SOCK_STREAM,0);
      if(clientSocket&lt;0){
          printf(&quot;Error in connection \n&quot;);
          exit(1);
      }
  
      //printf(&quot;Client socket is created\n&quot;);
  
      memset(&amp;serverAddr,'\0',sizeof(serverAddr));
  
      serverAddr.sin_port = htons(port);
      serverAddr.sin_family=AF_INET;
      
      serverAddr.sin_addr.s_addr=inet_addr(ip_address);
  
      ret = connect(clientSocket,(struct sockaddr*)&amp;serverAddr,sizeof(serverAddr));
      if(ret&lt;0){
          printf(&quot;\nLOOKS LIKE NETWORK CONNECTION HAS FAILED. HAVE A LOOK AT    THE NETWORK CONNECTIVITY at host : %s\n&quot;,hostname);
          printf(&quot;\n----Updated Status Information----:\n&quot;);
          printf(&quot;Discovery URL : opc.tcp://%s:%d\n&quot;,hostname,port);
          printf(&quot;Status:CONNECTON TIMED OUT\n&quot;);
          printf(&quot;\n&quot;);
     }
  
  </code></pre>
  <p>To test this, I switch off the network from one of the registered servers.
  When I measure the time, it shows inconsistent values of 18seconds,24,38 seconds etc.</p>
  <p>These values occur when I switch the network of the server and run my application. On a second run of the same application, the value reduces to 2seconds or 1 second sometimes.</p>
  <p>Output:</p>
  <pre><code>
  LOOKS LIKE NETWORK CONNECTION HAS FAILED. HAVE A LOOK AT THE NETWORK CONNECTIVITY at host : o755-gksr
  
  ----Updated Status Information----:
  Discovery URL : opc.tcp://o755-gksr:4841
  Status:CONNECTON TIMED OUT
  
  --Checks done!--
  Time measured: 18 seconds.
  
  
  </code></pre>
  <p>Output on another try</p>
  <pre><code>--Checking for network connectivity--
  
  LOOKS LIKE NETWORK CONNECTION HAS FAILED. HAVE A LOOK AT THE NETWORK CONNECTIVITY at host : o755-gksr
  
  ----Updated Status Information----:
  Discovery URL : opc.tcp://o755-gksr:4841
  Status:CONNECTON TIMED OUT
  
  --Checks done!--
  Time measured: 0 seconds.
  
  </code></pre>
  <p>My question is : Why does it show inconsistent values? If the connection is not possible, should it not return -1 and show the error quickly?
  Is there any background process, which tries to establish the connection for a finite number of times before coming to a halt?</p>
  <p>Please let me know.</p>
  <p>Regards,
  Rakshan</p>