信息: 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