List of usage examples for java.net SocketAddress getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.buaa.cfs.utils.NetUtils.java
/** * Like {@link NetUtils#connect(Socket, SocketAddress, int)} but also takes a local address and port to bind the * socket to.// w w w . j a va2s. c o m * * @param socket * @param endpoint the remote address * @param localAddr the local address to bind the socket to * @param timeout timeout in milliseconds */ public static void connect(Socket socket, SocketAddress endpoint, SocketAddress localAddr, int timeout) throws IOException { if (socket == null || endpoint == null || timeout < 0) { throw new IllegalArgumentException("Illegal argument for connect()"); } SocketChannel ch = socket.getChannel(); if (localAddr != null) { Class localClass = localAddr.getClass(); Class remoteClass = endpoint.getClass(); Preconditions.checkArgument(localClass.equals(remoteClass), "Local address %s must be of same family as remote address %s.", localAddr, endpoint); socket.bind(localAddr); } try { if (ch == null) { // let the default implementation handle it. socket.connect(endpoint, timeout); } else { // SocketIOWithTimeout.connect(ch, endpoint, timeout); } } catch (SocketTimeoutException ste) { // throw new ConnectTimeoutException(ste.getMessage()); } // There is a very rare case allowed by the TCP specification, such that // if we are trying to connect to an endpoint on the local machine, // and we end up choosing an ephemeral port equal to the destination port, // we will actually end up getting connected to ourself (ie any data we // send just comes right back). This is only possible if the target // daemon is down, so we'll treat it like connection refused. if (socket.getLocalPort() == socket.getPort() && socket.getLocalAddress().equals(socket.getInetAddress())) { LOG.info("Detected a loopback TCP socket, disconnecting it"); socket.close(); throw new ConnectException("Localhost targeted connection resulted in a loopback. " + "No daemon is listening on the target port."); } }
From source file:org.apache.sshd.common.util.net.SshdSocketAddress.java
public static SshdSocketAddress toSshdSocketAddress(SocketAddress addr) { if (addr == null) { return null; } else if (addr instanceof SshdSocketAddress) { return (SshdSocketAddress) addr; } else if (addr instanceof InetSocketAddress) { InetSocketAddress isockAddress = (InetSocketAddress) addr; return new SshdSocketAddress(isockAddress.getHostName(), isockAddress.getPort()); } else {/* w ww.j a v a 2 s. c om*/ throw new UnsupportedOperationException("Cannot convert " + addr.getClass().getSimpleName() + "=" + addr + " to " + SshdSocketAddress.class.getSimpleName()); } }