title: What does recv() write into bufferif data received is less than buffer's length
body: <p>I'm trying to write a server in C using sockets which will be receiving commands from multiple clients. What I'm trying to understand is: if the client sends command which consists of, say, 4 characters, while recv() function is told to receive 5 bytes of data, what will happen?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;time.h&gt;
  #include &lt;dirent.h&gt;
  
  void *connection_handler(void *);
  int sendall(int s, char *buf, int len, int flags);
  int recvall(int s, char *buf, int len, int flags);
  
  int main (int argc , char *argv[])
  {
  char check = 'n', *message;
  int socket_d, port, socket_n, *sock_n, c;
  struct sockaddr_in server_addr, client_addr;
  
  socket_d = socket(AF_INET , SOCK_STREAM , 0);
  if (socket_d == -1)
  {
      printf("Could not create socket. Exiting...\n");
      return 1;
  }
  
  printf("Welcome to Random server v0.0.1.\n");
  
  printf("Please input port (default - 3425): ");
  scanf("%i", &amp;port);
  if (port &gt;= 65536 || port &lt; 0) port = 3425;
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  server_addr.sin_port = htons(port);
  
  if( bind(socket_d,(struct sockaddr *)&amp;server_addr , sizeof(server_addr)) &lt; 0)
  {
      printf("Binding socket failed.\n");
      return 1;
  }
  printf("Binding socket done.\n");
  
  listen(socket_d, 3);
  
  printf("Waiting for connections...\n");
  c = sizeof(struct sockaddr_in);
  
  while( (socket_n = accept(socket_d, (struct sockaddr *)&amp;client_addr, (socklen_t*)&amp;c)) )
      {
          puts("Connection accepted");
          message = "Hello Client, I have received your connection. And now I will assign a handler for you\n";
          send(socket_n , message , strlen(message), 0);
  
          pthread_t sniffer_thread;
          sock_n = malloc(1);
          *sock_n = socket_n;
  
          if( pthread_create( &amp;sniffer_thread , NULL ,  connection_handler , (void*) sock_n) &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");
      }
  
  if (socket_n&lt;0)
      {
          printf("Accept failed.\n");
          return 1;
      }
  return 0;
  }
  void *connection_handler(void *socket_d)
  {
  printf("Thread created\n");
  int sock = *(int*)socket_d;
  int read_size;
  char *message , client_message[200];
  
  while (1)
  {
      message = malloc(5);
      read_size = recv(sock , message , 5 , 0);
      strcpy(client_message, message);
      //send(sock , message , strlen(message), 0);
      free(message);
  
      if (strcmp(client_message, "list") == 0)
      {
          DIR * dir;
          struct dirent * de;
          strcpy(client_message, "");
          if ((dir = opendir(".")) == NULL ) strcpy(client_message, "Unable to open the directory.\n");
          else
          {
              while (de = readdir(dir)) {strcat(client_message, de-&gt;d_name); strcat(client_message, "\n"); }
          }
          closedir(dir);
          send(sock, client_message, strlen(client_message), 0);
          continue;
      }
      if (strcmp(client_message, "date") == 0)
      {
          time_t rtime;
          rtime = time (NULL);
          strcpy(client_message, ctime(&amp;rtime));
          send(sock, client_message, strlen(client_message), 0);
          continue;
      }
      if (strcmp(client_message, "mkdir") == 0)
      {
          int result_code = mkdir("./new dir");
          if (result_code == 0) strcpy(client_message, "OK");
          else strcpy(client_message, "ERROR");
          send(sock, client_message, strlen(client_message), 0);
          continue;
      }
      if (strcmp(client_message, "exit") == 0)
      {
          strcpy(client_message, "Bye");
          send(sock, client_message, strlen(client_message), 0);
          break;
      }
      send(sock, client_message, strlen(client_message), 0);
  
  
      if(read_size == 0)
      {
          printf("Client disconnected\n");
          fflush(stdout);
          break;
      }
      else if(read_size == -1)
      {
          printf("Recv failed\n");
      }
  }
  
  printf("Thread will now be deleted.\n");
  free(socket_d);
  
  return 0;
  }
  </code></pre>
  
  <p>As of now, it seems that it will write those 4 characters into buffer AND something else, to make it five bytes long. Am I correct? What exactly is happening? How could I make it so that server could process commands of any length?</p>