title: Socket Communication using Java and C++ at linux
body: <p>I have implemented an application where I need to use Server socket in Java and Client Socket in C++.They are communicating each other over 127.0.0.1 IP and 4444 port. In our test server(Linux) application are working fine but in production server, socket is neither accepting anything nor client is sending any data.Though the OS are exactly same to each other.</p>
  
  <p>Any idea how I can overcome the same?</p>
  
  <p>Should I've to check security,firewall or anything else ?</p>
  
  <p><strong>Client Socket  C++ (Demo)
  =========================</strong></p>
  
  <pre><code>class tcp_client
  {
  private:
      int sock;
      std::string address;
      int port;
      struct sockaddr_in server;
  
  public:
      tcp_client();
      bool conn();
      bool send_data(string data);
     // string receive(int);
  };
  
  tcp_client::tcp_client()
  {
      sock = -1;
      port = 4444;
      address = "127.0.0.1";
  }
  
  /**
      Connect to a host on a certain port number
  */
  bool tcp_client::conn()
  {
      //create socket if it is not already created
      if(sock == -1)
      {
          //Create socket
          sock = socket(AF_INET , SOCK_STREAM , 0);
          if (sock == -1)
          {
              perror("Could not create socket");
          }
       }
      server.sin_addr.s_addr = inet_addr( address.c_str() );
      server.sin_family = AF_INET;
      server.sin_port = htons( port );
      //Connect to remote server
      if (connect(sock , (struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
      {
          perror("connect failed.## Error");
          return 1;
      }     
      cout&lt;&lt;"Connected\n";
      return true;
  }
  
  /**
      Send data to the connected host
  */
  bool tcp_client::send_data(string data)
  {
      //Send some data
      if( send(sock , data.c_str() , strlen( data.c_str() ) , 0) &lt; 0)
      {
          perror("Send failed : ");
          return false;
      }
      cout&lt;&lt;"Data send\n  "&lt;&lt;sock;
  
      return true;
  }
  
  int main(int argc , char *argv[])
  {
      tcp_client c;
  
          c.conn();  
          c.send_data("Test Class C++ Client \n");   
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Server Socket - Java (DEMO)
  ===========================</strong></p>
  
  <pre><code>public class ServerSocketTest {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) throws IOException {
  
              try {  
                  StringBuffer inputLine = new StringBuffer();
                  ServerSocket serverSocket = new ServerSocket(4444);
  
                  while(true){
  
                      System.out.println("Waiting to receive ----&gt;&gt; ");
                      Socket clientSocket = serverSocket.accept();
                      System.out.println("After receive ----&gt;&gt; ");
                      DataInputStream dis=new DataInputStream(clientSocket.getInputStream());                     
                      String tmp; 
                      while ((tmp = dis.readLine()) != null) {
                          inputLine.append(tmp);
                          System.out.println(tmp);
                      }
                      //use inputLine.toString(); here it would have whole source
                      dis.close();            
                      clientSocket.close();  
                }
  
              }
              catch(Exception e)
              {
                // e.printStackTrace();
              }
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance.</p>