2.0.2 springboot项目集成socketIo实现实时推送功能出现异常 Caused by: java.net.BindException: 地址已在使用


信息: Using a shared selector for servlet write/read
2019-12-15 13:57:42,380 ERROR (SocketIOServer.java:157) - SocketIO server start failed at port: 3703!
2019-12-15 13:57:42,395 ERROR (SpringApplication.java:842) - Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner

.......
Caused by: java.net.BindException: 地址已在使用
at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_212]
at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_212]
at sun.nio.ch.Net.bind(Net.java:425) ~[?:1.8.0_212]
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[?:1.8.0_212]
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:130) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558) ~[netty-transport-

“java.net.BindException: Address already in use”

出现这个问题的是有客户端在使用,项目重启后,socket的端口在短时间内没有完全释放,就会出现上述提示,端口被占用,但是用netstat命令查看又没有

开发作者给的意见:

Some developers don't know how to solve this problem, so I think this is a good idea to set the default value of SocketConfig.reuseAddress to true. It can avoid the BindException.

解决办法:

Configuration configuration = new Configuration();
 SocketConfig socketConfig = configuration.getSocketConfig();
 socketConfig.setReuseAddress(true);
项目使用:
Configuration config = new Configuration();
config.setHostname(host);
config.setPort(port);
SocketConfig socketConfig=config.getSocketConfig();
if(!socketConfig.isReuseAddress()){
    socketConfig.setReuseAddress(true);
    System.out.println("是否绑定了: "+socketConfig.isReuseAddress());
}
config.setSocketConfig(socketConfig);

经验证是有效的,项目重启后再没有端口占用问题

参考连接:
https://blog.csdn.net/zhouzuixin/article/details/26502597

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值