What is the correct way to construct InetSocketAddress with any host an IP address?

Viewed 37926

I want to create an InetSocketAddress but I want to do it right no matter if I get a host:port or a ip:port. I see it has two constructors, one for host (String) and another one for IP (InetAddress). Do I have to determine myself if I got an IP or HOST in order to choose between these two constructors? Am I missing something here?

4 Answers

Also it is worth to mention, if you don't know your dns name or ip, you can simple use constructor with port only.

new InetSocketAddress(8080)

it internally invokes InetAddress.anyLocalAddress()

Related