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

StringextractClientAddress(SocketAddress remoteAddress)
extract Client Address
String clientAddress;
if (remoteAddress instanceof InetSocketAddress) {
    clientAddress = ((InetSocketAddress) remoteAddress).getHostName();
} else {
    clientAddress = remoteAddress.toString();
return clientAddress;
StringextractHostAddr(InetSocketAddress address)
extract Host Addr
if (null != address.getAddress())
    return address.getAddress().getHostAddress();
else if (null != address.getHostName()) {
    if (address.getHostName().contains("/")) {
        int index = address.getHostName().indexOf("/");
        return address.getHostName().substring(index + 1);
    } else
        return address.getHostName();
...
Stringformat(final SocketAddress s, final int defaultPort)
Format an address string into host:port or *:port syntax.
if (s instanceof InetSocketAddress) {
    final InetSocketAddress addr = (InetSocketAddress) s;
    if (addr.getPort() == defaultPort) {
        return safeHostname(hostname(addr));
    return format(hostname(addr), addr.getPort());
return s.toString();
...
StringformatInetAddr(InetSocketAddress addr)
format Inet Addr
InetAddress ia = addr.getAddress();
if (ia == null) {
    return String.format("%s:%s", addr.getHostString(), addr.getPort());
if (ia instanceof Inet6Address) {
    return String.format("[%s]:%s", ia.getHostAddress(), addr.getPort());
} else {
    return String.format("%s:%s", ia.getHostAddress(), addr.getPort());
...
StringformatSocketAddress(final InetSocketAddress inetSocketAddress)
Create a string representation of a InetSocketAddress
if (inetSocketAddress == null) {
    throw new NullPointerException("inetSocketAddress");
final StringBuilder builder = new StringBuilder();
final InetAddress inetAddress = inetSocketAddress.getAddress();
if (inetAddress instanceof Inet6Address) {
    builder.append("[");
    builder.append(inetAddress.getHostAddress());
...
StringformatSocketAddress(SocketAddress address)
format Socket Address
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
return inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort();
Stringget2428Address(InetSocketAddress address)
Return the (RFC2428) Address in the format compatible with FTP (RFC2428)
InetAddress servAddr = address.getAddress();
int servPort = address.getPort();
StringBuilder builder = new StringBuilder();
String hostaddress = servAddr.getHostAddress();
builder.append('|');
if (hostaddress.contains(":")) {
    builder.append('2'); 
} else {
...
InetSocketAddressget2428InetSocketAddress(String arg)
Get the (RFC2428) InetSocketAddress corresponding to the FTP format of address (RFC2428)
if (arg == null || arg.length() == 0) {
    return null;
String delim = arg.substring(0, 1);
String[] infos = arg.split(delim);
if (infos.length != 3) {
    return null;
boolean isIPV4 = true;
if (infos[0].equals("1")) {
    isIPV4 = true;
} else if (infos[0].equals("2")) {
    isIPV4 = false;
} else {
    return null;
byte[] address = null;
if (isIPV4) {
    address = new byte[4];
    String[] elements = infos[1].split(".");
    if (elements.length != 4) {
        return null;
    for (int i = 0; i < 4; i++) {
        try {
            address[i] = (byte) Integer.parseInt(elements[i]);
        } catch (NumberFormatException e) {
            return null;
} else {
    address = new byte[16];
    int[] value = new int[8];
    String[] elements = infos[1].split(":");
    if (elements.length != 8) {
        return null;
    for (int i = 0, j = 0; i < 8; i++) {
        if (elements[i] == null || elements[i].length() == 0) {
            value[i] = 0;
        } else {
            try {
                value[i] = Integer.parseInt(elements[i]);
            } catch (NumberFormatException e) {
                return null;
        address[j++] = (byte) (value[i] >> 8);
        address[j++] = (byte) (value[i] & 0xFF);
int port = 0;
try {
    port = Integer.parseInt(infos[2]);
} catch (NumberFormatException e) {
    return null;
InetAddress inetAddress;
try {
    inetAddress = InetAddress.getByAddress(address);
} catch (UnknownHostException e) {
    return null;
return new InetSocketAddress(inetAddress, port);
Stringget_IP_from_SocketAddress(String addr)
Extracts simple IP ("10.0.0.102") from "host/10.0.0.102:54321"
String val[] = addr.split(Pattern.quote("/"));
if ((val == null) || (val.length == 0))
    return null;
String ip_port = val[val.length - 1];
String[] split = ip_port.split(Pattern.quote(":"));
if (split.length == 0)
    return null;
return split[0];
...
StringgetAddress(InetSocketAddress address)
get Address
if (address.getAddress() != null) {
    return address.getAddress().getHostAddress();
} else {
    return address.getHostName();