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

InetSocketAddressgetInetSocketAddressFromStringStrict(String s)
get Inet Socket Address From String Strict
String[] tokens = s.split(":");
if (tokens.length < 2) {
    return null;
return new InetSocketAddress(tokens[0], Integer.valueOf(tokens[1]));
ConstructorgetInetSocketTransportAddressConstructor()
get Inet Socket Transport Address Constructor
try {
    Class<?> clazz = Class.forName("org.elasticsearch.common.transport.InetSocketTransportAddress");
    return clazz.getConstructor(InetSocketAddress.class);
} catch (ClassNotFoundException e) {
    return null;
} catch (NoSuchMethodException e) {
    return null;
} catch (SecurityException e) {
...
StringgetIp(SocketAddress socketAddress)
get Ip
if (socketAddress instanceof InetSocketAddress) {
    InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
    String ip = inetSocketAddress.getAddress().toString();
    if (ip.charAt(0) == '/') {
        return ip.substring(1);
    return ip;
throw new IllegalStateException(
        "unknown socketaddress type:" + socketAddress.getClass() + "," + socketAddress);
StringgetIpAddress(Socket socket)
get Ip Address
return socket.getInetAddress().toString().substring(1);
StringgetIpAddress(SocketAddress sa)
get Ip Address
String ip = sa.toString();
if (ip == null)
    return "";
if (ip.startsWith("/"))
    ip = ip.substring(1);
int i = ip.indexOf(":");
if (i > 0)
    ip = ip.substring(0, i);
...
StringgetIpAsString(SocketAddress address)
get Ip As String
try {
    if (address instanceof InetSocketAddress) {
        return ((InetSocketAddress) address).getAddress().getHostAddress();
    return address.toString().split("/")[1].split(":")[0];
} catch (Throwable e) {
    e.printStackTrace();
return "0.0.0.0";
StringgetLocalAddress(Socket socket)
get Local Address
String localAddress = socket == null || socket.getLocalAddress() == null ? ""
        : socket.getLocalAddress().toString() + ":" + socket.getLocalPort();
if (localAddress.startsWith("/")) {
    localAddress = localAddress.substring(1);
return localAddress;
InetSocketAddressgetLocalSocketAddress(String host, int port)
get Local Socket Address
return isInvalidLocalHost(host) ? new InetSocketAddress(port) : new InetSocketAddress(host, port);
intgetNetworkVersion(InetSocketAddress address)
get Network Version
if (address == null) {
    throw new IllegalArgumentException("Address cannot be null");
if (address.getAddress() instanceof Inet4Address) {
    return 4;
} else if (address.getAddress() instanceof Inet6Address) {
    return 6;
throw new UnsupportedOperationException("Unknown network version " + address.getClass().getSimpleName());
InetSocketAddressgetOffsettedAddress(InetSocketAddress isa, int offset)
get Offsetted Address
return offset == 0 ? isa : new InetSocketAddress(isa.getAddress(), isa.getPort() + offset);