title: Socket communication, Java client C server
body: <p>I am trying to communicate through sockets a Java client and a C server
  All seems to work fine if I try the server using nc on the command line to connect or if I use nc as a server and connect with my Java client, but when I try to connect Java Client and C server it doesn't work.</p>
  
  <p>The client starts the connection, and sends the message, message is received by the server but then server´s response never arrives to client.</p>
  
  <p><strong>Server Code:</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;unistd.h&gt;
  
  int main( int argc, char *argv[] )
  {
      int sockfd, clisockfd, portno;
      char * start = "hello";
      char * end = "bye";
      socklen_t clilen;
      char buffer[256];
      char contentBuffer[255];
      struct sockaddr_in serv_addr, cli_addr;
      int  n;
      //int optval;
  
      /* First call to socket() function */
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd &lt; 0) 
      {
          perror("ERROR opening socket");
          return(1);
      }
  
      /* Initialize socket structure */
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      portno = 5000;
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = INADDR_ANY;
      serv_addr.sin_port = htons(portno);
  
  
      if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0)
      {
          perror("ERROR on binding");
          return(1);
      }
  
      listen(sockfd,5);
      clilen = (socklen_t) sizeof(cli_addr);
  
      clisockfd = accept(sockfd, (struct sockaddr *)&amp;cli_addr, &amp;clilen);
  
      if (clisockfd &lt; 0) 
      {
          perror("ERROR on accept");
          return(1);
      }
  
      while (strcmp(end, contentBuffer) !=0)
      {
          bzero(buffer,256);
          bzero(contentBuffer,255);
          /* If connection is established then start communicating */
          n = read( clisockfd,buffer,255 );
          if (n &lt; 0)
          {
              perror("ERROR reading from socket");
              return(1);
          }
  
          strncpy(contentBuffer,buffer,strlen(buffer) - 1);  
          if (strcmp(start, contentBuffer) ==0)
          {
              printf("command: %s\n",buffer);
              n = write(clisockfd,"Roger that",11);
              if (n &lt; 0)
              {
                  perror("ERROR writing to socket");
                  return(1);
              }
          }
          else 
          {
              printf("Unknown command: %s\n",buffer);
              n = write(clisockfd,"ERRCmd",7);
              if (n &lt; 0)
              {
                  perror("ERROR writing to socket");
                  return(1);
              }           
          }
      }
      close(sockfd);
      return 0;
  
  
  }
  </code></pre>
  
  <p><strong>Client Code</strong>:</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class Cliente {
      public static void main(String[] args) throws IOException {
  
          if (args.length != 2) {
              System.err.println(
                  "Usage: java EchoClient &lt;host name&gt; &lt;port number&gt;");
              System.exit(1);
          }
  
          String hostName = args[0];
          int portNumber = Integer.parseInt(args[1]);
          Socket firstSocket = new Socket(hostName, portNumber);
          PrintWriter out = new PrintWriter(firstSocket.getOutputStream(), true);
          BufferedReader in = new BufferedReader(new InputStreamReader(firstSocket.getInputStream()));
          BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
          String userInput;
          while ((userInput = stdIn.readLine()) != null) 
          {
              out.println(userInput);
              System.out.println("received: " + in.readLine());
          }
          in.close();
          stdIn.close();
          firstSocket.close();
  
      }
  }
  </code></pre>