title: getaddrinfo linux manpage server example but using TCP instead of UDP
body: <p>I'm trying to convert the <a href="https://man7.org/linux/man-pages/man3/getaddrinfo.3.html" rel="nofollow noreferrer">server program example from getaddrinfo's linux manpage</a> from UDP to TCP.</p>
  <p>When running the program without any change first, I can communicate with a netcat client using UDP :</p>
  <pre><code>gcc main.c
  ./a.out 1234
  </code></pre>
  <pre><code>netcat -u 127.0.0.1 1234
  // this works here, I can send messages to the server
  </code></pre>
  <p>Then if I try with TCP :</p>
  <pre><code>netcat 127.0.0.1 1234
  </code></pre>
  <p><strong>^ this doesn´t connect and exists directly</strong></p>
  <p>I tried to change some attributes in the <code>addrinfo</code> struct :</p>
  <pre><code>hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
  hints.ai_socktype = SOCK_STREAM; /* Stream socket */
  hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
  </code></pre>
  <p>And it doesn´t connect either.</p>
  <p>Here is the whole code :</p>
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  
  #define BUF_SIZE 500
  
      int
  main(int argc, char *argv[])
  {
      struct addrinfo hints;
      struct addrinfo *result, *rp;
      int sfd, s;
      struct sockaddr_storage peer_addr;
      socklen_t peer_addr_len;
      ssize_t nread;
      char buf[BUF_SIZE];
  
      if (argc != 2) {
          fprintf(stderr, &quot;Usage: %s port\n&quot;, argv[0]);
          exit(EXIT_FAILURE);
      }
  
      memset(&amp;hints, 0, sizeof(hints));
      hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
      hints.ai_socktype = SOCK_STREAM; /* Stream socket */
      hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
      hints.ai_protocol = 0;          /* Any protocol */
      hints.ai_canonname = NULL;
      hints.ai_addr = NULL;
      hints.ai_next = NULL;
  
      s = getaddrinfo(NULL, argv[1], &amp;hints, &amp;result);
      if (s != 0) {
          fprintf(stderr, &quot;getaddrinfo: %s\n&quot;, gai_strerror(s));
          exit(EXIT_FAILURE);
      }
  
      /* getaddrinfo() returns a list of address structures.
         Try each address until we successfully bind(2).
         If socket(2) (or bind(2)) fails, we (close the socket
         and) try the next address. */
  
      for (rp = result; rp != NULL; rp = rp-&gt;ai_next) {
          sfd = socket(rp-&gt;ai_family, rp-&gt;ai_socktype,
                  rp-&gt;ai_protocol);
          if (sfd == -1)
              continue;
  
          if (bind(sfd, rp-&gt;ai_addr, rp-&gt;ai_addrlen) == 0)
              break;                  /* Success */
  
          close(sfd);
      }
      freeaddrinfo(result);           /* No longer needed */
  
      if (rp == NULL) {               /* No address succeeded */
          fprintf(stderr, &quot;Could not bind\n&quot;);
          exit(EXIT_FAILURE);
      }
  
      /* Read datagrams and echo them back to sender */
  
      for (;;) {
          peer_addr_len = sizeof(peer_addr);
          nread = recvfrom(sfd, buf, BUF_SIZE, 0,
                  (struct sockaddr *) &amp;peer_addr, &amp;peer_addr_len);
          if (nread == -1)
              continue;               /* Ignore failed request */
  
          char host[NI_MAXHOST], service[NI_MAXSERV];
  
          s = getnameinfo((struct sockaddr *) &amp;peer_addr,
                  peer_addr_len, host, NI_MAXHOST,
                  service, NI_MAXSERV, NI_NUMERICSERV);
          if (s == 0)
              printf(&quot;Received %zd bytes from %s:%s\n&quot;,
                      nread, host, service);
          else
              fprintf(stderr, &quot;getnameinfo: %s\n&quot;, gai_strerror(s));
  
          if (sendto(sfd, buf, nread, 0,
                      (struct sockaddr *) &amp;peer_addr,
                      peer_addr_len) != nread)
              fprintf(stderr, &quot;Error sending response\n&quot;);
      }
  }
  </code></pre>
  <p>How to make this server program accept TCP connections with netcat ?</p>