Java API Tutorial - Java Socket.getPort()








Syntax

Socket.getPort() has the following syntax.

public int getPort()

Example

In the following code shows how to use Socket.getPort() method.

import java.net.Socket;
/*w  w  w  .j  av a2s. c  o m*/
public class Main {
  public static void main(String[] args) throws Exception {
    Socket client = new Socket("google.com", 80);

    System.out.println(client.getPort());

    client.close();
  }
}

The code above generates the following result.