3

I'm writing a chat program using sockets and Java and I have to specify a port that the servers listen on.

Here is the the questions I have:

  1. How can I be sure that that port is always free?

  2. Does it matter if another program is also listening on that port?

  3. If the port has to be free and the default port is occupied, how would I notify the clients of a change in port number?

  4. Should I just make it so the server keeps trying to bind to a new port, incrementing the port number until it finds a free port?

Akiner Alkan
  • 6,145
  • 3
  • 32
  • 68
Caders117
  • 319
  • 3
  • 18

4 Answers4

5

How can I be sure that that port is always free?

You can use "netstat" to check whether a port is available or not. You can list all the ports being used by a service via:

netstat -anp

If you want to search for a specific port you can use:

netstat -anp | find "port number", eg netstat -anp | find ":8080".

Does it matter if another program is also listening on that port?

From a traditional look, Yes, for TCP you can only have one application listening on the same port and same local ip address at one time. You may be able to use the same port by having multiple local IP adresses either by using multiple network cards or virtual network interfaces.

However, it seems that using the SO_REUSEPORT socket option you may be able to reuse it, check this for more information.

If the port has to be free and the default port is occupied, how would I notify the clients of a change in port number? Should I just make it so the server keeps trying to bind to a new port, incrementing the port number until it finds a free port?

Personally I would either choose a port that is always free or a small list of ports I knew would be usually free and try one by one.

The general rules of thumb for choosing a port include choosing at least a 4 digit number and avoid anything below 1024. Also, even if the port was already in use by another service, you could re-assign it to listen to another port, its your network after all.

imricardoramos
  • 846
  • 1
  • 7
  • 12
4

Could you please describe the architecture you are trying to build? Where do you run your server and clients?

My suggestions are:

  1. Use a single constant port and configure web-server (e.g. - nginx) to route requests to that port according to the domain. Clients won't have to know ports - domain only. Besides this will allow not to expose all ports to the network - only those that are needed (80, 443).

  2. On startup try to detect a free port (call netstat or just increment the port number until the service is up), store the port number in a local file. Dispatch this local file using web server - e.g. nginx - always 80 or 443 port - as a static data. Clients will get the port number by the static url first and then connect to a port provided.

  3. Use a name service - e.g. - Spring Cloud - run it on a separate server. Discover free port on application startup, register it in naming service. Clients will always request naming service first. Alternatively you can just use a simple balancer (e.g. AWS Elastic Balancer + AWC EC2).

  4. Use Kubernetes - configure a "Service" on a specific port and run any number of your application instances in pods - free ports will be detected automatically.

P.s. You can't run several applications on a single port. Most likely it won't even start.

Azee
  • 1,809
  • 17
  • 23
2

If it's your server the program is running on, they you should decide which port to use and make sure it is available (i.e. not used by another program). To check that, just try to bind() your ServerSocket on that port. If you get an IOException, the port is likely already in use (with Linux, check the output of netstat -anp | grep <port> to check which program is using it).

If you don't want to/cannot use a fixed port, then you can always bind to port 0, which will automatically assign a free port. Then you can get the assigned port with getLocalPort().

However, in order for clients to be able to connect to your server (which is running on a "floating" port), you need to advertise it, e.g. through JNDI (preferred in Java), LDAP, ... (see @Azee answer for examples).

As a side note, beware that TCP options SO_REUSEADDR / SO_REUSEPORT do not have the same meaning, depending on the OS you're using (not to mention their (un)availability).

Matthieu
  • 2,736
  • 4
  • 57
  • 87
1

Register at IANA

You could register your port at IANA. Here is a list of already used ports.

https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt

You might think

hey, I am a single person only! I dont like to threat a international authority!

Keep in mind that:

  1. 90% of all ports are unassigned/reserved.
  2. the website http://mobrien.com/ have registered the port 2031 and the person who registered the port is a private-person and not even a programmer (and already dead today maybe).
Grim
  • 1,938
  • 10
  • 56
  • 123