Find an available port

Port numbers are used to uniquely identify TCP services running on the same IP address.  Ports are 16-bit numbers, so will be in the range 0-65535.  Sometimes we need to find a unique port number to assign to a TCP service.  To do it, we need to identify any port numbers that are in use, and any port numbers that may be likely to be used, and then pick a port number that is not any of those.  Please note that this does not guarantee the unused port number is always available.  Some service not currently running may attempt to use this port at another time.  If you assign this port to your service, and then later some other service that needs this port fails to start, you may need to repeat the procedure to find another unused port for your service to use.

Step 1 – Talk to your Network Administrator

Some ports may always be blocked by policy at your company, and any port you choose may be blocked just because it is not in use.  Your Network Administrator will be able to determine if there are any ports that should not be used, and if the port selected will need to be unblocked before it can be used.

Step 2 – Think of a port number to use

If you have a specific port number or range of port numbers in mind, it can help you to more quickly determine if the number is in use.  The port number can be arbitrary, but often we like to use something that is close to a number that might be used for a similar purpose.  For example, if the MySQL service will not start because port 3306 is in use, we might decide to try port 3307.

Step 3 – Verify that the port number is not actively in use

You can use the netstat command to locate currently used local ports.  From a 5250 command line:

netstat *cnn

Use F13 and set the sort option to Local Port. Use F14 to display all ports as numbers.  Look at the local ports column.  Any number not present is not in use and is currently available.

                        Work with IPv4 Connection Status                        
                                                             System:   I71SUP2  
 Type options, press Enter.                                                     
   3=Enable debug   4=End   5=Display details   6=Disable debug                 
   8=Display jobs                                                               

      Remote           Remote  Local                                            
 Opt  Address           Port    Port  Idle Time  State                          
      *                     *   2001  +++++++++  Listen                         
      *                     *   2002  +++++++++  Listen                         
      *                     *   2006  +++++++++  Listen                         
      *                     *   2008  +++++++++  Listen                         
      *                     *   2015  +++++++++  Listen                         
      *                     *   2017  +++++++++  Listen                         
      *                     *   2018  +++++++++  Listen                         
      *                     *   3000  +++++++++  Listen                         
      *                     *   3306  000:41:13  Listen                         
      *                     *   4004  +++++++++  Listen                         
      *                     *   4800  +++++++++  Listen                         
      *                     *   4800  +++++++++  Listen                         
                                                                        More... 
 F3=Exit   F5=Refresh   F9=Command line   F11=Display byte counts   F12=Cancel  
 F15=Subset   F20=Work with IPv6 connections   F24=More keys

In the above example, we used F13 to sort by local port, and F14 to show port numbers.  We paged down to see if port 3307 is in use.  Nothing appears between port 3306 and port 4004, so port 3307 is not being used by any active service.

Step 4 – Verify the port is not likely to be used by some other service

Use WRKSRVTBLE to see commonly used port numbers on the IBM i.  This shows all of the ports that have defined names on the IBM i.  The list is sorted by service name, so we need to output to a spool file to search for the desired port.  Port numbers on this list should be avoided when assigning ports for other services.  From a 5250 command line:

WRKSRVTBLE OUTPUT(*PRINT)

                              Display Spooled File                              
 File  . . . . . :   QSYSPRT                          Page/Line   1/1           
 Control . . . . .                                    Columns     1 - 78        
 Find  . . . . . .   3307                                                       
 *...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+... 
                                           Work with Service Table Entries      
 5770SS1 V7R1M0  100423                                                         
  Service . . . . . . . . . . . . . :   as-admin-http                           
  Port  . . . . . . . . . . . . . . :   2001                                    
  Protocol  . . . . . . . . . . . . :   tcp                                     
  Text  . . . . . . . . . . . . . . :   AS400 Admin HTTP server                 
  Aliases:                                                                      
    Alias . . . . . . . . . . . . . :   www-http-admin                          
    Alias . . . . . . . . . . . . . :                                           
    Alias . . . . . . . . . . . . . :                                           
    Alias . . . . . . . . . . . . . :                                           
  Service . . . . . . . . . . . . . :   as-admin-http                           
  Port  . . . . . . . . . . . . . . :   2001                                    
  Protocol  . . . . . . . . . . . . :   udp                                     
  Text  . . . . . . . . . . . . . . :   AS400 Admin HTTP server                 
  Aliases:                                                                      
                                                                        More... 
 F3=Exit   F12=Cancel   F19=Left   F20=Right   F24=More keys                    
 Character string not found in file.

In the above example, we scanned the spool file for ‘3307’ and it was not found, so it is not reserved for a named port on the IBM i.

Wikipedia has a nice summary list of the most commonly used and registered port identifiers.  This list does not show any usage for port 3307.  We can see port 3306 is registered for MySQL.

The Service Name and Transport Protocol Port Number Registry of IANA provides the complete list of registered ports.  This list does show port 3307 is registered for OP Session Proxy.  A Google search for this term does not turn up much that we could find other than that it is the name for this port, so we might go ahead and use it for an IBM i application, or we could go back and try for another port number.

Learn more about ports at the Wikipedia Port (computer networking) page.

Bookmark the permalink.

2 Comments

  1. Great piece! Netstat information is also available in IBM i Navigator,

    • Thanks, Mike. In Navigator, go to Network -> TCP/IP Configuration -> IPv4 -> Connections. To sort by Local Port, click the heading tab over the Local Port column.

Leave a Reply