How to Free TCP/UDP Port on Windows Using netstat and taskkill?

  • 时间:2020-10-12 15:39:01
  • 分类:网络文摘
  • 阅读:126 次

If a port has been pocessed by a process/application/program, you cannot listen to it. Usually, an exception of “Address in Use – cannot bind” will be thrown. That is, a port can only be used by one program at a time. If you want to free up the port, you have to figure out which program occupies it.

On windows, you can use command netstat -ano to list the process and the ports.

-a Displays all connections and listening ports.
-n Displays addresses and port numbers in numerical form.
-o Displays the owning process ID associated with each connection.

If you run it, the output looks like this:

# netstat -ano

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1320
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:1801           0.0.0.0:0              LISTENING       5052
  TCP    0.0.0.0:2103           0.0.0.0:0              LISTENING       5052
  TCP    0.0.0.0:2105           0.0.0.0:0              LISTENING       5052
  TCP    0.0.0.0:2107           0.0.0.0:0              LISTENING       5052
  TCP    0.0.0.0:2869           0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:2968           0.0.0.0:0              LISTENING       14048
  TCP    0.0.0.0:5040           0.0.0.0:0              LISTENING       11184
  TCP    0.0.0.0:6646           0.0.0.0:0              LISTENING       85592
  TCP    0.0.0.0:49664          0.0.0.0:0              LISTENING       784
  TCP    0.0.0.0:49665          0.0.0.0:0              LISTENING       1824
  TCP    0.0.0.0:49666          0.0.0.0:0              LISTENING       1920
  TCP    0.0.0.0:49667          0.0.0.0:0              LISTENING       3316
  TCP    0.0.0.0:49668          0.0.0.0:0              LISTENING       4276
  TCP    0.0.0.0:49671          0.0.0.0:0              LISTENING       1068
  TCP    0.0.0.0:49688          0.0.0.0:0              LISTENING       1032
  TCP    0.0.0.0:49707          0.0.0.0:0              LISTENING       5052
  TCP    127.0.0.1:4300         0.0.0.0:0              LISTENING       15296
  TCP    127.0.0.1:4301         0.0.0.0:0              LISTENING       15296
  TCP    127.0.0.1:5354         0.0.0.0:0              LISTENING       4752
  TCP    127.0.0.1:5354         127.0.0.1:49669        ESTABLISHED     4752
  TCP    127.0.0.1:5354         127.0.0.1:49670        ESTABLISHED     4752
  TCP    127.0.0.1:5939         0.0.0.0:0              LISTENING       23360
  TCP    127.0.0.1:5939         127.0.0.1:55573        ESTABLISHED     23360
  TCP    127.0.0.1:6463         0.0.0.0:0              LISTENING       20872
  TCP    127.0.0.1:27015        0.0.0.0:0              LISTENING       4760
  TCP    127.0.0.1:27015        127.0.0.1:49708        ESTABLISHED     4760
  TCP    127.0.0.1:28317        0.0.0.0:0              LISTENING       5152
  TCP    127.0.0.1:49669        127.0.0.1:5354         ESTABLISHED     4760

where we can filter by the grep command (which can be downloaded via GNU Utilities for windows) however, you can use the windows command findstr to achieve the same thing, for example,

# netstat -ano | findstr 49669
STDIN
  TCP    127.0.0.1:5354         127.0.0.1:49669        ESTABLISHED     4752
  TCP    127.0.0.1:49669        127.0.0.1:5354         ESTABLISHED     4760

The last column is the Process ID, where you can now kill the process by using taskkill /f /PID pid where /f means force.

taskkill /f /PID 4752

Of course, we can write a batch script (cmd) that uses for or the GNU-awk to filter out the last column and pipe the commands so that two steps will be merged into one. Using the AWK can be better where you can filter out the second and third column and look for the port numbers as the grep may mistakenly match the PID or the IP address when given the port number string.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
Don’t Fall Victim to the Content Overproduction Trap  Are You Taking Advantage Of These Free WordPress Marketing Plugi  What Content Marketers Can Learn from Game of Thrones  All You Need To Know About How Small Business Should Handle Soci  Should You Add Live Chat to Your Blog?  Food Blogger Accuses Fast Food Giant Of Stealing His Recipe  Back To School 2016: How To Rock (And Profit) With Sales And Mar  The Five Fundamentals to Creating a Powerhouse Blog  Blog On The Go With These Mobile Apps  Here’s Why Your Content is Struggling 
评论列表
添加评论