Java Utililty Methods Socket Address Get

List of utility methods to do Socket Address Get

Description

The list of methods to do Socket Address Get are organized into topic(s).

Method

StringinetAddrToHoman(final InetSocketAddress sockAddr)
inet Addr To Homan
return sockAddr.getAddress().getHostAddress() + ":" + sockAddr.getPort();
StringinetSocketAddress2IpPort(InetSocketAddress addr)
Format the InetSocketAddress into IP:Port string.
String s = addr.toString();
int idx = s.indexOf('/') + 1;
return s.substring(idx);
StringIpToString(InetSocketAddress address)
Ip To String
String s = address.getAddress().getHostAddress() + ":" + address.getPort();
return s;
booleanisAddressDefined(InetSocketAddress address)
probe to see if the address
return !(address.getHostName().equals("0.0.0.0"));
booleanisLegalPeerAddress(@CheckForNull SocketAddress socketAddress)
is Legal Peer Address
if (socketAddress == null)
    return false;
if (socketAddress instanceof InetSocketAddress) {
    InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
    InetAddress inetAddress = inetSocketAddress.getAddress();
    if (inetAddress == null)
        return false;
    if (inetAddress.isAnyLocalAddress())
...
booleanisLocalAddress(Socket socket)
Check if a socket is connected to a local address.
InetAddress test = socket.getInetAddress();
if (test.isLoopbackAddress()) {
    return true;
InetAddress localhost = InetAddress.getLocalHost();
String host = localhost.getHostAddress();
for (InetAddress addr : InetAddress.getAllByName(host)) {
    if (test.equals(addr)) {
...
booleanisSameAddress(SocketAddress a1, SocketAddress a2)
Returns true if both SocketAddress es have the same InetAddress
if (isUnresolved(a1) && isUnresolved(a2)) {
    if (getHostName(a1).equals(getHostName(a2))) {
        return true;
return getResolved(a1).getAddress().equals(getResolved(a2).getAddress());
booleanisUdpPortAvailable(InetSocketAddress localAddress)
is Udp Port Available
DatagramSocket ds = null;
try {
    ds = new DatagramSocket(null);
    ds.setReuseAddress(false);
    ds.bind(localAddress);
    ds.close();
    ds = null;
    return true;
...
booleanisUnresolved(SocketAddress address)
Returns true if the given SocketAddress is unresolved.
return ((InetSocketAddress) address).isUnresolved();
booleanisValidMulticastAddress(final InetSocketAddress address)
is Valid Multicast Address
return address != null && address.getAddress() != null && address.getAddress().isMulticastAddress();