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

intcompareAddresses(InetSocketAddress addr1, InetSocketAddress addr2)
compare Addresses
byte[] addr1Array = addr1.getAddress().getAddress();
byte[] addr2Array = addr2.getAddress().getAddress();
for (int i = 0; i < addr1Array.length && i < addr2Array.length; i++)
    if (addr1Array[i] < addr2Array[i])
        return -1;
int port1 = addr1.getPort();
int port2 = addr2.getPort();
if (port1 < port2)
...
StringconvertToHostString(SocketAddress hostAddress)
convert To Host String
final StringBuilder host = new StringBuilder();
final InetSocketAddress inetSocketAddr = (InetSocketAddress) hostAddress;
final InetAddress addr = inetSocketAddr.getAddress();
if (addr instanceof Inet6Address) {
    host.append('[');
    host.append(addr.getHostAddress());
    host.append(']');
} else if (addr != null) {
...
SocketAddresscreate(SocketAddress address, SocketAddress port)
Creates and returns a SocketAddress .
if (isUnresolved(address)) {
    return createUnresolved(getHostName(address), getPort(port));
} else {
    return createResolved(getAddress(address), getPort(port));
ServerSocketcreateBoundServerSocket(final SocketAddress socketAddress)
create Bound Server Socket
final ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(socketAddress);
return serverSocket;
SocketcreateClientSocket(InetSocketAddress site)
create Client Socket
Socket socket = new Socket();
socket.setTcpNoDelay(true);
socket.setReceiveBufferSize(RECV_BUF_SIZE);
socket.setSendBufferSize(SEND_BUF_SIZE);
socket.bind(null);
socket.connect(site);
return socket;
ArrayListcreateHttpFields(InetSocketAddress endpoint)
create Http Fields
ArrayList<String> fields = new ArrayList<String>();
fields.add("Connection: Upgrade");
return fields;
InetSocketAddresscreateInetSocketAddress(String address)

Creates an InetSocketAddress given a host and optional port in a single String

This allows either IP4 or IP6 addresses (including port) to be provided as Strings as per rfc2732

return createInetSocketAddress(address, 0);
InetSocketAddresscreateInetSocketAddress(String ip, int port)
create Inet Socket Address
InetSocketAddress address = null;
if (ip == null) {
    address = new InetSocketAddress(port);
} else {
    address = new InetSocketAddress(ip, port);
return address;
Stringdescribe(InetSocketAddress localAddress)
describe
return String.format("local.address == %s", localAddress);
InetSocketAddressencodedStringToInetSocketAddress(String str)
encoded String To Inet Socket Address
byte[] buf = str.getBytes(CHARSET);
int port = (int) (buf[buf.length - 2] << 8) + (buf[buf.length - 1] & 255);
return new InetSocketAddress(InetAddress.getByAddress(Arrays.copyOfRange(buf, 0, 4)), port);