title: Why is the Socket provided by ServerSocket using a different port?
body: <p>Server:</p>
  
  <pre><code>int port = 7000
  ServerSocket ss;
  ..
  System.out.println("Listening on " + ss.getInetAddress() + ":"
     + ss.getLocalPort());
  
  Socket s = ss.accept();
  ..
  System.out.println("Accepted connection " + s.getInetAddress() + ":"
     + s.getPort());
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>Socket s;
  ..
  System.out.println("Connected to " + s.getInetAddress() + ":"
     + s.getPort());
  </code></pre>
  
  <p>Server starts listening:</p>
  
  <p><code>Listening on 0.0.0.0/0.0.0.0:7000</code></p>
  
  <p>Client connects:</p>
  
  <p><code>Connected to localhost/127.0.0.1:7000</code></p>
  
  <p>But the server says that the socket is connected on a different port?</p>
  
  <p><code>Accepted connection /127.0.0.1:54682</code></p>
  
  <p>Referencing:</p>
  
  <p><a href="https://stackoverflow.com/questions/17730392/java-the-difference-of-socket-and-serversocket-in-using-port">Java the difference of Socket and ServerSocket in using port</a></p>
  
  <blockquote>
    <p>.. ServerSocket.accept() accepts a connection, and wraps the endpoint in
    a Socket. The endpoint has the same local port number as the
    ServerSocket, by definition as per RFC 793, and therefore so does the
    wrapping Socket.</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>.. each client connection will get a separate Socket to communicate on,
    all communicating using the same server side TCP port.</p>
  </blockquote>