Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get InetAddress with wildcard ip 0.0.0.0?

Tags:

java

pmd

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?

like image 594
anupsth Avatar asked Dec 04 '12 06:12

anupsth


1 Answers

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.

like image 151
Meckie Avatar answered Sep 28 '22 06:09

Meckie