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

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

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

推荐阅读:
我们,就这样毕业了(七七鱼发表了日志)  观《太行山上有感》有感作文600字  写人作文新来的语文老师作文400字  雨中送花  我选择了坚强作文  有利于网站SEO的缓存策略是怎样的  网站建设提升用户回头率有哪些技巧要点  做网站有哪些好用的SEO优化技巧  网站的优化技巧,掌握这些对网站排名更好  商城系统常见开发语言及特点分享 
评论列表
添加评论