title: unix socket datagram: server receive invalid characters
body: <p>I'm trying to create a Unix socket datagram example. The client can send and receive message smoothly. The server is able to do that just in case the buffer length is smaller than <code>SOCKET_BUFFER_SIZE</code>, otherwise the server's output shows some invalid characters at received buffer.</p>
  <p>Client's code</p>
  <pre class="lang-c prettyprint-override"><code>#include &quot;ud_hdr.h&quot;
  
  #define IN_BUF_SIZE     200
  
  int main(int argc, char **argv)
  {
      struct sockaddr_un  svr_addr, cli_addr;
      int                 sfd, i;
      ssize_t             num_rw;
      socklen_t           svr_addr_len;
      char                sock_buf[SOCK_BUF_SIZE];
      char                in_buf[IN_BUF_SIZE];
  
      // Create sockets:
      sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
      if (sfd == -1) {
          printf(&quot;Error line|%d|: %s&quot;, __LINE__, strerror(errno));
          exit(EXIT_FAILURE);
      }
  
      // ! In Unix domain, the client needs to assign an address to its socket
      // ! if it wants to RECEIVE datagrams sent by server.
      memset(&amp;cli_addr, 0, sizeof(struct sockaddr_un));
      cli_addr.sun_family = AF_UNIX;
      snprintf(cli_addr.sun_path, sizeof(cli_addr.sun_path), \
              &quot;/tmp/dxduc/ud_cli.%ld&quot;, (long)getpid());
      if (bind(sfd, (struct sockaddr *) &amp;cli_addr, sizeof(struct sockaddr_un)) == -1) {
          printf(&quot;Error line|%d|: %s\n&quot;, __LINE__, strerror(errno));
          exit(EXIT_FAILURE);
      }
  
      // Construct server's address:
      memset(&amp;svr_addr, 0, sizeof(struct sockaddr_un));
      svr_addr.sun_family = AF_UNIX;
      strncpy(svr_addr.sun_path, SOCKET_SV_PATH, sizeof(svr_addr.sun_path) - 1);
  
      printf(&quot;Client is ready to run\n&quot;);
      while (1) {
          printf(&quot;Enter new msg: &quot;);
          fflush(stdout); // * clear the output buffer and move the buffered data to output (a.k.a console in this case)
          memset(in_buf, 0, sizeof(in_buf));
          memset(sock_buf, 0, sizeof(sock_buf));
          num_rw = read(STDIN_FILENO, in_buf, IN_BUF_SIZE);
  
          // Remove '\n' and '\r' in input:
          for (i = 0; i &lt; IN_BUF_SIZE; i++) {
              // if ((in_buf[i] == '\r') || (in_buf[i] == '\n')) {
              if (in_buf[i] == '\n') {
                  in_buf[i] = 0x0;
              }
          }
  
          printf(&quot;Msg to server: |%s|, len = %ld, num_rw = %ld\n&quot;, in_buf, strlen(in_buf), num_rw);
          svr_addr_len = sizeof(struct sockaddr_un);
          if (num_rw &gt; 0) {
              // Send msg to server:
              if (sendto(sfd, in_buf, strlen(in_buf), 0, (struct sockaddr *) &amp;svr_addr, svr_addr_len) != strlen(in_buf)) {
                  printf(&quot;Error line|%d|: %s\n&quot;, __LINE__, strerror(errno));
                  exit(EXIT_FAILURE);
              }
          } else if (num_rw == 0) {
              printf(&quot;Nothing to send to server\n&quot;);
              continue;
          } else if (num_rw == -1) {
              printf(&quot;Error line|%d|: %s\n&quot;, __LINE__, strerror(errno));
              exit(EXIT_FAILURE);
          }
  
          // Read response from server
          num_rw = recvfrom(sfd, sock_buf, SOCK_BUF_SIZE, 0, (struct sockaddr *) &amp;svr_addr, &amp;svr_addr_len);
          if (num_rw == -1) {
              printf(&quot;Error line|%d|, %s&quot;, __LINE__, strerror(errno));
              exit(EXIT_FAILURE);
          } else {
              printf(&quot;Receive from server: |%s|, len=%ld, num_rw=%ld\n&quot;, sock_buf, strlen(sock_buf), num_rw);
          }
      }
  }
  </code></pre>
  <p>Server's code</p>
  <pre class="lang-c prettyprint-override"><code>#include &quot;ud_hdr.h&quot;
  
  int main(int argc, char **argv)
  {
      struct sockaddr_un  svr_addr, cli_addr;
      int                 sfd, i;
      ssize_t             num_rw;
      socklen_t           cli_addr_len;
      char                buf[SOCK_BUF_SIZE];
  
      // Create socket:
      sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
      if (sfd == -1) {
          printf(&quot;Error line|%d|: %s\n&quot;, __LINE__, strerror(errno));
          exit(EXIT_FAILURE);
      }
  
      // Verify socket's name path:
      if (strlen(SOCKET_SV_PATH) &gt; (sizeof(svr_addr.sun_path) - 1)) {
          printf(&quot;Error line|%d|: invalid socket name&quot;, __LINE__);
          exit(EXIT_FAILURE);
      }
  
      // Remove any existing file having the same namepath with server's:
      if ((remove(SOCKET_SV_PATH) == -1) &amp;&amp; (errno != ENOENT)) {
          printf(&quot;Error line|%d|: %s\n&quot;, __LINE__, strerror(errno));
          exit(EXIT_FAILURE);
      }
  
      // Bind the socket to desired addr:
      memset(&amp;svr_addr, 0, sizeof(struct sockaddr_un));
      svr_addr.sun_family = AF_UNIX;
      strncpy(svr_addr.sun_path, SOCKET_SV_PATH, sizeof(svr_addr.sun_path) - 1);
  
      if (bind(sfd, (struct sockaddr *)&amp;svr_addr, sizeof(struct sockaddr_un)) == -1) {
          printf(&quot;Error line|%d|: %s\n&quot;, __LINE__, strerror(errno));
          exit(EXIT_FAILURE);
      }
  
      while (1) {
          // Receive data from client:
          memset(buf, 0, sizeof(buf));
          cli_addr_len = sizeof (struct sockaddr_un);
          num_rw = recvfrom(sfd, buf, SOCK_BUF_SIZE, 0, (struct sockaddr *) &amp;cli_addr, &amp;cli_addr_len);
  
          if (num_rw == -1) {
              printf(&quot;Error line|%d|: %s\n&quot;, __LINE__, strerror(errno));
              exit(EXIT_FAILURE);
          }
  
          printf(&quot;Server received %ld bytes from %s, ret_buf=|%s|, len=%ld\n&quot;, (long)num_rw, cli_addr.sun_path, buf, strlen(buf));
  
          // Uppercase received characters:
          for (i = 0; i &lt; num_rw; i++) {
              buf[i] = toupper((unsigned char)buf[i]);
          }
  
          // Send back uppercase-ed characters to client:
          printf(&quot;Send back to client: |%s|, len=%ld\n&quot;, buf, strlen(buf));
          if (sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *) &amp;cli_addr, cli_addr_len) != strlen(buf)) {
              printf(&quot;Error line|%d|: %s\n&quot;, __LINE__, strerror(errno));
              exit(EXIT_FAILURE);
          }
      }
  }
  </code></pre>
  <p>Header file</p>
  <pre class="lang-c prettyprint-override"><code>#ifndef _UD_HDR_H_
  #define _UD_HDR_H_
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/un.h&gt;
  
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;ctype.h&gt;
  
  // * The Makefile should have created /tmp/dxduc before run this program
  #define SOCKET_SV_PATH      &quot;/tmp/dxduc/ud_basic&quot;
  #define SOCK_BUF_SIZE            10
  
  #endif // _UD_HDR_H_
  </code></pre>
  <p>This is what I got</p>
  <pre class="lang-console prettyprint-override"><code># Client
  Client is ready to run
  Enter new msg: stack  
  Msg to server: |stack|, len = 5, num_rw = 6
  Receive from server: |STACK|, len=5, num_rw=5
  Enter new msg: stackoverflow
  Msg to server: |stackoverflow|, len = 13, num_rw = 14
  Receive from server: |STACKOVERF|, len=10, num_rw=10
  
  #Server
  Server received 5 bytes from /tmp/dxduc/ud_cli.2385, ret_buf=|stack|, len=5
  Send back to client: |STACK|, len=5
  Server received 10 bytes from /tmp/dxduc/ud_cli.2385, ret_buf=|stackoverf�  |, len=14
  Send back to client: |STACKOVERF�   |, len=14
  </code></pre>
  <p>It's weird that the client received the expected message, though the server did not format the message correctly before sending it.</p>
  <p>This is my expected output</p>
  <pre class="lang-console prettyprint-override"><code># Client
  Client is ready to run
  Enter new msg: stack  
  Msg to server: |stack|, len = 5, num_rw = 6
  Receive from server: |STACK|, len=5, num_rw=5
  Enter new msg: stackoverflow
  Msg to server: |stackoverflow|, len = 13, num_rw = 14
  Receive from server: |STACKOVERF|, len=10, num_rw=10
  
  #Server
  Server received 5 bytes from /tmp/dxduc/ud_cli.2385, ret_buf=|stack|, len=5
  Send back to client: |STACK|, len=5
  Server received 10 bytes from /tmp/dxduc/ud_cli.2385, ret_buf=|stackoverf|, len=10
  Send back to client: |STACKOVERF|, len=14
  </code></pre>
  <p>How should I solve it?</p>