What port should I use?

What port should I use for someone can connect me ?


ServerSocket serverSocket = new ServerSocket(port);
            Socket socket = serverSocket.accept();
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"),true);
            printWriter.println("Connection is established");

Well you should probably avoid port numbers below 1023 but here is the general idea…

Port numbers 0-1023 – Well known ports. These are allocated to server services by the Internet Assigned Numbers Authority (IANA). e.g Web servers normally use port 80 and SMTP servers use port 25 (see diagram above).

Ports 1024-49151- Registered Port -These can be registered for services with the IANA and should be treated as semi-reserved. User written programs should not use these ports.

Ports 49152-65535 – These are used by client programs and you are free to use these in client programs. When a Web browser connects to a web server the browser will allocate itself a port in this range. Also known as ephemeral ports .

Source: http://www.steves-internet-guide.com/tcpip-ports-sockets/

Edit: If you are running a web server or the like, you would naturally use the common port… port 80/443 etc. But for most client apps, follow the guidelines above.

I hope this answers the question. :slight_smile:

2 Likes