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

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

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

推荐阅读:
圆锥形木块沿着它的高切开  通过修改版本号屏蔽WordPress主程序及插件更新提示的方法  WordPress定时发布功能 网站SEO优化的好帮手  robots.txt的格式、写法及其对于WordPress的seo作用  无需修改代码 轻松隐藏WordPress管理工具栏  修改WordPress标签云字体大小及标签显示数量的方法  巧用wordpress更新服务 提升搜索引擎收录文章的速度  无需登陆FTP 新建一个wordpress主题文件  wordpress响应式杂志主题—Semicolon  禁止wordpress仪表盘加载谷歌字体链接 
评论列表
添加评论