socket客户端连接问题?

  • 项目使用的是Spring-boot

  • 服务器使用的是Centos7,项目部署在Docker中

问题:现在我在项目中使用了socket,设置端口号8089,现在客户端无法连接上

/root$ netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2400/master         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      5459/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2400/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1709/docker-proxy   
tcp6       0      0 :::6379                 :::*                    LISTEN      1587/docker-proxy   
tcp6       0      0 :::8080                 :::*                    LISTEN      23461/docker-proxy  
tcp6       0      0 :::80                   :::*                    LISTEN      15808/docker-proxy  
tcp6       0      0 :::22                   :::*                    LISTEN      5459/sshd 

按照网上的做法开启端口号:

/root$ firewall-cmd --zone=public --add-port=8089/tcp --permanent
success
/root$ firewall-cmd --reload
success

还是不能连接,请问是什么原因?


端口已经打开了,并且docker中做了端口映射,但是客户端还是无法连接...??

/$ netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1411/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2351/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      11457/docker-proxy  
tcp6       0      0 :::6379                 :::*                    LISTEN      11680/docker-proxy  
tcp6       0      0 :::8080                 :::*                    LISTEN      14386/docker-proxy  
tcp6       0      0 :::80                   :::*                    LISTEN      11748/docker-proxy  
tcp6       0      0 :::22                   :::*                    LISTEN      1411/sshd           
tcp6       0      0 :::8089                 :::*                    LISTEN      14377/docker-proxy  
tcp6       0      0 ::1:25                  :::*                    LISTEN      2351/master

0.0.0.0:8080->8080/tcp, 0.0.0.0:8089->8089/tcp

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at socketTest.Test.main(Test.java:15)
阅读 5.6k
3 个回答

经过检查,是防火墙的问题!

你启动docker image的时候,有没有使用-p参数,做端口映射

1、能不能ping通
2、关闭防火墙
3、开启ICMP协议

图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏