title: Bad Address when reading from sockets in C
body: <p>i'm facing a problem that i m not able to debug (it seems), let me explain the situation. I have a server that do something and 10 clients to connect. To test the code i use the same machine to host and connect the server and clients. Now, it seems that sometimes i m able to connect a random number of clients, like 4 and during the fifth connection the client side, after a good connection with the server, when i try to read from socket it gives me Bad address.</p>
  <p>Now this number seems pretty casual, infact this error on the client side is given sometimes during the third connection, sometimes during the sixth connection and rarely never when i try to connect 10 clients. anyway most of the time this error occurs during the fifth connection.</p>
  <p>Here the codes of server and client</p>
  <p>Client:</p>
  <pre><code>void contact_server(int sockfd, int buffer_size, int reading_pipe, int writing_pipe) { 
      float* buff = (float*)calloc(buffer_size,sizeof(float));
      int ret = 0;
      while(1){
          ret = 0;   
          while(ret == 0){
              if(sizeof(float)*buffer_size &lt;= sizeof(float)*2048){
                  ret = read(sockfd, buff, sizeof(float)*buffer_size);
                  if(ret == -1){
                      printf(&quot;(from server) Error description is: %s\n&quot;,strerror(errno));
                      exit(0);
                  }
              }
              else{
                  long long unsigned int sum = 0;
                  ret = read(sockfd, buff, sizeof(float)*2048);
                  if(ret == -1){
                      printf(&quot;(from server) Error description is : %s\n&quot;,strerror(errno));
                      exit(0);
                  }
                  sum+=ret;
                  int count;
                  for(count = 2048; sum &lt; sizeof(float)*buffer_size; count+=2048, sum+=ret){
                      if(buffer_size-count &lt; 2048){
                          ret = read(sockfd, &amp;buff[count], sizeof(float)*(buffer_size-count));
                          if(ret == -1){
                              printf(&quot;(from server) Error description is : %s\n&quot;,strerror(errno));
                              exit(0);
                          }
                      }
                      else{
                          ret = read(sockfd, &amp;buff[count], sizeof(float)*2048);
                          if(ret == -1){
                              printf(&quot;(from server) Error description is : %s\n&quot;,strerror(errno));
                              exit(0);
                          }
                      }
                  }
              }
          }// waiting for server
          ret = write(writing_pipe,buff, sizeof(float)*buffer_size);// writing to parent process
          if(ret == -1){
              printf(&quot;(to parent) Error description is : %s\n&quot;,strerror(errno));
              exit(0);
          }
          ret = 0;
          while(ret == 0){
              ret = read(reading_pipe, buff, sizeof(float)*buffer_size);
              if(ret == -1){
                  printf(&quot;(from parent) Error description is : %s\n&quot;,strerror(errno));
                  exit(0);
              }
          }// waiting for parent process
          ret = write(sockfd,buff, sizeof(float)*buffer_size);// writing to server
          if(ret == -1){
              printf(&quot;(to server) Error description is : %s\n&quot;,strerror(errno));
              exit(0);
          }
          
      }
      free(buff);
  } 
  
  
  /* See server.c for more specific details
   * 
   * 
   * Inputs:
   *             
   *                 @ int port:= the port of the server
   *                 @ char* server_address:= the server address
   *                 @ int buffer_size:= the buffer size written by the server and parent process
   *                 @ int reading_pipe:= to read from parent
   *                 @ int writing_pipe:= to write to parent
   * */
  int run_client(int port, char* server_address, int buffer_size, int reading_pipe, int writing_pipe){
      
      int sockfd, connfd; 
      struct sockaddr_in servaddr, cli;
      int sockaddr_len = sizeof(struct sockaddr_in);
    
      // socket create and varification 
      sockfd = socket(AF_INET, SOCK_STREAM, 0); 
      if (sockfd == -1) { 
          fprintf(stderr,&quot;Error: socket creation failed...\n&quot;); 
          exit(1); 
      } 
      else
          printf(&quot;Socket successfully created..\n&quot;); 
          
      bzero(&amp;servaddr, sizeof(servaddr)); 
    
      // assign IP, PORT 
      servaddr.sin_family = AF_INET; 
      servaddr.sin_addr.s_addr = inet_addr(server_address); 
      servaddr.sin_port = htons(port); 
    
      // connect the client socket to server socket 
      if (connect(sockfd, (struct sockaddr *)&amp;servaddr, sizeof(servaddr)) != 0) { 
          printf(&quot;connection with the server failed...\n&quot;); 
          exit(0); 
      } 
      else
          printf(&quot;connected to the server..\n&quot;);  
    
      // function for chat 
      contact_server(sockfd,buffer_size,reading_pipe,writing_pipe); 
    
      // close the socket 
      close(sockfd);
      
      return 0;
      
  }
  </code></pre>
  <p>Server:</p>
  <pre><code>void* server_thread(void* _args) {
      
      // depacking args
      thread_args_server* args = (thread_args_server*) _args;
      float* buff = (float*)calloc(args-&gt;buffer_size,sizeof(float));
      int ret;
      while(1){
          ret = 0;
          while(ret == 0){
              ret = read(args-&gt;reading_pipe, buff, sizeof(float)*args-&gt;buffer_size);
              if(ret == -1){
                  printf(&quot;Error description is : %s\n&quot;,strerror(errno));
                  exit(0);
              }
          }// waiting for parent process
          ret = write(args-&gt;client_desc,buff, sizeof(float)*args-&gt;buffer_size);// writing to client
          if(ret == -1){
              printf(&quot;Error description is : %s\n&quot;,strerror(errno));
              exit(0);
          }
          ret = 0;
          
          while(ret == 0){
              if(sizeof(float)*args-&gt;buffer_size &lt;= sizeof(float)*2048){
                  ret = read(args-&gt;client_desc, buff, sizeof(float)*args-&gt;buffer_size);
                  if(ret == -1){
                      printf(&quot;Error description is : %s\n&quot;,strerror(errno));
                      exit(0);
                  }
              }
              else{
                  long long unsigned int sum = 0;
                  ret = read(args-&gt;client_desc, buff, sizeof(float)*2048);
                  if(ret == -1){
                      printf(&quot;Error description is : %s\n&quot;,strerror(errno));
                      exit(0);
                  }
                  sum+=ret;
                  long long unsigned int count;
                  for(count = 2048;sum &lt; sizeof(float)*args-&gt;buffer_size; count+=2048, sum+=ret){
                      if(args-&gt;buffer_size-count &lt; 2048){
                          ret = read(args-&gt;client_desc, buff, sizeof(float)*(args-&gt;buffer_size-count));
                          if(ret == -1){
                              printf(&quot;Error description is : %s\n&quot;,strerror(errno));
                              exit(0);
                          }
                      }
                      else{
                          ret = read(args-&gt;client_desc, buff, sizeof(float)*2048);
                          if(ret == -1){
                              printf(&quot;Error description is : %s\n&quot;,strerror(errno));
                              exit(0);
                          }
                      }
                  }
              }
          }// waiting for client
          ret = write(args-&gt;writing_pipe,buff, sizeof(float)*args-&gt;buffer_size);// writing to parent process
          if(ret == -1){
              printf(&quot;Error description is : %s\n&quot;,strerror(errno));
              exit(0);
          }
      }
      
      free(buff);
      
      return NULL;
  }
  
  
  /* This function creates a server on your current ip address on the port: port,
   * accept a maximum number of connections = max_num_conn, and create each thread per connection.
   * each thread read a float vector from a client and writes on the writing_pipes[i] this vector, then
   * wait for a float vector from reading_pipe[i] and send this vector to the client.
   * The ideal situation is: client -&gt; compute some instances of the mini batch, send to server the partial derivatives
   * the thread of the client on the server side read these partial derivatives, send back to the father process with writing pipes,
   * the parent process sum up all these partial derivatives, update the model, send back to the thread the model updated, the thread
   * send to the client and the client goes on and compute again the new partial derivatives, and so on...
   * 
   * Inputs:
   * 
   *                 @ int port:= the server port
   *                 @ int max_num_conn:= the maximum number of connections accepted by the server
   *                 @ int* reading_pipes:= a pipe writing for float vector to the thread
   *                 @ int* writing_pipes:= where the thread write its vector
   *                 @ int buffer_size:= the buffer of the vector
   * 
   * */
  int run_server(int port, int max_num_conn, int* reading_pipes, int* writing_pipes, int buffer_size, char* ip){
      int socket_desc, client_desc,sockaddr_len = sizeof(struct sockaddr_in),c = 1;
      int ret;
      struct sockaddr_in server_addr;
      struct sockaddr_in* client_addr;
      struct sockaddr_in* client_addr2;
      
      bzero(&amp;server_addr, sizeof(server_addr)); 
      inet_pton(AF_INET, ip, &amp;(&amp;server_addr)-&gt;sin_addr);
      // socket creation
      socket_desc = socket(AF_INET,SOCK_STREAM,0);
      if(socket_desc == -1){
          fprintf(stderr,&quot;Error: can't create socket\n&quot;);
          exit(1);
      }
      
      // Which connection can accept
      
      server_addr.sin_addr.s_addr = INADDR_ANY;
      
      // Ip family
      server_addr.sin_family = AF_INET;
      // Port
      
      server_addr.sin_port = htons(port);
      
      // Handling crash case to reuse the descriptor
      
      ret = setsockopt(socket_desc,SOL_SOCKET,SO_REUSEADDR,&amp;c,sizeof(int));
      if(ret == -1){
          fprintf(stderr,&quot;Error: setsockopt failed..\n&quot;);
          exit(1);
      }
      
      // Binding newly created socket to given IP and verification 
      if ((bind(socket_desc, (struct sockaddr *)&amp;server_addr, sizeof(server_addr))) != 0) { 
          printf(&quot;socket bind failed...\n&quot;); 
          exit(0); 
      } 
      else
          printf(&quot;Socket successfully binded..\n&quot;); 
      
      // listen on tot number of connections
      ret = listen(socket_desc,max_num_conn);
      if(ret == -1){
          fprintf(stderr,&quot;Error, listen failed\n&quot;);
          exit(1);
      }
      int i = 0;
      while(1){
          client_addr = calloc(1,sizeof(struct sockaddr_in));
          client_desc = accept(socket_desc,(struct sockaddr*)&amp;client_addr,(socklen_t*)&amp;sockaddr_len);
          if((client_desc == 1 &amp;&amp; errno == EINTR)) continue;
          thread_args_server* thread = (thread_args_server*)malloc(sizeof(thread_args_server));
          thread-&gt;idx = i;
          thread-&gt;client_desc = client_desc;
          thread-&gt;client_addr = client_addr;
          thread-&gt;reading_pipe = reading_pipes[i];
          thread-&gt;writing_pipe = writing_pipes[i];
          thread-&gt;buffer_size = buffer_size;
          pthread_t t;
          pthread_create(&amp;t,NULL,server_thread,thread);
          pthread_detach(t);
          printf(&quot;connected client id: %d\n&quot;,i);
          i++;
      }
      
      pthread_exit(NULL);
      return 0;
      
  }
  </code></pre>
  <p>The error is given by ret = read(sockfd, &amp;buff[count], sizeof(float)*(buffer_size-count)); on client side:
  Thank you for the help</p>