title: Port Scanner in C++ Hangs
body: <p>I made a simple C port-scanner program in Linux. The main algorithm is try calling connect, if it returns -1, I was not able to connect, else I connected the host. This works fine in my computer, it lists my open ports. However, in my home there is another computer (behind same router with mine) and I give it's IP but program hangs. I tried google.com, facebook.com or any other website and they do not even respond to first request. What am I doing wrong? </p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>for(port=0; port&lt;=65536; port++)  
  {  
      struct sockaddr_in addr;        
      addr.sin_family = AF_INET;  
      addr.sin_port = htons(port);  
      addr.sin_addr = *((struct in_addr *)he-&gt;h_addr);  
      memset(&amp;(addr.sin_zero), '\0', 8);  
      if (connect(sockfd, (struct sockaddr *)&amp;addr, sizeof(struct sockaddr)) == -1) {
          printf("%i closed\n", port);  
          close(sockfd);  
      }  
      else {  
          printf("%i open\n", port);  
          close(sockfd);  
      }  
  } 
  </code></pre>