title: Java socket - client and server IP address
body: <p>I am writing a program with TCP sockets connection between client and server. When the server starts I want to display the IP and port that clients need to use to connect, and when client connects I want the server to show what IP did the client connect from. I am getting confused which command should i use to each of those:</p>
  
  <p><code>getInetAdress()</code></p>
  
  <p><code>getLocalAdress()</code></p>
  
  <p><code>getRemoteSocketAdress()</code></p>
  
  <p>edit</p>
  
  <p>I earlier used <code>int port = 1234</code> and <code>String IP = "localhost"</code> to test and it worked, but I only used it on one PC, so I think <code>localhost</code> will not work if i start server and client on different computers.</p>
  
  <p>This is server side:</p>
  
  <pre><code>int port = 1234;
  
  ...
  
  public void start() {
          keepRunning = true;
          // create socket
          try {
              ServerSocket server = new ServerSocket(port);
              while (keepRunning) {
                  display("Waiting for client connections on "
                          + server.getInetAddress().getLocalHost()
                                  .getHostAddress() + ":" + port);
                  Socket conn = server.accept();
                  if (!keepRunning)
                      break;
  
                  ClientThread t = new ClientThread(conn);
                  cList.add(t);
                  t.start();
  </code></pre>
  
  <p>And this is client: </p>
  
  <pre><code>int port = 1234;
  String IP = "localhost";
  //these variables can be changed from Client GUI before making connection
  
  ...
  
  
  public boolean start() {
      try {
          socket = new Socket(IP, port);
      } catch (Exception e) {
          display("Error connectiong to server:" + e);
          return false;
      }
      try {
          sInput = new ObjectInputStream(socket.getInputStream());
          sOutput = new ObjectOutputStream(socket.getOutputStream());
      } catch (IOException e) {
          display("Exception creating new Input/output Streams: " + e);
          return false;
      }
  </code></pre>
  
  <p>When i start the server,</p>
  
  <p><code>display("Waiting for client connections on " + server.getInetAddress().getLocalHost().getHostAddress() + ":" + port);</code></p>
  
  <p>return this:</p>
  
  <p><code>Waiting for client connections on 192.168.1.104:1234</code></p>
  
  <p>which is kind of what i want, but I still cant get it to show me the port. <code>1234</code> is a fixed value i used, but I want to use <code>ServerSocket server = new ServerSocket(0);</code> to asign port dynamically, then when i start the client i just put in the values that i got from server and connect.</p>
  
  <p>I tried to use <code>server.getLocalPort()</code> in the <code>display</code> line in server and it returned 55410 or something like that, but when i put this port in client to make connection, it doesn't work. I get <code>Error connectiong to server:java.net.ConnectException: Connection refused: connect.</code> from client</p>