title: Multithreaded socket programming : sharing variable
body: <p>Hi I want to implement a ring of sockets .For example IP address 127.0.0.1 listens to 127.0.0.3 and this one listens to 127.0.0.2. They are supposed to share some information and pass it such as a number and each time a client receives the data, increments the number and passes it to the next one.</p>
  <p>Example : at the beginning counter = 1</p>
  <p><code>(127.0.0.1)--(send : counter ++)--&gt; (127.0.0.2)</code></p>
  <p><code>(127.0.0.2)--(send : counter ++)--&gt; (127.0.0.3)</code></p>
  <p><code>(127.0.0.3)--(send : counter ++)--&gt; (127.0.0.1)</code></p>
  <pre><code>#define _GNU_SOURCE
  #include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;semaphore.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  #define MESSAGE_LENGTH 256ssssssssssssssssssssssssssssssssssssssssss
  sem_t semaphore;
  
  typedef struct{
      int number;
      char* message;
      char* address;
  }PC_info;
  
  
  
   void* routine_server(void* args){
      PC_info* arg = (PC_info*)args;
      printf(&quot;[%d]\t:MyAddress is %s\t%s\n&quot;,arg-&gt;number,arg-&gt;address,arg-&gt;message);
    }   
  
  void* routine_client(void* args){
      PC_info* arg = (PC_info*)args;
      printf(&quot;[%d]\t:I listen to %s\t%s\n&quot;,arg-&gt;number,arg-&gt;address,arg-&gt;message);
  
    }  
  
  PC_info** allocate_threads_args(int n,const char* address[n],const char* message[n]){
  
      PC_info** PCs_info  = (PC_info**)malloc(n * sizeof(PC_info*));
  
      if(PCs_info == NULL){
          perror(&quot;SYSTEM : cannot allocate so many clients&quot;);
          exit(0);
      }
      for(int i = 0;i &lt; n;i++){
          PCs_info[i] = (PC_info*)malloc(sizeof(PC_info));
          
          PCs_info[i]-&gt;address = (char*)malloc(sizeof(char) * MESSAGE_LENGTH);
          PCs_info[i]-&gt;message = (char*)malloc(sizeof(char) * MESSAGE_LENGTH);
  
          memcpy(PCs_info[i]-&gt;address,address[i],sizeof(char) * MESSAGE_LENGTH);
          memcpy(PCs_info[i]-&gt;message,message[i],sizeof(char) * MESSAGE_LENGTH);
          PCs_info[i]-&gt;number= i+1;
  
      }
      
      return PCs_info;
  }
  
  int main(){
  
      const char* address[3] ={&quot;127.0.0.3&quot;,
                               &quot;127.0.0.1&quot;,
                               &quot;127.0.0.2&quot;};
  
      const char* address_client[3] ={&quot;127.0.0.1&quot;,
                               &quot;127.0.0.2&quot;,
                               &quot;127.0.0.3&quot;};
  
      const char* message[3] = {&quot;client 1...&quot;,
                                &quot;client 2...&quot;,
                                &quot;client 3...&quot;};
      int n = 3;
      PC_info** server_info = allocate_threads_args(n,address,message);
      PC_info** client_info = allocate_threads_args(n,address_client,message);
  
      // for(int i = 0;i &lt; n;i++){
      //     printf(&quot;[%d]\t:\t%s\t%s\n&quot;,clients[i]-&gt;number,clients[i]-&gt;address,clients[i]-&gt;message);
      // }
  
      pthread_t* thread = (pthread_t*)malloc(sizeof(pthread_t)* (n * 2));
  
      for(int i = 0;i &lt; n;i++){
          if(pthread_create(&amp;thread[i],NULL,&amp;routine_server,(void*)server_info[i]) != 0){
              perror(&quot;SYSTEM : cannot create thread\n&quot;);
              exit(1);
          }
  
              if(pthread_create(&amp;thread[n+i],NULL,&amp;routine_client,(void*)client_info[i]) != 0){
              perror(&quot;SYSTEM : cannot create thread\n&quot;);
              exit(1);
          }
          
      }
  
      for(int i = 0;i &lt; 2 * n;i++){
          if(pthread_join(thread[i],NULL) != 0){
              perror(&quot;SYSTEM : cannot join thread\n&quot;);
              exit(2);
          }
      }
  
  return 0;
  </code></pre>
  <p>}</p>