如何设置在java中code数据报套接字重用地址选项? [英] How to set reuse address option for a datagram socket in java code?

查看:139
本文介绍了如何设置在java中code数据报套接字重用地址选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于如何设置在java中code数据报套接字重用地址选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆