Digital Integration Support Forums


Product support for software brought to you by Digital Integration

Cannot bind, port already in use

As software developers we come across lots of useful snippets of information and ways of using technology. This forum is a repository of tips that we've found helpful that might also be helpful to anyone scouring the web for answers.

Cannot bind, port already in use

Postby Josh Gibbs » Tue Mar 13, 2012 9:53 am

This tip is about a fun little problem that crops up rarely enough for it to be very difficult to diagnose.

Many programs, iMail included, 'bind' themselves to a particular TCP/IP port number to provide access to their services. Programs that perform standard services such as SMTP, POP, HTTP etc. use well known, and registered port numbers that are reserved for use by the operating system.

Separately from ports registered for program access, every application that needs to make a connection to another TCP/IP service must use a port to receive data back from the service it is connected to. These ports are provided by the operating system at the time of connection and are retrieved from a pool of available port numbers known as the ephemeral TCP ports.

In addition to the well known, registered ports, many applications have custom requirements for their TCP/IP needs and will use ports in the ephemeral range to do their listening. In the case of iMail, it listens on port 46245 for web traffic so that it can co-exist with existing HTTP webservers which may already be using port 80.

As the OS starts up, it will start establishing TCP connections to various services and computers. Each of these connections will be made on one of the ephemeral ports and usually will persist until the OS is restarted. Upon restart, the same connections will be given a completely different set of ports from the pool.

The problem that you may see is your application cannot use the port it has been designed to bind to, because it's been allocated by the OS to another purpose. To see which ports are being used, type 'netstat' at a command line.

To resolve this issue in Windows you can reserve ports for your program to use in the ephemeral range by following these steps (ref:kb812873):


  • Start Registry Editor (Regedit.exe).
  • Locate and then click the following registry subkey:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
  • On the Edit menu, point to New, and then click Multi-string Value.
  • Right-click the new value, click Rename, type ReservedPorts, and then press ENTER.
  • Double-click the ReservedPorts value, type the range of ports that you want to reserve, and then click OK.

    Note You must type the range of ports in the following format:
    xxxx-yyyy
    To specify a single port, use the same value for x and y. For example, to specify port 4000, type 4000-4000.

    Warning If you specify the continuous ports separately and if one port is reserved and not used, the next port is not correctly reserved, and it is used.
  • Click OK.
  • Quit Registry Editor.

To reserve more than one port, or range of ports, you can add additional range sets by simply adding multiple lines to the registry value.

If you are in the middle of the problem with a port that's currently in use, you either need to restart the application that has the port bound, or restart the server so that the reservation of the ports takes place.
Josh Gibbs
Site Admin
 
Posts: 46
Joined: Fri Oct 15, 2010 1:37 pm

Return to Tech tips

Who is online

Users browsing this forum: No registered users and 1 guest

cron