将端口重新绑定到IP上的数据报套接字(Rebinding a port to datagram socket on a difftent IP)

编程入门 行业动态 更新时间:2024-10-25 06:23:29
将端口重新绑定到IP上的数据报套接字(Rebinding a port to datagram socket on a difftent IP)

在我的应用程序中,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 bind

Any 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();

更多推荐

本文发布于:2023-08-07 05:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   端口   数据   IP   Rebinding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!