Java Utililty Methods SocketChannel

List of utility methods to do SocketChannel

Description

The list of methods to do SocketChannel are organized into topic(s).

Method

ServerSocketChannelcreateServerSocketChannel(int aPort, String aHostname)
Creates a ServerSocketChannel
ServerSocketChannel lServer = ServerSocketChannel.open();
lServer.configureBlocking(false);
if (null == aHostname) {
    lServer.socket().bind(new InetSocketAddress(aPort));
} else {
    lServer.socket().bind(new InetSocketAddress(InetAddress.getByName(aHostname), aPort));
return lServer;
...
IOExceptionenhanceExceptionWithAddress(final SocketChannel channel, final IOException e)
Added a socket address to a message of an IOException.
try {
    if (channel == null) {
        return e;
    final SocketAddress socketAddress = channel.socket().getRemoteSocketAddress();
    if (socketAddress == null) {
        return e;
    @SuppressWarnings("ObjectToString")
    final String socketAddressAsString = socketAddress.toString();
    final IOException result = new IOException(
            e.getMessage() == null ? socketAddressAsString : socketAddressAsString + ':' + e.getMessage());
    result.setStackTrace(e.getStackTrace());
    return result;
} catch (final Throwable ignored) {
    return e;
booleanfinishConnect(SocketChannel socketChannel)
finish Connect
boolean result = false;
try {
    if (socketChannel != null) {
        result = socketChannel.finishConnect();
} catch (Exception ex) {
return result;
...
SocketChannelgetConnectedSocketChannel(InetAddress host, int port, int timeout)
This will attempt to connect to the passed host and port.
SocketChannel sChannel = null;
try {
    sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);
    sChannel.connect(new InetSocketAddress(host, port));
    long startConnectTime = System.currentTimeMillis();
    do {
        if (!sChannel.finishConnect()) {
...
InetSocketAddressgetSocketAddress(SocketChannel socketChannel)
get Socket Address
String hostAddress = socketChannel.socket().getInetAddress().getHostAddress();
int port = socketChannel.socket().getPort();
return InetSocketAddress.createUnresolved(hostAddress, port);
StringgetSocketDisplayString(SocketChannel channel)
get Socket Display String
Socket socket = channel.socket();
String identifier = "disconnected";
if (socket != null) {
    InetAddress localAddress = socket.getLocalAddress();
    String localAddressStr = localAddress == null ? "null"
            : localAddress.toString() + ":" + socket.getLocalPort();
    InetAddress remoteAddress = socket.getInetAddress();
    String remoteAddressStr = remoteAddress == null ? "null"
...
SocketChannelopenSocketChannel(SocketAddress sa)
Utility method for working around leak in SocketChannel.open( SocketAddress ) method.
SocketChannel sc = SocketChannel.open();
try {
    sc.connect(sa);
    return sc;
} catch (RuntimeException exc) {
    try {
        sc.close();
    } catch (IOException ioe) {
...
booleanpingWithSocketChannel()
ping With Socket Channel
boolean connected = false;
Socket socket = null;
try {
    SocketChannel socketChannel = SocketChannel
            .open(new InetSocketAddress(IPv6_ONLY_SERVER_IP, IPv6_ONLY_SERVER_PORT));
    socket = socketChannel.socket();
    out.println("Created!");
    connected = true;
...
StringremoteAddress(SocketChannel channel)
remote Address
SocketAddress addr = channel.socket().getRemoteSocketAddress();
String res = String.format("%s", addr);
return res;
voidsendFile(FileChannel fileChannel, SocketChannel socketChannel)
send File
long pos = 0;
long fileSize = fileChannel.size();
long remainingBytes = fileSize;
long transferredBytes = 0;
while ((transferredBytes += fileChannel.transferTo(pos, remainingBytes, socketChannel)) < fileSize) {
    pos += transferredBytes;
    remainingBytes -= transferredBytes;
socketChannel.socket().getOutputStream().flush();