title: unix socket send() succeeds, but recv() fails
body: <p>I am writing a simple client server program using unix domain sockets, but am having issues with the recv() call in my client program.</p>
  
  <p>The program executes as follows:</p>
  
  <ul>
  <li>Server sets up socket and waits for a connection</li>
  <li>Client connects and sends a string</li>
  <li>Server receives string, and sends string back to client (like an echo)</li>
  <li>Client recv() call fails, returning "resource temporarily unavailable"</li>
  <li>Client exits</li>
  <li>Server waits for another connection</li>
  </ul>
  
  <p>I have also tried using a poll() call in my client to wait for the response from the server.
  In this case however, the recv() call simply receives a 0, implying the connection has been closed serverside, which it has not.</p>
  
  <p>I have exhausted google on this error, but no fixes I came accross seem applicable to my code.</p>
  
  <p>I have included my client (with poll() code commented out) and server code below.</p>
  
  <p>I'm probably missing something obvious... but any insight would be greatly appreciated!</p>
  
  <p>Server code:</p>
  
  <pre><code>    /*
   *  testServer.c
   *
   */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;termios.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;poll.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;sys/un.h&gt;
  #include &lt;linux/spi/spidev.h&gt;
  #include &lt;linux/sockios.h&gt;
  #include &lt;errno.h&gt;
  
  #define SOCK_PATH "/var/run/ts.serv" 
  
  void handleSockIO(int *sockDesc);
  
  int main ()
  {
      int sock;
      struct sockaddr_un sock_addr;
      int len, p;
      struct pollfd poll_fd[1];
  
      printf("[TS] testServer Started.\r\n");
  
      if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
      {
          perror("[TS]wr_sock creation");
      }
      else 
      {
          printf("[TS] Created socket descriptor.\r\n");
      }
  
  
      sock_addr.sun_family = AF_UNIX;
      strcpy(sock_addr.sun_path, SOCK_PATH);
      unlink(sock_addr.sun_path);
  
      len = strlen(sock_addr.sun_path) + sizeof(sock_addr.sun_family);
  
      if (bind(sock, (struct sockaddr *)&amp;sock_addr, len) == -1) 
      {
          perror("[TS]sock bind failed\r\n");
      }
      else 
      {
          printf("[TS] Bound socket to sock_addr.\r\n");
      }
  
      if (listen(sock, 5) == -1) 
      {
          perror("[TS] sock listen fail");
      }
      else 
      {
          printf("[TS] Socket now listening.\r\n");
      }
  
      poll_fd[0].fd = sock;
      poll_fd[0].events = POLLIN;
  
      printf("[TS] Waiting for a connection...\r\n");
  
      while (1) 
      {
          p = poll(poll_fd, 1, 1); //Wait for 1 ms for data
  
          if (p == -1) 
          {
              perror("[TS] Poll");
          } 
          else if (p == 0) 
          {
              //printf("Timeout occurred!\n"); 
          } 
          else 
          {
              if (poll_fd[0].revents &amp; POLLIN)//Data available to read without blocking
              { 
                  printf("[TS] Data available on sock..\r\n");
                  handleSockIO(&amp;sock);
  
                  printf("[TS] Waiting for another connection...\r\n");
              }
          }
      }//While(1)
  
      return 0;
  }
  
  void handleSockIO(int *sockDesc)
  {
      int ioSock, n;
      socklen_t t;
      struct sockaddr_un remote_addr;
      char str[15];
  
      memset(str, ' ', sizeof(str));
  
      t = sizeof(remote_addr);
  
      if ((ioSock = accept(*sockDesc, (struct sockaddr *)&amp;remote_addr, &amp;t)) == -1) 
      {
          perror("accept failed\r\n");
      }
      else 
      {
          printf("[TS] Receiving...\r\n");
  
          n = recv(ioSock, str, sizeof(str), 0);
  
          if (n &lt; 0) 
              printf("[TS] recvfrom failed: %s\r\n", strerror(errno));
          else if(n == 0)
          {
              printf("[TS] Received %d on ioSock...\r\n", n);
          }
          else if(n &gt; 0)
          {
              printf("[TS] Received: %s, which is %d long.\r\n", str, strlen(str));
  
              printf("[TS] Echoing response...\r\n");
  
              if (send(ioSock, str, n, 0) == -1) //Echo str back
              {
                  printf("[TS] send failed: %s\r\n", strerror(errno));
              }
              else 
              {
                  printf("[TS] Send successful\r\n");
              }
  
              //============Wait to close IO descriptor=================
              int r;
              char temp[1]; //Arbitrary buffer to satisfy recv()
              do 
              {
                  printf("[TS] Waiting for client to close connection...\r\n");
                  r = recv(ioSock, temp, sizeof(temp), 0);
  
                  if (r == 0)
                  {
                      printf("[TS] Client closed connection, closing ioSock...\r\n");
                      close(ioSock);
                  }
              } while (r != 0);
  
              //========================================================
          }//if(n&gt;0) else...
      }       
  }
  </code></pre>
  
  <p>Client code:</p>
  
  <pre><code> /*
   *  testClient.c
   *
   */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;termios.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;poll.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;sys/un.h&gt;
  #include &lt;linux/sockios.h&gt;
  #include &lt;errno.h&gt;
  
  #define CHAR_BUF_SIZE 15
  #define SEND_STRING "Hello!"
  #define SOCK_PATH "/var/run/ts.serv" 
  
  int main ()
  {
      char str[CHAR_BUF_SIZE] = {0};
      int c, len, n, p;
      int s; // s will hold a socket descriptor returned by socket()
      struct sockaddr_un serv_addr;
      struct pollfd poll_fd[1];
  
      printf("[TC] testClient Started.\r\n");
  
      //===============SOCKET SETUP===============================
  
      if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
      {
          printf("[TC] Socket failed: %s\r\n", strerror(errno));
      }
  
      memset(&amp;serv_addr, '0', sizeof(serv_addr));
  
      serv_addr.sun_family = AF_UNIX;
      strcpy(serv_addr.sun_path, SOCK_PATH);
  
      len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
  
      //==========================================================
  
      //  printf("[TC]Trying to connect to TS socket...\r\n");
  
      //===============RESPONSE POLL SETUP========================
  
      poll_fd[0].fd = s;
      poll_fd[0].events = POLLIN;
  
      //==========================================================
  
      printf("[TC] Connecting to SOCK_PATH...\r\n");
  
      c = connect(s, (struct sockaddr *)&amp;serv_addr, len);
  
      if (c == -1)
      {
          printf("[TC] Connection failed: %s\r\n", strerror(errno));
      }
      else 
      {
          printf("[TC] Connected. Sending string....\r\n");
  
          if (send(s, SEND_STRING, strlen(SEND_STRING), 0) == -1) 
          {
              printf("[TC] send() failed: %s\r\n", strerror(errno));
          }
          else 
          { 
              printf("[TC] Send on SOCK_PATH successful.\r\n");
              //Sending complete------------------------------------------------
              //Wait for response...
  
              printf("[TC] Waiting for server response...\r\n");
  
  //          p = poll(poll_fd, 1, -1); //Wait for a response
  //          
  //          if (p == -1) 
  //          {
  //              perror("[TC] Poll");
  //          } 
  //          else 
  //          {
  //              if(poll_fd[0].revents &amp; POLLIN)
  //              {
                      n = recv(s, str, sizeof(str), 0);
  
                      if (n &lt; 0) 
                      {
                          printf("[TC] Receive on SOCK_PATH failed: %s\r\n", 
                                 strerror(errno));
                      }
                      else if(n == 0)
                      {
                          printf("[TC] %d Received on SOCK_PATH.\r\n", n);
                      }
                      else if(n &gt; 0)
                      {
                          printf("[TC] Received %d from SOCK_PATH: %s\r\n", 
                                 n, str);
                      }
  //              }
  //          }
          }//if(send())
      }//if(connect())
  
      printf("[TC] Transction complete, closing connection and exiting.\r\n");
  
      close(s);
  
      return 0;
  }
  </code></pre>