title: echo client-server in C
body: <p>I'm new to socket programming and C as well. I'm creating a simple echo client server application which operate like this
  ex: **if client send "hi" ---- server recv "wellcome"<br>
      if client send "bye"----- server recv "see you"</p>
  
  <p>else if i sent any other string i will recv what i have send**</p>
  
  <p>here is my server :</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;  //strlen
  #include&lt;sys/socket.h&gt;
  #include&lt;arpa/inet.h&gt;   //inet_addr
  #include&lt;unistd.h&gt;  //write
  
  int main(int argc , char *argv[])
  {
     int socket_desc , client_sock , c , read_size;
     struct sockaddr_in server , client;
     char client_message[2000];
     char wellcome[]="wellcome", seeyou[]="see you",hi[]="hi",bye[]="bye";
  
     //Create socket
     socket_desc = socket(AF_INET , SOCK_STREAM , 0);
     if (socket_desc == -1)
     {
         printf("Could not create socket");
     }
     puts("Socket created");
  
     //Prepare the sockaddr_in structure
     server.sin_family = AF_INET;
     server.sin_addr.s_addr = INADDR_ANY;
     server.sin_port = htons( 8882 );
  
     //Bind
     if( bind(socket_desc,(struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
     {
         //print the error message
         printf("bind failed. Error");
         return 1;
     }
     puts("bind done");
  
     //Listen
     listen(socket_desc , 3);
  
     //Accept and incoming connection
     puts("Waiting for incoming connections...");
     c = sizeof(struct sockaddr_in);
  
     //accept connection from an incoming client
     client_sock = accept(socket_desc, (struct sockaddr *)&amp;client, (socklen_t*)&amp;c);
     if (client_sock &lt; 0)
     {
         printf("accept failed");
         return 1;
     }
     puts("Connection accepted");
  
     //Receive a message from client
     while( (read_size = recv(client_sock , client_message , 2000 , 0)) &gt; 0 )
     {
         //Send the message back to client
         *(client_message + read_size) = '\0';
      //*(wellcome + read_size) = '\0';
      //*(seeyou + read_size) = '\0';
      if(strcmp(client_message,hi)==0)
          write(client_sock, wellcome, strlen(wellcome));
      else
          if (strcmp(client_message,bye)==0)
              write(client_sock, seeyou, strlen(seeyou));
  
  
         write(client_sock , client_message , strlen(client_message));
     }
  
     if(read_size == 0)
     {
         puts("Client disconnected");
         fflush(stdout);
     }
     else if(read_size == -1)
     {
         printf("recv failed");
     }
  
     return 0;
  }
  </code></pre>
  
  <p>and the client:</p>
  
  <pre><code>#include&lt;stdio.h&gt;   
  #include&lt;string.h&gt;  
  #include&lt;sys/socket.h&gt;  
  #include&lt;arpa/inet.h&gt;   
  
  int main(int argc , char *argv[])
  {
      int sock;
      struct sockaddr_in server;
      char message[1000] , server_reply[2000];
  
      //Create socket
      sock = socket(AF_INET , SOCK_STREAM , 0);
      if (sock == -1)
      {
          printf("cound'n create socket");
      }
      puts("Socket created");
  
      server.sin_addr.s_addr = inet_addr("127.0.0.1");
      server.sin_family = AF_INET;
      server.sin_port = htons( 8792 );
  
      //Connect to remote server
      if (connect(sock , (struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
      {
          printf("connection error");
          return 1;
      }
  
      puts("Connected\n");
  
      //keep communicating with server
      while(1)
      {
          printf("write your message : ");
          gets(message);
  
          //Send some data
          if( send(sock , message , strlen(message) , 0) &lt; 0)
          {
              puts("send error");
              return 1;
          }
  
          //Receive a reply from the server
      bzero(server_reply,2000);
          if( recv(sock , server_reply , 2000 , 0) &lt; 0)
          {
              puts("recv failed");
              break;
          }
  
          puts("Server reply :");
          puts(server_reply);
      }
  
      close(sock);
      return 0;
  }
  </code></pre>
  
  <p>and i ran it
  server :</p>
  
  <pre><code>[root@localhost chat]# gcc -o newser newser.c
  [root@localhost chat]# ./newser
  Socket created
  bind done
  Waiting for incoming connections...
  Connection accepted
  </code></pre>
  
  <p>and here is the problem with the client :</p>
  
  <pre><code>[root@localhost chat]# ./client 127.0.0.1
  Socket created
  Connected
  
  write your message : hi
  Server reply :
  wellcome
  write your message : bye
  Server reply :
  hi
  write your message : bye
  Server reply :
  see youbye
  write your message : hey buddy
  Server reply :
  see youbye
  write your message : hey ya
  Server reply :
  hey buddy
  write your message : 
  </code></pre>
  
  <p>you can see : <strong>if i say hi, server reply wellcome.
                if i say bye, server reply hi.
                if i say bye gain, server reply see youbye (its't i'm expecting ).</strong></p>