找出占用你端口的罪魁祸首

做开发的同学经常会碰到一个恼人的问题,那就是启动服务器时会抛出一个XXXX端口被占用的异常(⊙o⊙)…,究竟这个端口被哪个家伙给使用了呢?下面我们花两步来找出来,并kill掉它。

  假设一下,异常报出 1371端口被占用了。

  在windows 环境下面,从开始->运行,输入cmd,进入dos界面,然后输入如下命令并运行。

 
view plaincopy to clipboardprint?

   1. C:\Documents and Settings\qingxu>netstat -aon|findstr "1371" 
   2.   TCP    10.5.35.151:1371       203.208.37.104:80      CLOSE_WAIT      2160 

C:\Documents and Settings\qingxu>netstat -aon|findstr "1371" TCP 10.5.35.151:1371 203.208.37.104:80 CLOSE_WAIT 2160

可以看到,运行命令后出现了一行记录,我们关注的是最后一个字段,对,就是2160,它表示占用这个端口的进程号。


有了进程号之后,继续输入如下命令并运行

view plaincopy to clipboardprint?

   1. C:\Documents and Settings\qingxu>tasklist|findstr "2160" 
   2. Fiddler.exe                 2160 Console                 0    150,648 K 

C:\Documents and Settings\qingxu>tasklist|findstr "2160" Fiddler.exe 2160 Console 0 150,648 K

可以看到,是fiddler.exe这个程序在运行。之后你就可以关闭掉这个程序即解决问题,当然你也可以换端口号。


在linux下键入命令并运行


view plaincopy to clipboardprint?

   1. -bash-2.05b$  sudo netstat -anp|grep '1371' 
   2.  
   3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 
   4. tcp 0 52 218.104.81.152:1371 211.100.39.250:29488 ESTABLISHED 6111/1 

-bash-2.05b$ sudo netstat -anp|grep '1371' Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 52 218.104.81.152:1371 211.100.39.250:29488 ESTABLISHED 6111/1

   最后一个字段即是进程号和应用程序名称,找到并关闭之。

猜你喜欢

转载自cher.iteye.com/blog/933394
今日推荐