title: Socket Programming in C: When client exit server, the server is crashed
body: <p>I run a socket programming code for communication of multiple clients with one server. Everything happens properly but when I ctrl C to exit one of client, the server does not show as I expected. Below is the code:</p>
  
  <p>Client.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  #define PORT 4444
  
  int main(){
  
      int clientSocket, ret;
      struct sockaddr_in serverAddr;
      char buffer[1024];
  
      clientSocket = socket(AF_INET, SOCK_STREAM, 0);
      if(clientSocket &lt; 0){
          printf("[-]Error in connection.\n");
          exit(1);
       }
       printf("[+]Client Socket is created.\n");
  
       memset(&amp;serverAddr, '\0', sizeof(serverAddr));
       serverAddr.sin_family = AF_INET;
       serverAddr.sin_port = htons(PORT);
       serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  
      ret = connect(clientSocket, (struct sockaddr*)&amp;serverAddr, sizeof(serverAddr));
      if(ret &lt; 0){
           printf("[-]Error in connection.\n");
           exit(1);
       }
       printf("[+]Connected to Server.\n");
  
       while(1){
          printf("Client: \t");
          scanf("%s", &amp;buffer[0]);
          send(clientSocket, buffer, strlen(buffer), 0);
  
          if(strcmp(buffer, ":exit") == 0){
              close(clientSocket);
              printf("[-]Disconnected from server.\n");
              exit(1);
          }
  
          if(recv(clientSocket, buffer, 1024, 0) &lt; 0){
              printf("[-]Error in receiving data.\n");
          }else{
              printf("Server: \t%s\n", buffer);
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Server.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  #define PORT 4444
  
  int main(){
  
      int sockfd, ret;
      struct sockaddr_in serverAddr;
  
      int newSocket;
      struct sockaddr_in newAddr;
  
      socklen_t addr_size;
  
      char buffer[1024];
      pid_t childpid;
  
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if(sockfd &lt; 0){
          printf("[-]Error in connection.\n");
          exit(1);
      }
      printf("[+]Server Socket is created.\n");
  
      memset(&amp;serverAddr, '\0', sizeof(serverAddr));
      serverAddr.sin_family = AF_INET;
      serverAddr.sin_port = htons(PORT);
      serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  
      ret = bind(sockfd, (struct sockaddr*)&amp;serverAddr, sizeof(serverAddr));
      if(ret &lt; 0){
          printf("[-]Error in binding.\n");
          exit(1);
      }
      printf("[+]Bind to port %d\n", 4444);
  
      if(listen(sockfd, 10) == 0){
          printf("[+]Listening....\n");
      }else{
          printf("[-]Error in binding.\n");
      }
  
  
      while(1){
          newSocket = accept(sockfd, (struct sockaddr*)&amp;newAddr, &amp;addr_size);
          if(newSocket &lt; 0){
              exit(1);
          }
          printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
  
          if((childpid = fork()) == 0){
              close(sockfd);
  
              while(1){
                  recv(newSocket, buffer, 1024, 0);
                  if(strcmp(buffer, ":exit") == 0){
                      printf("Disconnected from %s:%d\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
                      break;
                  }else{
                      printf("Client: %s\n", buffer);
                      send(newSocket, buffer, strlen(buffer), 0);
                      bzero(buffer, sizeof(buffer));
                  }
              }
          }
  
       }
  
      close(newSocket);
  
  
      return 0;
  }
  </code></pre>
  
  <p>When I press ctrl C to exit the client. On the server side, it shows:</p>
  
  <pre><code>Client:
  Client:
  Client:
  Client:
  </code></pre>
  
  <p>and it's seem to loop "Client:" forever instead of showing message of printf"Disconnected from..." and continue to work with other clients as I expected. I look at this code from on youtube, they can run it properly in the video but I do not know why when I download this code and run on my computer, it gets that problem. Can anyone help me to fix that problem so that the server can print the message "Disconnection...". Thank you.</p>