List of utility methods to do SocketChannel
ServerSocketChannel | createServerSocketChannel(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; ... |
IOException | enhanceExceptionWithAddress(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; |
boolean | finishConnect(SocketChannel socketChannel) finish Connect boolean result = false; try { if (socketChannel != null) { result = socketChannel.finishConnect(); } catch (Exception ex) { return result; ... |
SocketChannel | getConnectedSocketChannel(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()) { ... |
InetSocketAddress | getSocketAddress(SocketChannel socketChannel) get Socket Address String hostAddress = socketChannel.socket().getInetAddress().getHostAddress(); int port = socketChannel.socket().getPort(); return InetSocketAddress.createUnresolved(hostAddress, port); |
String | getSocketDisplayString(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" ... |
SocketChannel | openSocketChannel(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) { ... |
boolean | pingWithSocketChannel() 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; ... |
String | remoteAddress(SocketChannel channel) remote Address SocketAddress addr = channel.socket().getRemoteSocketAddress(); String res = String.format("%s", addr); return res; |
void | sendFile(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(); |