我看过关于这个问题的其他答案,但他们大多声明在绑定(bind)套接字之前调用 setReuseAddress (true)
。但我正在这样做,但仍然不起作用,有人可以帮我解决这个问题吗?
SocketChannel tunnel = SocketChannel.open();
if (!protect(tunnel.socket())){
throw new IllegalStateException("Can not protect tunnel");}
Log.i("TAG", "test");
tunnel.socket().setReuseAddress(true);
tunnel.socket().bind(new InetSocketAddress(localAddress, localPort));
Log.i("TAG", "bind test");
tunnel.connect(new InetSocketAddress(destAddress, destPort));
tunnel.configureBlocking(false);
packet.position(0);
packet.limit(length);
Log.i("TAG", "schreibe: " + tunnel.write(packet));
packet.clear();
最佳答案
除非同一端口上的先前套接字绑定(bind)到除 0.0.0.0 之外的不同 IP 地址,否则它不适用于 TCP。 Windows 一如既往地异常(exception)。
关于java - Android,setReuseAddress 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18827676/