title: C TCP sockets: read () on socket returns Resourse temporarily unavailable
body: <p>The system I realize consists of a ring of tokens (each token is a ckient-server process).</p>
  
  <p>I tested it runnimy two processes.</p>
  
  <p>In function <code>serverMessageProcessor()</code> I call <code>readMessage()</code> function, in which I use <code>read()</code> to read all parts of a message from an (in)socket and (after processes it) write another message on (out)socket.</p>
  
  <p>The first time I do it, it works well, the second time, one of the <code>read()</code> function in my <code>readMessage()</code> function, gives <code>read: Success</code> (I use <code>perror</code>  to catch read error) and <code>read: Resourse temporarily unavailable</code> on the other process.</p>
  
  <p>There are some prints for debugging in the code. 
  I hope you can help me.
  Thanks a lot in advance.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/socket.h&gt;     //per le funzioni sui socket.
  #include &lt;arpa/inet.h&gt;      //per la funzione inet_aton().
  #include &lt;netinet/in.h&gt;     //per la struct sockaddr.
  #include &lt;sys/wait.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;ctype.h&gt;          //per la funzione isdigit().
  #include &lt;fcntl.h&gt;          //per rendere il socket bloccante o no.
  #include &lt;sys/select.h&gt;
  
  /* Error hendler */
  void sys_error (const char *message) {
      perror (message);
  }
  
  
  /* Connessione in ingresso */
  int setUpConnectionIn(int port, int *sock_fd){
      int sock_in;
      struct sockaddr_in addr;
  
  
      addr.sin_family = AF_INET;                  //Internet Address Family.
      addr.sin_port=htons(port);                  //Local port.
      addr.sin_addr.s_addr = htonl(INADDR_ANY);   //Any incoming interface.
  
      //Create socket for incoming connections.
      if((sock_in = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) {
          stampaStr("Errore: Creazione socket non riuscita.", 1);
          return -1;
      }
  
      //Bind to the local address.
      if(bind(sock_in, (struct sockaddr *) &amp;addr, sizeof(addr)) &lt; 0){
          sys_error("Errore: impossibile assegnare l'indirizzo al socket.");
          close(sock_in);
          return -1;
      }
  
      //Mark the socket so it will listen for incoming connections.
      if(listen(sock_in,5) &lt; 0) {
          sys_error("Errore: listen non riuscita.");
          close(sock_in);
          return -1;
      }
  
      *sock_fd = sock_in;
  
      return 0;
  }
  
  /* Interfaccia Connessione in ingresso */
  int setUpConnectionIn_2 (int sock_fd){
      int connection_in;
  
  
      if ((connection_in = accept(sock_fd, NULL, NULL)) &lt; 0) {
          sys_error ("Errore in funzione setUpConnectionIn_2");
          return -1;
      }
  
  
      return connection_in;
  }
  
  /* Connessione in uscita */
  int setUpConnectionOut (int *sock_fd, int connection_port, char *conn_addr){
      int check;
      struct sockaddr_in addr_out;
      int sock_out;
      addr_out.sin_family = AF_INET;
      addr_out.sin_port = htons(connection_port);
  
      if (inet_aton(conn_addr, &amp;addr_out.sin_addr) &lt; 0) {
          sys_error ("Errore: inet_aton");
          return -1;
      }
  
  
      if((sock_out = socket(PF_INET, SOCK_STREAM, 0))&lt;0) {
          sys_error("Errore: Creazione socket non riuscita.");
          return -1;
      }
  
      stampaStr("In attesa di connessione alla stazione successiva..\n", 1);
      while(connect(sock_out, (struct sockaddr *)&amp;addr_out, sizeof(addr_out)) &lt; 0);
  
      *sock_fd = sock_out;
  
      return 0;
  }
  
  int closed_ring = 0;
  
  int readMessage (int *fd, unsigned char *cod, int *size, char **content) {
      char *content_msg;
      unsigned char cod_msg;
      int size_msg;
  
      int retval = fcntl(*fd, F_SETFL, fcntl(*fd, F_GETFL) | O_NONBLOCK);
  
      printf ("readMessage - prima read\n");
  
      if (read(*fd, &amp;cod_msg, 1) == 0) {
          sys_error ("read - cod_msg");
          return -1;
      }
  
      if (read(*fd, &amp;size_msg, 4) == 0) {
          sys_error ("read - size_msg");
          return -1;
      }
  
      *content = (char *)malloc((size_msg+1)*sizeof(char));
      (*content)[size_msg+1] = '\0';
      if (read(*fd, *content, size_msg) == 0) {
          sys_error ("read - content_msg");
          return -1;
      }
  
      retval = fcntl(*fd, F_SETFL, (fcntl(*fd, F_GETFL) | O_NONBLOCK) &amp; (~O_NONBLOCK));
  
      printf ("readMessage - dopo read\n");
      printf ("(readMessage)cod: %c - size: %d - msg: %s\n", cod_msg, size_msg, *content);
      *cod = cod_msg;
      *size = size_msg;
      //content = (char *)malloc(size_msg*sizeof(char));
      //memcpy(content, content_msg, strlen(content_msg));
      //content = content_msg;
      printf ("content: %s\n", *content);
      return 0;
  } 
  
  int buildMessage (char **new_msg, unsigned char cod, int size, char *content) {
      int msg_len = size+6;
      *new_msg = (char *)malloc(msg_len*sizeof(char));
      (*new_msg)[0] = cod;
      memcpy (((*new_msg)+1), &amp;size, 4);
      memcpy (((*new_msg)+5), content, size);
      (*new_msg)[msg_len] = '\0';
  
      //new_msg = msg;
      return strlen (*new_msg);
  }
  
  int serverMessageProcessor (int *fd_in, int *out_fds) {
      char *new_msg;
      char *content_msg;
      unsigned char cod_msg = 100;
      int size_msg;
      int msg_length;
      char *tmp_PID_str;
      uint64_t myPID;
      uint64_t tempPID;
      int fd;
  
      if (readMessage (fd_in, &amp;cod_msg, &amp;size_msg, &amp;content_msg) &lt; 0) {
          sys_error ("readMessage");
          return -1;
      }
  
      printf ("(serverMessageProcessor) cod: %c - size: %d - msg: %s\n", cod_msg, size_msg, content_msg);
  
      printf ("serverMessageProcessor - prima switch\n");
      switch (cod_msg) {
          case ('1'): {
              fd = 0;
              if ((myPID = getpid()) &lt; 0) {
                  sys_error ("getpid");
                  return -1;
              }
  
              tmp_PID_str = (char *)malloc((size_msg+1)*sizeof(char));
              tmp_PID_str[size_msg+1] = '\0';
              printf ("serverMessageProcessor - prima memcpy(myPID) PID: %ld\n", myPID);
              memcpy (tmp_PID_str, &amp;myPID, sizeof(uint64_t));
              printf ("serverMessageProcessor - dopo memcpy(myPID)\n");   
  
              if (strcmp (content_msg, tmp_PID_str) == 0) {
                  printf ("serverMessageProcessor - dopo strcmp TRUE (myPID, tempPID)\n");
                  closed_ring = 1;
              } else {
                  //new_msg = (char *)malloc(size_msg+6)*sizeof(char));
                  printf ("serverMessageProcessor - dopo strcmp FALSE (myPID, tempPID)\n");
                  //msg_length = buildMessage (&amp;new_msg, cod_msg, size_msg, content_msg);
                  msg_length = size_msg+6;
                  new_msg = (char *)malloc(msg_length*sizeof(char));
                  new_msg[0] = cod_msg;
                  memcpy (new_msg+1, &amp;size_msg, 4);
                  memcpy (new_msg+5, content_msg, size_msg);
                  new_msg[msg_length] = '\0';
                  printf ("serverMessageProcessor - dopo buildMessage: %s\n", new_msg);
  
                  printf ("(serverMessageProcessor) cod: %c - size: %d - msg: %s\n", cod_msg, size_msg, content_msg);
  
                  if (write (out_fds[fd], &amp;cod_msg, 1) &lt; 0) {
                      sys_error ("write");
                      return -1;
                  }
  
                  if (write (out_fds[fd], &amp;size_msg, 4) &lt; 0) {
                      sys_error ("write");
                      return -1;
                  }
  
                  if (write (out_fds[fd], content_msg, size_msg) &lt; 0) {
                      sys_error ("write");
                      return -1;
                  }
  
                  printf ("serverMessageProcessor - dopo write\n");
              }
  
              printf ("serverMessageProcessor - fuori if strcmp(myPID, tempPID)\n");
          }
      }
  }
  
  int main (int argc, char *argv[]) {
      int listen_port, connection_port;
      int connection;
      int check = -1;
      char *conn_addr;
      char inter_lv = 'a';
      int sock_in;   //socket descriptor for reading.
      int sock_out;  //socket descriptor for writing.
      struct sockaddr_in addr_in;
      struct sockaddr_in addr_out;
  
      int connection_flag = 0;
      int out_fds[3];
      /********************* VARIABILI GESTIONE MESSAGGI ************************/
      char *new_msg = NULL;
      char *content_msg = NULL;
      unsigned char cod_msg;
      int size_msg;
      int msg_len;
      uint64_t tempPID;
      uint64_t myPID;
      /**************************************************************************/
  
      /********************** VARIABILI GESTIONE SOTTOPROCESSI ******************/
      int pipe_IN[2];
      int pipe_OUT[2];
      int pipe_CLIENT[2];
      int pid_client;
      /**************************************************************************/
  
      /***************************** VARIABILI PER SELECT SYSCALL ***************/
      fd_set rfds;
      struct timeval tv;
      int retval;
      /**************************************************************************/
  
      if(readIntFromString(0, &amp;listen_port, argv[1]) &lt; 0){
          printf("Invalid value of listen_port\n");
          return -1;
      }
  
      if(listen_port &gt;= 0 &amp;&amp; listen_port &lt;= 1023){
          printf("Insert a port in range 0-1023\n");
          return -1;
      }
  
      if(listen_port &gt;= 49152){
          printf("Insert a port not higher than 49152\n");
          return -1;
      }
  
      if(readIntFromString(0, &amp;connection_port, argv[2]) &lt; 0){
          printf("Invalid value of connection_port\n");
          return -1;
      }
  
      if(connection_port &gt;= 0 &amp;&amp; connection_port &lt;= 1023){
          printf("Insert a port in range 0-1023\n");
          return -1;
      }
  
      if(connection_port &gt;= 49152){
          printf("Insert a port not higher than 49152\n");
          return -1;
      }
  
      conn_addr = argv[3];        
  
      if((toupper(argv[4][0]) == 'S' || toupper(argv[4][0]) == 'N') &amp;&amp; argv[4][1] == '\0'){
          inter_lv = toupper(argv[4][0]);
      }else{
          printf("Invalid Interactivity Level\n");
          return -1;
      }
  
  
      /*********************** Circuit Building **************************/
  
  
      if(setUpConnectionIn(listen_port, &amp;sock_in) &lt; 0) {
          sys_error ("Errore in funzione: setUpConnectionIn");
          return -1;
      }
  
      if (setUpConnectionOut (&amp;sock_out, connection_port, conn_addr) &lt; 0) {
          sys_error ("Errore in funzione setUpConnectionOut");
          return -1;
      }
  
      printf ("-------------out-connected-------------\n");
  
      if ((connection = setUpConnectionIn_2 (sock_in)) &lt; 0) {
          sys_error ("Errore in funzione setUpConnectionIn_2");
          return -1;
      }
  
      printf ("-------------in-connected--------------\n");
  
      /**********************************************************************/
  
      /* Controlla connection (socket in entrata) per vedere se Ã¨ pronto per operazioni di I/O */
      //retval = fcntl(connection, F_SETFL, fcntl(connection, F_GETFL) | O_NONBLOCK);
      FD_ZERO(&amp;rfds);
      FD_SET(connection, &amp;rfds);
  
      out_fds[0] = sock_out;
      /* Attende 5 secondi */
  
      tv.tv_sec = 5;
      tv.tv_usec = 0;
  
      while (closed_ring == 0) {
          if (connection_flag == 0) {
              if ((myPID = getpid()) &lt; 0) {
                  sys_error ("getpid");
                  return -1;
              }
  
              size_msg = sizeof(uint64_t);
              msg_len = size_msg+6;
              new_msg = (char*)malloc(msg_len*sizeof(char));
  
              new_msg[0] = '1';
              memcpy(new_msg+1, &amp;size_msg, 4);
              memcpy(new_msg+5, &amp;myPID, size_msg);
              new_msg[msg_len] = '\0';
  
              printf ("(MAIN) myPID: %ld - cod: %c - size: %d - msg: %s\n", myPID, new_msg[0], size_msg, &amp;(new_msg[5]));
  
              if (write (out_fds[0], new_msg, msg_len) &lt; 0) {
                  sys_error ("write");
                  return -1;
              }
  
              printf ("Message 1 (ring closing) sent: %s\n", new_msg);
              connection_flag = 1;
          }
  
          retval = select(connection+1, &amp;rfds, NULL, NULL, &amp;tv);
  
          if (retval == -1) {
              sys_error ("select");
              return -1;
          } else if (retval) {
              stampaStr("Available data on (in)socket\n", 1);   /* FD_ISSET(0, &amp;rfds) avrÃ  valore TRUE */
  
              /*************** DEBUG ************************
              if (read (connection, &amp;cod_msg, 1) == 0) {
                  sys_error ("read - main");
                  return -1;
              } else {
                  printf ("read - main: %c\n", cod_msg);
              }
              **********************************************/
              if (serverMessageProcessor (&amp;connection, out_fds) &lt; 0) {
                  sys_error ("serverMessageProcessor");
                  return -1;
              }
          } else {
              stampaStr("None message in entry\n", 1);
              char c = getchar();
              fflush(stdin);
          }
      }
  
  
      /* Creazione sottoprocesso per gestione connessioni con Client per iscrizione auto */
      if (pipe(pipe_CLIENT) &lt; 0) {
          sys_error ("pipe_client");
          return -1;
      }
  
      if ((pid_client = fork()) &lt; 0) {
          sys_error ("fork_client");
          return -1;
      } else if (pid_client == 0) { /* Processo figlio - accetta connessioni da Client */
          printf("Subprocess to manage client(s) connection\n");
          //while (1);
      } else { /* Processo Padre */
          printf("Parent process\n");
      }
  
  
  
      return 0;
  }
  </code></pre>