How to get InetAddress with wildcard ip 0.0.0.0?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 17:08:18

问题


InetAddress doesn't provide any static method or constructor to get InetAddress with wildcard IP 0.0.0.0 . The only way is InetAddress.getByName("0.0.0.0")

But PMD gives AvoidUsingHardCodedIP if we pass hard coded IP. Is there any way to get InetAddress with wildcard IP but without hard coding the IP?


回答1:


I know that this is a little bit old. But i was in the same situation:

InetAddress wildCard = new InetSocketAddress(0).getAddress();

does the trick. The main constructor

public InetSocketAddress(InetAddress addr, int port)

will be called with

InetAddress.anyLocalAddress() 

The port is not needed so i picked 0.



来源:https://stackoverflow.com/questions/13697566/how-to-get-inetaddress-with-wildcard-ip-0-0-0-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!