title: Client/Server Socket Communication (AF_UNIX)
body: <p>I'm trying to write a client program and a server program in which when the client connects to the server, the server sends a random string from a file back to it. Here is what I have so far (reading from file omitted):</p>
  
  <h1>server.c</h1>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;signal.h&gt;
  
  int listfd;
  int connfd;
  
  int main(int argc, char *argv[]){
  
      /*
      * Create Sockets
      */
      listfd = socket(AF_UNIX, SOCK_STREAM, 0);
      if(listfd == -1)
          exit(-1);
  
      struct sockaddr saddr = {AF_UNIX, "server"};
      socklen_t saddrlen = sizeof(struct sockaddr) + 6;
      bind(listfd, &amp;saddr, saddrlen);
  
      listen(listfd, 10);
  
      fflush(stdout);
      printf("Running...\n");
  
      /*
      * Listen for connections
      * and send random phrase on accept
      */
      while(1){
          connfd = accept(listfd, NULL, NULL);
  
          int r = rand() % num_of_lines; //Pick random phrase/hint pair
          write(connfd, phrases[r], strlen(phrases[r]));
  
          close(connfd);
          sleep(1);
      }
  
      exit(0);
  }
  </code></pre>
  
  <h1>client.c</h1>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ioctl.h&gt;
  
  int main(int argc, char *argv[])
  {
      int sock;
      int conn;
  
      struct sockaddr saddr = {AF_UNIX, "server"};
      socklen_t saddrlen = sizeof(struct sockaddr) + 6;
  
      sock = socket(AF_UNIX, SOCK_STREAM, 0);
  
      conn = connect(sock, &amp;saddr, saddrlen);
  
      char BUFF[1024];
  
      read(sock, BUFF, 1024);
  
      printf("%s", BUFF);
  
      return 0;
  }
  </code></pre>
  
  <p>My problem arises when I try to print in the client. I run the server, but when I run the client, it only prints garbage characters, and I'm not entirely sure what's causing this.</p>
  
  <p>Any help would be much appreciated, thank you!</p>
  
  <h1>EDIT:</h1>
  
  <p>I figured out my problem. Because the server socket was bound to "server", which was also the name of the executable, this caused a lot of issues.</p>
  
  <p>Renaming the sockaddr.sa_data field fixed my problem.</p>