title: Unix C Internet Domain Socket: Server unresponsive and Client unable to bind to localhost
body: <p>I'm fairly new to Unix domain sockets. I am attempting to write a simple client/server application. The server will run in the background and print a message stating it received a connection at the host and service name that the client exists at. My client's  hostname is dependent upon user input, as such I made the user inputted hostname "localhost". However, when I attempt to do this, my client program is unable to find a suitable address to connect the socket at. Furthermore, the only way I am able to successfully get my client to join the socket to an address is by keeping my hostname blank- and although this successfully connects the socket to an address, my server does not print any message that it received a connection from a client that exists at x host and y servicename. Per my understanding of the man page for getaddrinfo() leaving the hostname blank but not setting the hints.ai_flags to AI_PASSIVE simply sets the hostname to be the loopback address. So it appears my server does not send back the successful connection message since my client is sending back to itself it's own message ("Connection Attempt Successful"). Why can I not create a socket at the localhost hostname? My code for the server and client are such:</p>
  
  <pre><code>#include &lt;netdb.h&gt;
  #define BACKLOG 20
  #include "server.h"
  #include "stddef.h"
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  
  
  int main(){
      int cfd,afd;
      char service[NI_MAXSERV];
      char host[NI_MAXHOST];
      struct addrinfo hints;
      struct addrinfo *result, *rp;
      struct sockaddr_storage claddr;
      char addrstr[NI_MAXHOST+NI_MAXSERV+10];
      memset(&amp;hints,0,sizeof(struct addrinfo));
      socklen_t socklen;
      hints.ai_family=AF_UNSPEC; 
      hints.ai_socktype=SOCK_STREAM;
      hints.ai_next=NULL;
      hints.ai_canonname=NULL;
      hints.ai_addr=NULL;
      hints.ai_flags=AI_PASSIVE | AI_NUMERICSERV;
      if ((getaddrinfo(NULL,PORT_NUM,&amp;hints,&amp;result))!=0){
          perror("Failed to get result pointer of address structs");
      }
      for (rp=result;rp=rp-&gt;ai_next;rp!=NULL){
          cfd=socket(rp-&gt;ai_family,rp-&gt;ai_socktype, rp-&gt;ai_protocol);
          if (cfd==-1){
              continue;
          }
          if (bind(cfd,rp-&gt;ai_addr, rp-&gt;ai_addrlen)==0){
              break;
          }
  
          close(cfd);
      } 
      if (rp==NULL){
  
          perror("Reached end of address list without finding suitable socket address space");
      }
      freeaddrinfo(result);
      listen(cfd,BACKLOG);
      for (;;){
          socklen=sizeof(struct sockaddr);
          afd=accept(cfd,(struct sockaddr*) &amp;claddr,&amp;socklen);
          if (afd==-1){
              perror("Accept failed");
          }
          getnameinfo((struct sockaddr*) &amp;claddr,socklen,
              host,NI_MAXHOST,service,NI_MAXSERV,0);
          snprintf(addrstr,NI_MAXSERV+NI_MAXHOST+10, "(%s, %s)", host,service);   
          printf("Connection received from %s", addrstr);
  
      }
  
  }
  </code></pre>
  
  <p>and client:</p>
  
  <pre><code>#include &lt;netdb.h&gt;
  #include "server.h"
  #include "stddef.h"
  #include &lt;stdio.h&gt;
  
  int main(int argc, char*argv[]){
      struct addrinfo hints;
      struct addrinfo *result, *rp;
      int cfd;
      socklen_t socklen;
      hints.ai_family=AF_UNSPEC; //can accept IPV4 or IPV6
      hints.ai_socktype=SOCK_STREAM;
      hints.ai_next=NULL; 
      hints.ai_canonname=NULL;
      hints.ai_addr=NULL;
      hints.ai_flags=AI_NUMERICSERV; 
      if ((getaddrinfo(argv[1],PORT_NUM,&amp;hints,&amp;result))!=0){
          perror("getaddrinfo failed");
      }
      for (rp=result;rp=rp-&gt;ai_next;rp!=NULL){
          cfd=socket(rp-&gt;ai_family,rp-&gt;ai_socktype, rp-&gt;ai_protocol);
          if (cfd==-1){
              continue;
          }
          if (connect(cfd,rp-&gt;ai_addr,rp-&gt;ai_addrlen)!=-1){
              printf("Connection attempt successful");
              break;
          }
          close(cfd);
      }
      if (rp==NULL){
          perror("Could not connect socket to any address");
      }
      freeaddrinfo(result);
  
  }
  </code></pre>