Windows seems to have a problem with port management, mainly because it tries to simplify port management for the user. This is a good thing for most users but complicates things for developers.Recently my troubles began when I was trying to set up a glassfish server to run on port 80 so that when a user navigates to my test website they wouldn’t see the port being used. The issue was IIS was constantly listening on port 80, and glassfish was failing to start and would display an error message:
java.net.BindException: No free port within range
First issue (user process is using the port)
The port is blocked because a user , aka non-system, process is listening in on and/or using port 80. The way to solve this problem is to work out which process is listening to the port and terminate it by using the windows command line
First you need to workout what process is occupying the port and find its PID (process ID) We do this by starting a windows command prompt and trying to find any strings containing “:80” or “LISTENING” in the netstat command (with additional arguments aon).
netstat -aon | findstr :80 | findstr LISTENING
We should get an output displaying either nothing, meaning the port is not being used by any processes at all, or a five column row where the last column is the process ID:
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 4653 //PID is 4653
The next step is to kill (terminate) the process and free up the port for your desired process. Again in the command line, input the following:
taskkill /f /IM 4653 //where 4653 is the PID, and we see:
SUCCESS: The process with PID 4653 has been terminated.
You can kill the process by using windows’ task manager by finding the process by PID
Start the task manager (Ctrl + Shift + Esc) find the process under the Processes tab, and finally click on the process and select End Process.
Note* To display the PID column in the task manager, first select the “Processes Tab”, then from the top menu select “View”, next select the “Select Column…” option, after the pop-up dialogue appears tick “PID” (Process Identifier) and select “OK”
More to come soon on killing the process if it is a system process!
Second issue (system process is using the port)
The infamous PID 4 using port 80 problem…
Resolving this issue is a-bit trickier as we don’t explicitly have permissions to kill the process. This process (PID : 4) is typically IIS and in-order to stop it we need to edit registry values, but this method will work for all system level processes that cant be stopped with the first solution due to permission restrictions.
First launch the registry editor
(press the windows key then type regedit.exe and start the program)
Next step Navigate to:
Finally change the value of “start” to “4” (default value is 3 symbolising enabled and we are changing it to 4 meaning disabled)
Note* For the changed to take affect, we need to reboot our systems.
After the reboot, there shouldn’t be any processes listening to port 80 and the port will now be free for your webserver or application!