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

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

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) —

推荐阅读:
食品营养之葡萄干具有的营养价值  女性养生:女人吃葡萄干有哪些好处?  对转基因食品必须采取最严格保护措施  《食品安全法》修订四大焦点引发关注  秋冬季节风寒感冒 葱白有治疗感冒功效  秋冬时节抗病毒防感冒的食疗养生方  吃香蕉太多也可能伤害到身体的健康  糖类在食物烹饪中能起到什么作用呢?  健康饮食:五种不适宜在深夜吃的食物  养阴益肺润燥止咳之梨的六款食疗方 
评论列表
添加评论