List of usage examples for java.net InetAddress getByAddress
public static InetAddress getByAddress(String host, byte[] addr) throws UnknownHostException
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] ipAddr = new byte[] { 127, 0, 0, 1 }; InetAddress addr = InetAddress.getByAddress("Localhost", ipAddr); System.out.println(addr.equals(InetAddress.getByAddress(ipAddr))); }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] ipAddr = new byte[] { 127, 0, 0, 1 }; InetAddress addr = InetAddress.getByAddress("Localhost", ipAddr); System.out.println(Arrays.toString(addr.getAddress())); }
From source file:org.restcomm.connect.testsuite.http.util.CustomDnsResolver.java
@Override public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException { if (domainIpMap != null) { if (domainIpMap.containsKey(host)) { final byte[] arrayOfByte = sun.net.util.IPAddressUtil.textToNumericFormatV4(domainIpMap.get(host)); final InetAddress address = InetAddress.getByAddress(host, arrayOfByte); return new InetAddress[] { address }; } else {//from www . java2s .c o m throw new UnknownHostException(); } } else { final byte[] arrayOfByte = sun.net.util.IPAddressUtil.textToNumericFormatV4(LOCALHOST); final InetAddress address = InetAddress.getByAddress(host, arrayOfByte); return new InetAddress[] { address }; } }
From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java
@Override public Socket createSocket(String host, int port) throws IOException { final String resolvedHost = resolver.resolve(host); if (resolvedHost != null && !resolvedHost.equals(host)) { if (InetAddressUtils.isIPv6Address(resolvedHost)) { final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); } else if (InetAddressUtils.isIPv4Address(resolvedHost)) { final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); }// w ww .j ava 2 s.c o m } return defaultFactory.createSocket(host, port); }
From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java
@Override public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { final String resolvedHost = resolver.resolve(host); if (resolvedHost != null && !resolvedHost.equals(host)) { if (InetAddressUtils.isIPv6Address(resolvedHost)) { final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); } else if (InetAddressUtils.isIPv4Address(resolvedHost)) { final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); }//from w w w. j a va 2 s .c o m } return defaultFactory.createSocket(host, port, localHost, localPort); }
From source file:com.cazoodle.crawl.DummySSLProtocolSocketFactory.java
private InetAddress getInetAddress(String host) throws UnknownHostException { byte[] ip = dnsCache.getValue(host); if (ip == null) { LOG.debug(host + ": No IP cached, DNS resolving"); return InetAddress.getByName(host); } else {/* w w w. jav a 2s . c om*/ LOG.debug(host + ": IP cached, skip DNS"); return InetAddress.getByAddress(host, ip); } }
From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java
@Override public Socket createSocket(InetAddress host, int port) throws IOException { final String hostName = host.getHostName(); final String resolvedHost = resolver.resolve(hostName); if (resolvedHost != null && !resolvedHost.equals(hostName)) { if (InetAddressUtils.isIPv6Address(resolvedHost)) { final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(hostName, resolvedAddress), port); } else if (InetAddressUtils.isIPv4Address(resolvedHost)) { final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(hostName, resolvedAddress), port); }// ww w . ja v a 2s .c o m } return defaultFactory.createSocket(host, port); }
From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java
@Override public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { final String hostName = address.getHostName(); final String resolvedHost = resolver.resolve(hostName); if (resolvedHost != null && !resolvedHost.equals(hostName)) { if (InetAddressUtils.isIPv6Address(resolvedHost)) { final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(hostName, resolvedAddress), port, localAddress, localPort);//from w w w. j a va 2 s . c om } else if (InetAddressUtils.isIPv4Address(resolvedHost)) { final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(hostName, resolvedAddress), port, localAddress, localPort); } } return defaultFactory.createSocket(address, port, localAddress, localPort); }
From source file:org.hyperic.hq.bizapp.agent.client.SecureAgentConnection.java
@Override protected Socket getSocket() throws IOException { SSLSocket socket;// w w w . j a va 2 s. co m log.debug("Creating secure socket"); try { // Check for configured agent read timeout from System properties int readTimeout; try { readTimeout = Integer.parseInt(System.getProperty(PROP_READ_TIMEOUT)); } catch (NumberFormatException e) { readTimeout = READ_TIMEOUT; } // Check for configured agent post handshake timeout // from System properties int postHandshakeTimeout; try { postHandshakeTimeout = Integer.parseInt(System.getProperty(PROP_POST_HANDSHAKE_TIMEOUT)); } catch (NumberFormatException e) { postHandshakeTimeout = POST_HANDSHAKE_TIMEOUT; } SSLProvider sslProvider = new DefaultSSLProviderImpl(keystoreConfig, acceptUnverifiedCertificate); SSLSocketFactory factory = sslProvider.getSSLSocketFactory(); // See the following links... // http://www.apache.org/dist/httpcomponents/httpcore/RELEASE_NOTES-4.1.x.txt // http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?message=13695343&cat=10&thread=73546&treeDisplayType=threadmode1&forum=178#13695343 // In any case, it would seem as though the bug has since been fixed in IBM's JRE, no need to work around it anymore... socket = (SSLSocket) factory.createSocket(); // Make sure the InetAddress used to initialize the socket has a non-null hostname (empty string). // This prevents slow and unnecessary reverse DNS querying when the connection is opened. InetAddress withoutHost = InetAddress.getByName(this.agentAddress); InetAddress withHost = InetAddress.getByAddress("", withoutHost.getAddress()); InetSocketAddress address = new InetSocketAddress(withHost, this.agentPort); socket.connect(address, readTimeout); // Set the socket timeout during the initial handshake to detect // connection issues with the agent. socket.setSoTimeout(readTimeout); log.debug("Secure socket is connected to " + address + " - starting handshake."); socket.startHandshake(); log.debug("SSL handshake complete"); // [HHQ-3694] The timeout is set to a post handshake value. socket.setSoTimeout(postHandshakeTimeout); } catch (IOException exc) { IOException toThrow = new IOException( "Unable to connect to " + this.agentAddress + ":" + this.agentPort + ": " + exc.getMessage()); // call initCause instead of constructor to be java 1.5 compat toThrow.initCause(exc); throw toThrow; } // Write our security settings try { DataOutputStream dOs; dOs = new DataOutputStream(socket.getOutputStream()); dOs.writeUTF(this.authToken); } catch (IOException exc) { IOException toThrow = new IOException("Unable to write auth params to server"); // call initCause instead of constructor to be java 1.5 compat toThrow.initCause(exc); throw toThrow; } return socket; }
From source file:org.apache.nifi.processors.standard.util.FTPUtils.java
/** * Creates a new FTPClient connected to an FTP server. The following properties must exist: * <ul>Required Properties://from w w w . ja va 2 s . c o m * <li>remote.host - The hostname or IP address of the FTP server to connect to</li> * <li>remote.user - The username of the account to authenticate with</li> * <li>remote.password = The password for the username to authenticate with</li> * </ul> * <ul>Optional Properties: * <li>remote.port - The port on the FTP server to connect to. Defaults to FTP default.</li> * <li>transfer.mode - The type of transfer for this connection ('ascii', 'binary'). Defaults to 'binary'</li> * <li>connection.mode - The type of FTP connection to make ('active_local', 'passive_local'). Defaults to 'active_local'. In active_local the server initiates 'data connections' to the client * where in passive_local the client initiates 'data connections' to the server.</li> * <li>network.data.timeout - Default is 0. Sets the timeout in milliseconds for waiting to establish a new 'data connection' (not a control connection) when in ACTIVE_LOCAL mode. Also, this * establishes the amount of time to wait on read calls on the data connection in either mode. A value of zero means do not timeout. Users should probably set a value here unless using very * reliable communications links or else risk indefinite hangs that require a restart.</li> * <li>network.socket.timeout - Default is 0. Sets the timeout in milliseconds to use when creating a new control channel socket and also a timeout to set when reading from a control socket. A * value of zero means do not timeout. Users should probably set a value here unless using very reliable communications links or else risk indefinite hangs that require a restart.</li> * </ul> * * @param conf conf * @param monitor if provided will be used to monitor FTP commands processed but may be null * @return FTPClient connected to FTP server as configured * @throws NullPointerException if either argument is null * @throws IllegalArgumentException if a required property is missing * @throws NumberFormatException if any argument that must be an int cannot be converted to int * @throws IOException if some problem occurs connecting to FTP server */ public static FTPClient connect(final FTPConfiguration conf, final ProtocolCommandListener monitor) throws IOException { if (null == conf) { throw new NullPointerException(); } final String portVal = conf.port; final int portNum = (null == portVal) ? -1 : Integer.parseInt(portVal); final String connectionModeVal = conf.connectionMode; final String connectionMode = (null == connectionModeVal) ? ACTIVE_LOCAL_CONNECTION_MODE : connectionModeVal; final String transferModeVal = conf.transferMode; final String transferMode = (null == transferModeVal) ? BINARY_TRANSFER_MODE : transferModeVal; final String networkDataTimeoutVal = conf.dataTimeout; final int networkDataTimeout = (null == networkDataTimeoutVal) ? 0 : Integer.parseInt(networkDataTimeoutVal); final String networkSocketTimeoutVal = conf.connectionTimeout; final int networkSocketTimeout = (null == networkSocketTimeoutVal) ? 0 : Integer.parseInt(networkSocketTimeoutVal); final FTPClient client = new FTPClient(); if (networkDataTimeout > 0) { client.setDataTimeout(networkDataTimeout); } if (networkSocketTimeout > 0) { client.setDefaultTimeout(networkSocketTimeout); } client.setRemoteVerificationEnabled(false); if (null != monitor) { client.addProtocolCommandListener(monitor); } InetAddress inetAddress = null; try { inetAddress = InetAddress.getByAddress(conf.remoteHostname, null); } catch (final UnknownHostException uhe) { } if (inetAddress == null) { inetAddress = InetAddress.getByName(conf.remoteHostname); } if (portNum < 0) { client.connect(inetAddress); } else { client.connect(inetAddress, portNum); } if (networkDataTimeout > 0) { client.setDataTimeout(networkDataTimeout); } if (networkSocketTimeout > 0) { client.setSoTimeout(networkSocketTimeout); } final boolean loggedIn = client.login(conf.username, conf.password); if (!loggedIn) { throw new IOException("Could not login for user '" + conf.username + "'"); } if (connectionMode.equals(ACTIVE_LOCAL_CONNECTION_MODE)) { client.enterLocalActiveMode(); } else if (connectionMode.equals(PASSIVE_LOCAL_CONNECTION_MODE)) { client.enterLocalPassiveMode(); } boolean transferModeSet = false; if (transferMode.equals(ASCII_TRANSFER_MODE)) { transferModeSet = client.setFileType(FTPClient.ASCII_FILE_TYPE); } else { transferModeSet = client.setFileType(FTPClient.BINARY_FILE_TYPE); } if (!transferModeSet) { throw new IOException("Unable to set transfer mode to type " + transferMode); } return client; }