title: The accept() function in the server doesn't seem to accept new connection from the client
body: <p>I was trying the code from <em>UNIX Network Programming</em>, the socket networking API 
  Where I encountered a the code for a simple server client and server based example </p>
  
  <p>the code for server is </p>
  
  <pre><code>#define BL 10
  
  void *get_in_addr(struct sockaddr *sa)
  {
    if (sa-&gt;sa_family == AF_INET)
    {
      return &amp;(((struct sockaddr_in*) sa)-&gt;sin_addr);
    }
    return &amp;(((struct sockaddr_in6*) sa)-&gt;sin6_addr);
  }
  
  int main(int argc, char ** argv)
  {
    struct sockaddr_in server, client;
    int listenfd;
    int port;
  
    if (argc &lt; 2)
    {
      printf("Port Not specified Error ~~ ! ");
      exit(0);
    }
  
    port = atoi(argv[1]);
    memset(&amp;server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htons(INADDR_ANY);
    server.sin_port = htons(port);
  
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
  
    if (bind(listenfd, (struct sockaddr *) &amp;server, sizeof(server)) == 0)
      printf("\n [~] Bind Sucessfull \n");
  
    if (listen(listenfd, BL) == 0)
      printf("\n [~] Listenig on port %d \n\n ", port);
  
    while (1)
    {
      socklen_t clientlen = sizeof(client);
      printf("%d", clientlen);
      int conffd = accept(listenfd, (struct sockaddr *) &amp;client, &amp;clientlen);
      char s[INET6_ADDRSTRLEN];
      inet_ntop(AF_INET, get_in_addr((struct sockaddr *) &amp;client), s, sizeof(s));
      if (conffd == -1)
        printf("\n not Accepted \n");
      else
      {
        printf("Got a connection from %s", s);
      }
    }
  
    return 0;
  }
  </code></pre>
  
  <p>and then the code for the client is </p>
  
  <pre><code>int main(int argc, char ** argv)
  {
  
    struct sockaddr_in server;
    int listenfd;
    int port = 11111;
    memset(&amp;server, 0, sizeof(server));
    inet_pton(AF_INET, argv[1], &amp;server.sin_addr);
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
  
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if (connect(listenfd, (struct sockaddr *) &amp;server, sizeof(server)) != 0)
    {
      printf("Connection error ! \n");
    }
  
    return 0;
  }
  </code></pre>
  
  <p>The problem here is when I start the server and run the client , nothing happens in the server  but its supposed to say " got  a connection from  #IP " </p>
  
  <p>the output is : </p>
  
  <pre><code>./server 11111
  
  [~] Bind Sucessfull 
  
  [~] Listenig on port 11111 
  </code></pre>
  
  <p>the should have been output :</p>
  
  <pre><code>./server 11111
  
  [~] Bind Sucessfull 
  
  [~] Listenig on port 11111 
  
  Got an connection from 127.0.0.1
  </code></pre>
  
  <p>So I thought there was some thing wrong with the client ,</p>
  
  <p>so I checked it , I closed the server and ran the client again now it said connection error </p>
  
  <p>which means that the client is working fine , so the problem was with server's accept() </p>
  
  <p>I can't seem to find  whats causing the problem . </p>
  
  <p>If anyone could explain whats wrong would be really helpfull :) </p>