在我的应用程序中,hyave创建了一个datagarm套接字并绑定了一个端口,将9999表示为ip 192.168.173.1,现在我想将该端口绑定到一个新的ip,例如192.168.173.2,但我无法做到这一点。
1 DatagramSocket s= new DatagramSocket(port,ip1); 2 s.disconnect(); s.close(); s= new DatagramSocket(port,ip2);但是这给了一个
java,net,BindException :Address already in use : Cannot bind任何见解都会非常有帮助。
In my application I hyave created a datagarm socket and binded a port say 9999 to ip 192.168.173.1 now i want to bind the port to a new ip say 192.168.173.2 but i am not able to do it Steps i followed
1 DatagramSocket s= new DatagramSocket(port,ip1); 2 s.disconnect(); s.close(); s= new DatagramSocket(port,ip2);but this gives a
java,net,BindException :Address already in use : Cannot bindAny insight would be very helpful.
最满意答案
为了在尝试解除绑定和重新绑定时避免异常,您可以将每个创建的套接字设置为可重用。 为此,您必须创建一个未绑定的套接字:
DatagramSocket s = new DatagramSocket(null); s.setReuseAddress(true); s.bind(someSocketAddress);更多信息: http : //docs.oracle.com/javase/7/docs/api/java/net/Socket.html#setReuseAddress(boolean )
PS在使用TCP的情况下,在这种情况下BindException的主要原因的超时期限可能不适用于UDP套接字,但是SO_REUSE应该允许您立即重新绑定。 http://docs.oracle.com/javase/7/docs/api/java/net/DatagramSocket.html#setReuseAddress(boolean )
这里有一些例子:
final int port = 55880;A)不重用,不关闭=地址已被使用
DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress("127.0.0.1", port)); s = new DatagramSocket(null); s.setReuseAddress(true); s.bind(new InetSocketAddress("localhost", port));B)重用,不关闭=不抱怨
DatagramSocket s = new DatagramSocket(null); s.setReuseAddress(true); s.bind(new InetSocketAddress("127.0.0.1", port)); s = new DatagramSocket(null); s.setReuseAddress(true); s.bind(new InetSocketAddress("localhost", port));C)不重用,关闭=没有投诉(仅适用于数据报套接字)
DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress("127.0.0.1", port)); s.close(); s = new DatagramSocket(null); s.bind(new InetSocketAddress("localhost", port)); s.close();To avoid exceptions when trying to unbind and rebind, you would set each created socket as reusable. In order to do so, you MUST create an unbound socket:
DatagramSocket s = new DatagramSocket(null); s.setReuseAddress(true); s.bind(someSocketAddress);More info: http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#setReuseAddress(boolean)
P.S. The timeout period that is the main reason for a BindException under such circumstances when using a TCP may not apply to UDP sockets, but the SO_REUSE should allow you to rebind instantly anyway. http://docs.oracle.com/javase/7/docs/api/java/net/DatagramSocket.html#setReuseAddress(boolean)
Here are a few examples:
final int port = 55880;A) No reuse, no close = Address already in use
DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress("127.0.0.1", port)); s = new DatagramSocket(null); s.setReuseAddress(true); s.bind(new InetSocketAddress("localhost", port));B) Reuse, no close = no complaints
DatagramSocket s = new DatagramSocket(null); s.setReuseAddress(true); s.bind(new InetSocketAddress("127.0.0.1", port)); s = new DatagramSocket(null); s.setReuseAddress(true); s.bind(new InetSocketAddress("localhost", port));C) No reuse, close = no complaints (for datagram sockets only)
DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress("127.0.0.1", port)); s.close(); s = new DatagramSocket(null); s.bind(new InetSocketAddress("localhost", port)); s.close();更多推荐
发布评论