title: Is there a way to implement two way TCP communication using socket programming in c?
body: <p>I have a two code files and I am trying to establish two way TCP communication. I have one way communication ready and working which is sending messages from client to the server and then the server printing it. I have been trying to establish a two way TCP connection, meaning that I can send messages from either machines.</p>
  
  <p>client.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  #define SERVER_PORT 5432
  #define MAX_LINE 256
  
  int main(int argc, char * argv[])
  {
      struct hostent *hp;
      struct sockaddr_in sin;
      char *host;
      char buf[MAX_LINE];
      int s;
      int len, new_s;
  
      if (argc==2) {
          host = argv[1];
      }
      else {
          fprintf(stderr, "usage: simplex-talk host\n");
          exit(1);
      }
  
      /* translate host name into peer’s IP address */
      hp = gethostbyname(host);
      if (!hp) {
          fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
          exit(1);
      }
  
      /* build address data structure */
      bzero((char *)&amp;sin, sizeof(sin));
      sin.sin_family = AF_INET;
      bcopy(hp-&gt;h_addr, (char *)&amp;sin.sin_addr, hp-&gt;h_length);
      sin.sin_port = htons(SERVER_PORT);
  
      /* active open */
      if ((s = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) {
          perror("simplex-talk: socket");
          exit(1);
      }
  
      if (connect(s, (struct sockaddr *)&amp;sin, sizeof(sin)) &lt; 0) {
          perror("simplex-talk: connect");
          close(s);
          exit(1);
      }
  
      /* main loop: get and send lines of text */
      /*while (fgets(buf, sizeof(buf), stdin)) {
          buf[MAX_LINE-1] = '\0';
          len = strlen(buf) + 1;
          send(s, buf, len, 0);
      }*/
  
      while(1){
  
          printf("sent: ");
          if(fgets(buf, sizeof(buf), stdin)){
              buf[MAX_LINE-1] = '\0';
              len = strlen(buf)+1;
              send(s, buf, len, 0);
          }
  
          if((len = recv(new_s, buf, sizeof(buf), 0)) != 0){
              printf("received: ");
              fputs(buf, stdout);
  
          }
      }
  }
  </code></pre>
  
  <p>server.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  #define SERVER_PORT 5432
  #define MAX_PENDING 5
  #define MAX_LINE 256
  
  int main()
  {
      struct sockaddr_in sin;
      char buf[MAX_LINE];
      int len;
      int s, new_s;
  
      /* build address data structure */
      bzero((char *)&amp;sin, sizeof(sin));
      sin.sin_family = AF_INET;
      sin.sin_addr.s_addr = INADDR_ANY;
      sin.sin_port = htons(SERVER_PORT);
  
      /* setup passive open */
      if ((s = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) {
          perror("simplex-talk: socket");
          exit(1);
      }
  
      if ((bind(s, (struct sockaddr *)&amp;sin, sizeof(sin))) &lt; 0) {
          perror("simplex-talk: bind");
          exit(1);
      }
  
      listen(s, MAX_PENDING);
  
      /* wait for connection, then receive and print text */
      while(1) {
          len = sizeof(sin);
          if ((new_s = accept(s, (struct sockaddr *)&amp;sin, (socklen_t *)&amp;len)) &lt; 0) {
              perror("simplex-talk: accept");
              exit(1);
          }
  
  
  
          while(1){
  
              if((len = recv(new_s, buf, sizeof(buf), 0)) != 0){
                  printf("received: ");
                  fputs(buf, stdout);
              }
  
  
              printf("sent: ");
              if(fgets(buf, sizeof(buf), stdin)){
                  buf[MAX_LINE-1] = '\0';
                  len = strlen(buf)+1;
                  send(s, buf, len, 0);
              }
  
          }
  
          close(new_s);
      }
  }
  
  </code></pre>
  
  <p>compilation:
  <code>gcc -o server server.c</code>
  <code>gcc -o client client.c</code></p>
  
  <p>Program execution:
  Client side: <code>./client (ip address of the server machine)</code>
  Server side: <code>./server</code>
  <hr>
  Expected Result:<br/>
  Client side:<br/></p>
  
  <pre><code>sent: hi
  received: hello world
  sent: what is your name?
  </code></pre>
  
  <p>Server side:</p>
  
  <pre><code>received: hi
  sent: hello world
  received: what is your name?
  </code></pre>
  
  <p><br/>
  <hr>
  Actual Result:<br/>
  Client side:<br/></p>
  
  <pre><code>sent: hi
  received: hi
  sent: ^C
  </code></pre>
  
  <p>Server side:</p>
  
  <pre><code>received: hi
  sent: hello world
  </code></pre>