title: Deny a connection to a server C socket programming
body: <p>I have a little problem : my server understand that he can't accept a new connection but the client still thinks he's connected.
  I don't know how to fix this.</p>
  
  <p>My client code :</p>
  
  <pre><code>#include&lt;stdio.h&gt; //printf
  #include&lt;string.h&gt;    //strlen
  #include&lt;sys/socket.h&gt;    //socket
  #include&lt;arpa/inet.h&gt; //inet_addr
  #include &lt;fcntl.h&gt; // for open
  #include &lt;unistd.h&gt; // for close
  
  int main(int argc , char *argv[]){
      int sock;
      struct sockaddr_in server;
      char message[1000] , server_reply[2000];
  
      //Create socket
      sock = socket(AF_INET , SOCK_STREAM , 0);
      if (sock == -1){
          printf("Could not create socket");
      }
      puts("Socket created");
  
      server.sin_addr.s_addr = inet_addr("127.0.0.1");
      server.sin_family = AF_INET;
      server.sin_port = htons(8080);
  
      //Connect to remote server
      if (connect(sock , (struct sockaddr *)&amp;server , sizeof(server)) &lt; 0){
          perror("connect failed. Error");
          return 1;
      }
  
      puts("Connected\n");
  
      //keep communicating with server
      while(1){
          printf("Enter message : ");
          scanf("%s" , message);
  
          //Send some data
          if(send(sock , message , strlen(message) , 0) &lt; 0){
              puts("Send failed");
              return 1;
          }
  
          //Receive a reply from the server
          if(recv(sock , server_reply , 2000 , 0) &lt; 0){
              puts("recv failed");
              break;
          }
  
          puts("Server reply :");
          puts(server_reply);
      }
  
      close(sock);
      return 0;
  }
  </code></pre>
  
  <p>And my server code :</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;    //strlen
  #include&lt;stdlib.h&gt;    //strlen
  #include&lt;sys/socket.h&gt;
  #include&lt;arpa/inet.h&gt; //inet_addr
  #include&lt;unistd.h&gt;    //write
  #include&lt;pthread.h&gt; //for threading , link with lpthread
  
  #define MAX_PLAYER 4
  
  void *connection_handler(void *);
  
  int nbrePlayer = 0;
  
  int main(int argc , char *argv[]){
    int socket_desc , client_sock , c , *new_sock;
  
    struct sockaddr_in server , client;
  
    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
      printf("Could not create socket");
    }
    puts("Socket created");
  
    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(8080);
  
    //Bind
    if( bind(socket_desc,(struct sockaddr *)&amp;server , sizeof(server)) &lt; 0){
      //print the error message
      perror("bind failed. Error");
      return 1;
    }
    puts("bind done");
  
    //Listen
    if(listen(socket_desc , 4)&lt;0){
      perror("listen");
      return 1;
    }
  
    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);
  
    while((client_sock = accept(socket_desc, (struct sockaddr *)&amp;client, (socklen_t*)&amp;c))&gt;0){
      if(nbrePlayer&gt;=MAX_PLAYER){
        puts("connection refused");
        close(client_sock);
        continue;
      } else {
        puts("Connection accepted");
      }
      nbrePlayer++;
      pthread_t sniffer_thread;
      new_sock = malloc(1);
      *new_sock = client_sock;
  
      if( pthread_create( &amp;sniffer_thread , NULL ,  connection_handler , (void*) new_sock) &lt; 0){
        perror("could not create thread");
        return 1;
      }
  
      //Now join the thread , so that we dont terminate before the thread
      //pthread_join( sniffer_thread , NULL);
      puts("Handler assigned");
      printf("%d\n",nbrePlayer);
    }
  
    if (client_sock &lt; 0){
      perror("accept failed");
      return 1;
    }
  
    return 0;
  }
  
  /*
  * This will handle connection for each client
  * */
  void *connection_handler(void *socket_desc){
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size;
    char *message , client_message[2000];
  
    //Receive a message from client
    while( (read_size = recv(sock , client_message , 2000 , 0)) &gt; 0 ){
      //Send the message back to client
      write(sock , client_message , strlen(client_message));
    }
  
    if(read_size == 0){
      puts("Client disconnected");
      fflush(stdout);
    }
    else if(read_size == -1){
      perror("recv failed");
    }
  
    //Free the socket pointer
    free(socket_desc);
    `nbrePlayer`--;
    //Delete the player for the shared memory
    return 0;
  }
  </code></pre>
  
  <p>Thx forward for your answer</p>