title: Socket programming client server message read write in C
body: <p>I have written a code for client server model. It works fine if I pass value in program but when I tried to do it by passing address. 
  I am making quite a few silly mistakes which i am not able to figure out. I have also tried to make 100 threads using pthreads concept,basic intention was that when a client side pings my server and sends message server echoes it back and it can assign any one of the 100 threads message that client has sent. but how to do this... i am still working on that. </p>
  
  <p>Here is my code for server:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;    
  #include &lt;stdlib.h&gt;    
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt; 
  #include &lt;unistd.h&gt;    
  #include &lt;pthread.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/uio.h&gt;
  #define NTHREADS 100
  
  void *connection_handler(void *);
  pthread_t thread_id[NTHREADS];
  pthread_mutex_t lock;
  int service_count, sockfd,d1;
  struct sockaddr_in server , client;
  
  // Socket create
  int sock_create( )
  {
      sockfd= socket(AF_INET , SOCK_STREAM , 0);
  
      if (sockfd &lt;0)
      {
          printf("Could not create socket");  
          return 1;
      }
      puts("Socket created");
      memset(&amp;server,0,sizeof(server));
      server.sin_family = AF_INET;
      server.sin_addr.s_addr = INADDR_ANY;
      server.sin_port = htons( 2100);
  
  }      
  // Bind
  int sock_bind()
  {
      int b= bind(sockfd,(struct sockaddr *)&amp;server , sizeof(server));
      if (b &lt;0)
      {
          perror("Bind failed. Error");
          return 1;
      }
      puts("Bind");
  
  }
  // Listen
  int sock_listen()
  {
     listen(sockfd , 10);
  }
  //Connection accept
  int sock_accept()
  {
      int s = sizeof(struct sockaddr_in);
      d1= accept(sockfd, (struct sockaddr *)&amp;client, (socklen_t*)&amp;s);
  
      if (d1 &lt; 0)
      {
          perror("accept failed");
          return 1;
      } 
      puts("Connection accepted");
  }
  
  int main(int argc , char *argv[])
  {  int client_sock;
     sock_create();
     sock_bind();
     sock_listen();
     sock_accept();
  
      pthread_attr_t attr;
      int i,j;
      pthread_attr_init(&amp;attr);
      pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE);
      printf("Creating threads\n");
  
      int cli_sock=client_sock;
  
      for (i = 0; i &lt; NTHREADS ; i++)
      {
          pthread_create(&amp;(thread_id[i]), &amp;attr, connection_handler, (void*) &amp;cli_sock);
      }
  
      pthread_attr_destroy(&amp;attr); //Free attribute, wait for the other threads
      for(j=0; j &lt; NTHREADS; j++)
      {
          pthread_join( thread_id[j], NULL);
      }
      pthread_exit(NULL);
      return 0;
  }
  
  void *connection_handler(void *sfd)
  {   
      int sock = d1;
      int read_size=0;
      char *message , client_message[2000];
  
      //Receive msg from client
      while( (read_size = recv(sock , client_message , 2000 , 0)) &gt; 0 )
      {   
          client_message[read_size] = '\0';
          //back to client
          write(sock, client_message , strlen(client_message));
          memset(client_message,'\0',sizeof(client_message));
          memset(client_message, 0, 2000);    
      }
       if(read_size == 0)
      {
          puts("Client disconnected");
          fflush(stdout);
      }
      else if(read_size == -1)
      {
          perror("Recv failed");    
      }
      pthread_mutex_lock(&amp;lock);
      service_count++;
      pthread_mutex_unlock(&amp;lock);
  
      pthread_exit((void*) sfd);
      return 0;
  }
  </code></pre>
  
  <p>my client code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc , char *argv[])
  {
      int sockfd;
      struct sockaddr_in servaddr;
      char msg[1000] , servaddr_reply[2000];
  
  
      if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) &lt;0)
      {
          printf("Could not create socket\n");
          return 1;
      }
      puts("Socket created");
  
      servaddr.sin_family= AF_INET;
      servaddr.sin_port= htons(2100);
      servaddr.sin_addr.s_addr= inet_addr("10.205.28.13");
  
      if (connect(sockfd , (struct sockaddr *)&amp;servaddr , sizeof(servaddr)) &lt;0)
      {
          perror("Connection failed\n");
          return 1;
      }
      puts("Connected");
  
      while(1)
      {
          printf("Enter msg:");
          scanf("%s" , msg);
  
          if( send(sockfd , msg , strlen(msg) , 0) &lt; 0)
          {
              puts("Send failed");
              return 1;
          }
           // server reply
          if( recv(sockfd, servaddr_reply , 2000 , 0) &lt; 0)
          {
              puts("Recv failed");
              break;
          }
          puts("Echo: ");
          puts(servaddr_reply);              
      }
          close (sockfd);
          return 0;
  }
  </code></pre>
  
  <p>now when my client is suppose sending hello server replies hello again if i enter message hi sever echoes back hillo .... cant figure out why?</p>