title: Making multiple processes on same local machine communicate to each other using C sockets
body: <p>I am working on my semester project in which i have to simulate multiple processes on a single computer (number of processes is entered by user) that do some useful computation in a distributed manner and communicate to each other using C sockets. I will use fork() system call to create as many processes as user want to create.</p>
  
  <p>Here in the code I am trying it on two processes. Here a process creates the other process using fork() and then they try to communicate to each other. Each process(P0, P1) will have a server thread and a client thread. P0's clent will send a message to P1's server and P1's client will send a message to P0's server. Here is my code.</p>
  
  <pre><code>int arr_of_ports[3]={7000,7001};
  int P_ID;
  void *server(void *i)
  {
     int sockfd, newsockfd=-1, portno, clilen;
     charenter code here buffer[256];
     struct sockaddr_in serv_addr, cli_addr;    
     int  n;
     sockfd = socket(AF_INET, SOCK_STREAM, 0);                                             
     if(sockfd &lt; 0) 
       {
          perror("ERROR opening socket");                
          pthread_exit(NULL);
       }
     bzero((char *) &amp;serv_addr, sizeof(serv_addr));      
     portno = arr_of_ports[P_ID];                                      
     serv_addr.sin_family = AF_INET;                 
     serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");    
     serv_addr.sin_port = htons(portno);                     
     if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)&lt; 
     0)  
     {                                                                         
        perror("ERROR on binding");
        pthread_exit(NULL);
     }
     listen(sockfd,5);                 
     clilen = sizeof(cli_addr);        
     printf("P%d server waiting for incoming connections \n",P_ID); 
     newsockfd = accept(sockfd, (struct sockaddr *)&amp;cli_addr, (socklen_t 
     *)&amp;clilen);
     printf("server side : connection accepted \n");
     bzero(buffer,256);
     n = read( newsockfd,buffer,255 );
     if (n &lt; 0) 
       {
          perror("ERROR reading from socket");
          pthread_exit(NULL);
       }
     printf("P%d received message: %s\n",P_ID,buffer);
     close(newsockfd);
     pthread_exit(NULL);
  }
  void *client(void *i)
  {
    int *p=(int*)i;
    int to=*p;
    int port_no;
    int client_socket;
    int status=-1;
    char buffer[100]="hello I am ";
    char c=P_ID+'0';
    buffer[11]=c;
    buffer[12]=0;       //to send message like 'hello i am 1' 
    struct sockaddr_in server_addr;
    if((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0)
      {   
        printf("socket not created\n");
        pthread_exit(NULL);
      }
    port_no=arr_of_ports[to];      /* gives the server thread a port 
    number according to the ID of its process*/
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port_no);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    printf("P%d client trying to connect to server \n",P_ID);
    int atmpt=0;
    status = connect(client_socket, (struct sockaddr*)&amp;server_addr, 
    sizeof(server_addr));
    if(status &lt; 0)
      {
        printf("error connecting to the server \n "); 
        pthread_exit(NULL);
      }
    else
      {
        printf("P%d client connected to server",P_ID);
      }
    write(client_socket,buffer,sizeof(buffer));
    close(client_socket);
    pthread_exit(NULL);
  </code></pre>
  
  <p>}</p>
  
  <pre><code>int main()
  {
    pthread_t t[2];
    int flag=1;
    int count=0;
    for(int i=1;i&lt;2;i++)          //creating one more process
    {
    if(flag&gt;=1)
      {
        flag=fork();
        count++;
         }
     else
       {
         break;
       }
   }  
   /*gives the processes an ID so that their server thread can be bound 
  to a port such that the other processes know this server's port*/
  if(flag==0)
    P_ID=count;     
  else
    P_ID=0;
  int rv=pthread_create(&amp;t[0],NULL,server,NULL);  // server thread
  int tc=1;
  for(int i=0;i&lt;2;i++)
  {
    if(i != P_ID)
     {
       int rv=pthread_create(&amp;t[tc],NULL,client,&amp;i);   /*i is the 
     process to which the message is to be sent*/
       tc++;     
     }
  }
  printf("main thread exited \n ");
  pthread_exit(NULL);
  }
  </code></pre>
  
  <p>Although, the code seems to be fine, but the clients never connect to server. Clients does not connect to server and the process does not terminate. Here is the output that I get every time.<a href="https://i.stack.imgur.com/BGqJm.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>If anybody bothers to answer, that will be a great help. Thank you very much in advance.</p>