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

InetSocketAddressnormalizeSocketAddr(InetSocketAddress address)
normalize Socket Addr
return new InetSocketAddress(extractHostAddr(address), address.getPort());
String[]parseAddress(SocketAddress address)
parse Address
String addressStr = address.toString();
if (addressStr.startsWith("/")) {
    addressStr = addressStr.replace("/", "");
String[] tmp = addressStr.split(":");
return tmp;
InetSocketAddressparseInetSocketAddress(String address)
Parses InetSocketAddress from a String.
if (address == null) {
    return null;
String[] strArr = address.split(":");
if (strArr.length != 2) {
    throw new IOException("Invalid InetSocketAddress " + address);
return new InetSocketAddress(strArr[0], Integer.parseInt(strArr[1]));
...
InetSocketAddressparseInetSocketAddress(String endPoint)
Parse an InetSocketAddress object the given string.
String hostname = null;
String portString;
int port;
int index = endPoint.indexOf(":");
if (index >= 0) {
    hostname = endPoint.substring(0, index);
    portString = endPoint.substring(index + 1);
} else {
...
InetSocketAddressparseInetSocketAddress(String text)
parse Inet Socket Address
if (text == null || text.isEmpty()) {
    throw new IllegalArgumentException("Address is empty");
final int index = text.lastIndexOf(':');
if (index == -1 || index == text.length() - 1) {
    throw new IllegalArgumentException("Port is not found in: " + text);
if (index == 0) {
...
InetSocketAddressparseSocketAddress(String address)
parse Socket Address
String[] tokens = address.split(":");
if (tokens.length != 2)
    return null;
return parseSocketAddress(tokens[0], tokens[1]);
InetSocketAddressparseSocketAddress(String addressPort, int defaultPort)
parse Socket Address
String address = addressPort;
int port = defaultPort;
int lastColon = (addressPort != null) ? addressPort.lastIndexOf(":") : -1;
if (lastColon >= 0) {
    if (addressPort.indexOf(":") == lastColon) {
        address = addressPort.substring(0, lastColon);
        port = Integer.parseInt(addressPort.substring(lastColon + 1));
    } else { 
...
InetSocketAddressparseSocketAddress(String addrString)
Parses the string and returns a InetSocketAddress.
return parseSocketAddress(addrString, -1);
InetSocketAddressparseSocketAddress(String anAddress)
Parses a String containing a SokectAddress formatted as either:

 <host> ":" <port> "[" <numeric_host> "]:" <port> 

String hostAddress;
String port;
if (anAddress.startsWith("[")) {
    int endBracketAt = anAddress.indexOf(']');
    int portSeparatorAt = anAddress.lastIndexOf(':');
    if (-1 == endBracketAt) {
        throw new IllegalArgumentException("missing final ]");
    if (-1 == portSeparatorAt) {
        throw new IllegalArgumentException("missing port separator");
    if (portSeparatorAt < endBracketAt) {
        throw new IllegalArgumentException("missing port");
    hostAddress = anAddress.substring(1, endBracketAt);
    port = anAddress.substring(portSeparatorAt);
} else {
    int portSeparatorAt = anAddress.lastIndexOf(':');
    if (-1 == portSeparatorAt) {
        throw new IllegalArgumentException("missing port separator");
    hostAddress = anAddress.substring(0, portSeparatorAt);
    port = anAddress.substring(portSeparatorAt + 1);
int portNum = Integer.parseInt(port);
return InetSocketAddress.createUnresolved(hostAddress, portNum);
InetSocketAddressparseSocketAddress(String socketAddress)
parse Socket Address
if (socketAddress != null) {
    int colonIdx = socketAddress.indexOf(':');
    if (colonIdx != -1) {
        String hostName = socketAddress.substring(0, colonIdx);
        String port = socketAddress.substring(colonIdx + 1);
        return new InetSocketAddress(hostName, Integer.parseInt(port));
return null;