1

I am doing a simple exercise to understand sockets and server sockets. When myServerSocket = new ServerSocket(0) a port is randomly assigned to the socket. I want to know how I can set the client up to connect to that port. How does the client find the listening port if it is randomly assigned?

Here is the snippets of code from both the server and client classes:

Server:

public static void main(String[] args) {

    try {
        // First we create a server socket and bind it to a random port.
        ServerSocket myServerSocket = new ServerSocket(0);


        // wait for an incoming connection... 
        System.out.println("Server is waiting for an incoming connection on host=" 
                + InetAddress.getLocalHost().getCanonicalHostName() 
                + " port=" + myServerSocket.getLocalPort());
        Socket skt = myServerSocket.accept();

Client:

public static void main(String[] args) {

    // create a socket and bind it to the host/port server is listening on.
    String host;
    int port;

    if(args.length==0) {
        host = "localhost";
        port = 9999;         
    } else {
        host = args[0];
        String portStr = args[1];
        try {
            port = Integer.parseInt(portStr);
        } catch (NumberFormatException nfe) {
            System.out.println("Whoops, invalid port number.  Will default to 9999");
            port = 9999;
        }
    }
2
  • 2
    client must know exact port number, where server listens Commented Feb 10, 2015 at 20:43
  • What was said above. Otherwise some kind of port scan, but I am guessing it would take ages to go through all ports, unless some heuristic is used (or the min max [i.e. range] of the random port function is tightened up). Commented Feb 10, 2015 at 20:47

2 Answers 2

2

The client has three options to find the listening port: Port-scanning, user input, or a shared directory.

Port Scanning

The client can attempt to connect to all valid ports on the server; when it successfully communicates with your server program, it can stop attempting other ports.

User Input

Once you start the server, you'll know the port assigned. Just write an interface to type that in on the client side, and the client can continue normally.

Shared Directory

In this case, the server can make a request to a third server telling what port it ended up using. The client then makes a request to that third server to look up the port. This is definitely the most complicated option due to its reliance on another server in the mix.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, on a local network, you can use an UDP to broadcast your availability.
1

The normal procedure for a server is to use a socket port which all clients know about. This is why there are lists of well known ports so that well known servers such as web servers, ftp servers, telnet servers, etc. all have standard ports they use. Clients wanting to connect to those servers use the well known port as a part of the connection request.

However in your case what you might consider doing is for the server to get its port and to then publish the port number it is using.

Or what you may do is instead of using a random port to pick a port which your server will use.

If you do decide to pick a port you should use a port other than one of the well known ports such as provided in this Wikipedia list of well known ports.

Publishing a port number means that you will need some way for clients to discover the port that you are using for your server. There are a number of different approaches to this however they all share the ability for a client to query some known device or server in some known way. You may have some kind of a server that provides a lookup directory of servers so a client would request the port to use from the server for instance Service Name and Transport Lookup Service.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.