title: How do I connect to the server socket using the ip address and port number? (client is running on a different machine than server)
body: <p>Client program</p>
  <pre><code>public class client implements Runnable {
  
  protected static String server_IP = &quot;141.117.57.42&quot;;
  private static final int server_Port = 5555 ;
  protected static String client_IP ;
  
  
  public static void main(String[] args) throws IOException{
      final  String host = &quot;localhost&quot;;
      int init = 0  ;
      
      try {
          InetAddress iAddress = InetAddress.getLocalHost();
          client_IP = iAddress.getHostAddress();
          System.out.println(&quot;Current IP address : &quot; +client_IP);
      } catch (UnknownHostException e) {
      }
      
      try {System.out.println(&quot;hello1&quot;);
          Socket socket = new Socket(server_IP,server_Port);
          System.out.println(&quot;hello3&quot;);
          init = initialize(socket);
         
      }catch (SocketException e) {
          System.out.println(&quot;Error: Unable to connect to server port &quot;);
      }
  
      
      if (init ==  0 ){
          System.out.println(&quot;error: Failed to initialize &quot;);
          System.exit(0);
          
      }
      //Thread init_Thread = new Thread();
  }
  private static int initialize(Socket socket ) throws IOException{
      System.out.println(&quot;hello&quot;);
      int rt_value = 0 ;
      
      OutputStream os = socket.getOutputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      PrintWriter pw = new PrintWriter(os, true);
      
      System.out.println(&quot;server: &quot; + br.readLine());
      pw.println(&quot;192.343.34.321&quot;);
     // BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));
      //String userInput = userInputBR.readLine();
      
      //out.println(userInput);
      
      socket.close();
      return rt_value = 1 ;
      
      
      
  }
  public void run(){
      
  }
  }
  </code></pre>
  <p>server side program</p>
  <pre><code>public class server {
  
  protected static String server_IP ;
  
  public static void main(String[] args) throws IOException {
     
      
      int server_Port = 5555 ;
  
  
  
      try {
          InetAddress iAddress = InetAddress.getLocalHost();
          server_IP = iAddress.getHostAddress();
          System.out.println(&quot;Server IP address : &quot; +server_IP);
      } catch (UnknownHostException e) {
      }
      
          ServerSocket serverSocket = new ServerSocket(server_Port);
      
      
          while (true) {
              Socket socket = serverSocket.accept();
              
               OutputStream os = socket.getOutputStream();
              PrintWriter pw = new PrintWriter(os, true);
              InputStreamReader isr = new InputStreamReader(socket.getInputStream());
              pw.println(&quot;Connection confirmed &quot;);
              BufferedReader br = new BufferedReader(isr);
              String str = br.readLine();
              
              pw.println(&quot;your ip address is &quot; + str);
              
              pw.close();
             //socket.close();
  
              //System.out.println(&quot;Just said hello to:&quot; + str);
          }
      
  </code></pre>
  <p>How do I connect to the server socket using the ip address and port number (client is running on a different machine than server).</p>
  <p>When I change the server_IP in client to &quot;local host&quot;, it works perfectly.</p>