如何设置在java中code数据报套接字重用地址选项?如何设置、选项、地址、数据

2023-09-13 23:57:06 作者:落日伴晚风

在我的应用程序将有它总是处于运行状态,将被发送或听一些端口一个线程。

In my application there will be one thread which always be running and will be sending or listening to some port.

该应用程序在后台运行。有时,在创建插座,我发现它之前所用的同一个线程中的端口,是没有得到释放插座关闭()。所以,我想这样的

This application runs in the background. Sometimes while creating the socket, i found that the port which was used by the same thread before, is not getting released on close() of the socket. So i tried like this

        dc = new DatagramSocket(inetAddr);
        dc.setReuseAddress(true);  

的问题是,它没有延伸到第二行也。在第一线本身我收到expcetion BindException,使其:地址已在使用

任何人都可以请帮我该如何处理这个情况。

Can anyone please help me how to handle this is situation.

有没有办法释放端口?

感谢和放大器;问候, SSuman185

Thanks & Regards, SSuman185

推荐答案

使用的MulticastSocket 。不带参数的构造吧。隐式调用setReuseAddress(真)。然后调用bind()。

Use a MulticastSocket. Construct it with no arguments. That implicitly calls setReuseAddress(true). Then call bind().

目前,您所呼叫setReuseAddress()来不及为它做任何好处。

At the moment you are calling setReuseAddress() too late for it to do any good.