title: Random port connection while portscan
body: <p>I've made a very basic port-scan program in C to scan a port range. Here it is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  
  #define HOST "127.0.0.1"
  #define PORT 4444
  
  int createConnection(const char *host[], const int port)
  {
      struct sockaddr_in addr;
      int sock = 0;
      int ret;
      struct sockaddr_in server_addr;
      if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) &lt; 0)
      {
          printf("Error %d socket creating.\n", sock);
          return -1;
      }
      else
      {
          memset(&amp;server_addr, '0', sizeof(server_addr));
  
          server_addr.sin_family = AF_INET;
          server_addr.sin_port = htons(port);
          ret = inet_pton(AF_INET, host, &amp;server_addr.sin_addr);
          if (ret &lt;= 0)
          {
              printf("Error %d unsuported address: %d\n", ret);
              return -2;
          }
          else
          {
              ret = connect(sock, (struct sockaddr *)&amp;server_addr, sizeof(server_addr));
              if (ret &lt; 0)
              {
                  //printf("[-] Port %d closed.\n", port);
                  close(sock);
                  return 1;
              }
              else
              {
                  printf("[+] Port %d open.\n", port);
                  close(sock);
                  return 0;
              }
          }
  
      }
  }
  
  int main(int argc, const char *argv[])
  {
      for (int i = 1; i &lt; 65536; i++)
      {
          createConnection("127.0.0.1", i);
      }
  }
  </code></pre>
  
  <p>As you can see in the for loop, it scans from port 1 to port 65535. The problem is that when I start it, I get this output:</p>
  
  <pre><code>[+] Port 42178 open.
  [+] Port 48650 open.
  [+] Port 60078 open.
  </code></pre>
  
  <p>The "open ports" always change, but are always superior to 40000. But, I checked with <code>netstat -tulpn</code>, only my port 68 is open for the dhclient, which is UDP and not TCP. Why does it say that I have open ports ?</p>