记录java.net.BindException: Address already in use: bind及如何查看端口占用

这个问题很常见,解决起来也不难,就是你想要开启的端口被占用了,那么如何排查是哪个进程占用了呢?比如看windows下80端口被哪个程序占用,Linux下80端口被那个程序占用。

java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_65]
    at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_65]
    at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_65]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_65]
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_65]
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:765) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:986) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.StandardService.addConnector(StandardService.java:239) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:194) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:151) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) [spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at com.didispace.Chapter1Application.main(Chapter1Application.java:10) [classes/:na]

windows下查看80端口占用

直接打开cmd,执行netstat -ano | findstr "80",最后一栏是进程ID:

netstat -ano | findstr "80"
  TCP    0.0.0.0:80             0.0.0.0:0              LISTENING       1324
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       2932
  TCP    0.0.0.0:18080          0.0.0.0:0              LISTENING       20836
  TCP    0.0.0.0:20080          0.0.0.0:0              LISTENING       12476
  TCP    10.18.74.12:49580      10.16.133.155:22       ESTABLISHED     17508
  TCP    10.18.74.12:51158      123.58.182.253:80      ESTABLISHED     19244
  TCP    10.18.74.12:51452      123.58.182.253:80      ESTABLISHED     19244
  TCP    10.18.74.12:53706      123.58.182.253:80      ESTABLISHED     19244
  TCP    10.18.74.12:54247      123.58.182.253:80      ESTABLISHED     19244
  TCP    10.18.74.12:54555      42.202.154.56:80       CLOSE_WAIT      8360
  TCP    10.18.74.12:54563      36.110.236.217:80      CLOSE_WAIT      19244
  TCP    10.18.74.12:54568      10.16.13.129:80        TIME_WAIT       0
  TCP    10.18.74.12:54574      10.16.12.90:80         TIME_WAIT       0
  TCP    10.18.74.12:54724      220.181.78.89:80       ESTABLISHED     19244
  TCP    10.18.74.12:54725      10.16.13.129:8088      TIME_WAIT       0
  TCP    10.18.74.12:54726      10.16.13.129:8088      TIME_WAIT       0
  TCP    10.18.74.12:54728      101.226.89.156:80      CLOSE_WAIT      19244
  TCP    10.18.74.12:54733      10.16.13.129:80        ESTABLISHED     1224
  TCP    10.18.74.12:54737      10.16.13.18:8080       ESTABLISHED     5256
  TCP    10.18.74.12:54741      10.16.13.18:8080       CLOSE_WAIT      5256
  TCP    10.18.74.12:54742      10.16.13.18:8080       CLOSE_WAIT      5256
  TCP    10.18.74.12:54744      36.110.170.48:80       ESTABLISHED     19244
  TCP    10.18.74.12:54924      36.110.170.48:80       ESTABLISHED     19244
  TCP    10.18.74.12:54942      220.181.158.203:80     ESTABLISHED     19244
  TCP    10.18.74.12:54943      180.149.131.104:80     ESTABLISHED     5256
  TCP    10.18.74.12:54957      180.149.131.104:443    ESTABLISHED     5256
  TCP    10.18.74.12:54961      180.149.131.104:443    ESTABLISHED     5256
  TCP    10.18.74.12:54969      180.97.104.146:443     ESTABLISHED     5256
  TCP    10.18.74.12:54980      106.38.179.49:443      TIME_WAIT       0
  TCP    10.18.74.12:54983      10.16.13.129:8088      TIME_WAIT       0
  TCP    10.18.74.12:54984      10.16.13.129:8088      ESTABLISHED     1224
  TCP    10.18.74.12:55862      123.58.182.253:80      ESTABLISHED     19244
  TCP    10.18.74.12:58582      10.16.15.110:22        ESTABLISHED     15980
  TCP    10.18.74.12:60013      123.151.77.237:80      ESTABLISHED     8360
  TCP    10.18.74.12:60180      180.163.238.136:443    ESTABLISHED     6068
  TCP    10.18.74.12:60267      10.16.13.129:8000      ESTABLISHED     1224
  TCP    10.18.74.12:60268      101.199.97.175:80      ESTABLISHED     6068
  TCP    10.18.74.12:60802      10.16.133.155:22       ESTABLISHED     19872
  TCP    10.18.74.12:62839      61.155.183.248:8000    CLOSE_WAIT      8268
  TCP    10.18.74.12:62910      10.16.15.123:22        ESTABLISHED     18072
  TCP    127.0.0.1:9080         0.0.0.0:0              LISTENING       13128
  TCP    [::]:80                [::]:0                 LISTENING       1324
  TCP    [::]:8080              [::]:0                 LISTENING       2932
  TCP    [::]:18080             [::]:0                 LISTENING       20836
  TCP    [::]:20080             [::]:0                 LISTENING       12476
  UDP    127.0.0.1:54480        *:*                                    744
  UDP    [fe80::ceb:eb83:7d1c:c622%11]:1900  *:*
    1872
  UDP    [fe80::ceb:eb83:7d1c:c622%11]:2177  *:*
    1872
  UDP    [fe80::ceb:eb83:7d1c:c622%11]:61729  *:*
     1872
  UDP    [fe80::2064:e86b:3b5d:b585%24]:546  *:*
    412
  UDP    [fe80::2064:e86b:3b5d:b585%24]:1900  *:*
     1872
  UDP    [fe80::2064:e86b:3b5d:b585%24]:2177  *:*
     1872
  UDP    [fe80::9c6b:6f7f:980b:e794%26]:546  *:*
    412
  UDP    [fe80::9c6b:6f7f:980b:e794%26]:1900  *:*
     1872
  UDP    [fe80::9c6b:6f7f:980b:e794%26]:2177  *:*
     1872
  UDP    [fe80::9c6b:6f7f:980b:e794%26]:5353  *:*
     1728

可以通过tasklist | findstr 1324 找到是哪个任务在使用

tasklist | findstr 1324
httpd.exe                     1324 Services                   0      4,084 K

taskkill可以终止PID

taskkill /pid 1324 /F
成功: 已终止 PID 为 1324 的进程。

Linux下查看80端口占用

使用netstat查看使用情况

netstat -anlp | grep "80" 
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      27010/nginx         
tcp        0      0 104.128.91.247:33040        119.147.192.232:80          TIME_WAIT   -                   
tcp        0      0 104.128.91.247:59334        192.30.252.153:80           TIME_WAIT   -                   
tcp        0      0 104.128.91.247:33066        119.147.192.232:80          ESTABLISHED 28977/php           
tcp        0      0 104.128.91.247:52152        120.92.102.53:80            TIME_WAIT   -                   
tcp        0      0 104.128.91.247:45424        216.58.193.206:80           TIME_WAIT   -                   
tcp        0      0 104.128.91.247:55166        101.37.21.224:80            TIME_WAIT   -                   
tcp        0      0 104.128.91.247:58314        188.166.214.151:80          TIME_WAIT   -                   
tcp        0      0 104.128.91.247:59240        106.185.31.105:80           TIME_WAIT   -                   
tcp        0      0 104.128.91.247:49682        104.128.91.247:80           TIME_WAIT   -                   
tcp        0      0 104.128.91.247:33494        118.178.215.65:80           TIME_WAIT   -                   
tcp        0      0 104.128.91.247:36536        47.95.47.253:80             TIME_WAIT   -                   
tcp        0      0 104.128.91.247:59324        192.30.252.153:80           TIME_WAIT   -                   
tcp        0      0 104.128.91.247:34184        106.11.61.7:80              TIME_WAIT   -                   
tcp        0      0 104.128.91.247:42526        120.55.250.143:80           TIME_WAIT   -                   
tcp        0      0 104.128.91.247:35170        47.95.47.253:80             TIME_WAIT   -                   
tcp        0      0 104.128.91.247:59328        106.185.31.105:80           TIME_WAIT   -                   
tcp        0      0 104.128.91.247:45050        103.37.167.63:80            TIME_WAIT   -                   
tcp        0      0 104.128.91.247:80           107.182.184.12:46066        TIME_WAIT   -                   
tcp        0      0 104.128.91.247:35250        47.95.47.253:80             TIME_WAIT   -                   
unix  2      [ ACC ]     STREAM     LISTENING     727180 1/init              @/com/ubuntu/upstart

lsof查看文件描述符

lsof -i:80
COMMAND   PID USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
nginx   27010 root    8u  IPv4 1090465385      0t0  TCP *:http (LISTEN)
nginx   27012  web    8u  IPv4 1090465385      0t0  TCP *:http (LISTEN)
nginx   27013  web    8u  IPv4 1090465385      0t0  TCP *:http (LISTEN)
nginx   27014  web    8u  IPv4 1090465385      0t0  TCP *:http (LISTEN)
nginx   27015  web    8u  IPv4 1090465385      0t0  TCP *:http (LISTEN)
0条留言