title: UDP Socket programming - fork server on recev
body: <p>I want to implement a UDP socket server (using c under Linux). I want to avoid hanging client by forking a new process when the server receives a new message from the client. (i don wanna use threading)</p>
  
  <p>The problem is that server keeps waiting (block on the recvfrom function), but when the client send the first message, the <strong>server keeps forking MANY processes</strong> (it looks like it is receiving the message more than one time or like the client is sending it many times, really i don know where the problem)</p>
  
  <p><strong>Server:</strong></p>
  
  <pre><code>// socket file descriptor (socket descriptor)
  int socket_fd;
  // serverTCP and client addresses
  struct sockaddr_in server_addr, client_addr;
  socklen_t sin_size;
  int bytes_read;
  char recv_data[1024];
  
  // create the socket
  if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
      perror("UDP Server: ERROR while creating the socket.\n");
      exit(1);
  }
  
  // define the socket address associated with the created socket
  // set the server address and port number
  
  // set the binding of the serverUDP socket
  printf("\nUDP Server: server socket binding...");
  if (bind(socket_fd, (struct sockaddr *) &amp;server_addr, sizeof(struct sockaddr)) == -1) {
      perror("UDP Server: ERROR while binding the socket.\n");
      exit(1);
  }
  printf("\nUDP Server: done binding.");
  
  sin_size = sizeof(struct sockaddr);
  // block and wait for connection request
  int pid; // process ID for forking new serverUDP process
  while (1) {
      printf("\nUDP Server: waiting for connection...");
      bytes_read = recvfrom(socket_fd, recv_data, 1023, 0,
              (struct sockaddr *) &amp;client_addr, &amp;sin_size);
  
      // a connection has been established
      recv_data[bytes_read] = '\0';
      printf("\nUDP Server: received -&gt; %s", recv_data);
      pid = fork();
  
      if (pid &lt; 0) {
          perror("UDP Server: ERROR while forking new process.\n");
          exit(1);
      }
      // check if the process ID is zero
      if (pid == 0) {
          // we are now inside the new forked process
          char result[50];
          int len = sprintf(result, "%d", server_parse_command(recv_data));
          len = sendto(socket_fd, result, len, 0,
                  (struct sockaddr *) &amp;client_addr, sin_size);
          close(socket_fd);
          exit(0);
      }
  }
  </code></pre>
  
  <p><strong>Client:</strong></p>
  
  <pre><code>int socket_fd;
  struct sockaddr_in server_addr;
  struct hostent *host;
  host = gethostbyname("127.0.0.1");
  
  // create the socket
  if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
      perror("UDP Client: ERROR while creating the socket.\n");
      exit(1);
  }
  // define the socket address associated with the created socket
      // set the client address and port number
  
  // send messages to the server socket
  int count;
  char buffer[128];
  while (1) {
      socklen_t sin_size = sizeof(struct sockaddr);
      // read message to send to the server and set count to the length of the message
  
      count = sendto(socket_fd, buffer, count, 0,
              (struct sockaddr *) &amp;server_addr, sizeof(struct sockaddr));
      if (count &lt; 0) {
          perror("UDP Client: ERROR while writing message to server socket.\n");
          exit(1);
      }
      bzero(buffer, 128);
      count = recvfrom(socket_fd, buffer, 127, 0,
              (struct sockaddr *) &amp;server_addr, &amp;sin_size) ;
  
      if (count &lt; 0) {
          perror("UDP Client: ERROR while reading response from server socket.\n");
          exit(1);
      }
      buffer[count] = '\0';
      printf("\nUDP Client: received response -&gt; %s", buffer);
      close(socket_fd);
  }
  </code></pre>
  
  <p>Sorry for the long code.</p>