List of usage examples for java.net InetSocketAddress getAddress
public final InetAddress getAddress()
From source file:io.servicecomb.serviceregistry.RegistryUtils.java
private static IpPort genPublishIpPort(String schema, IpPort ipPort) { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "") .get();/*from w ww . ja va 2s.c om*/ publicAddressSetting = publicAddressSetting.trim(); if (publicAddressSetting.isEmpty()) { InetSocketAddress socketAddress = ipPort.getSocketAddress(); if (socketAddress.getAddress().isAnyLocalAddress()) { String host = NetUtils.getHostAddress(); LOGGER.warn("address {}, auto select a host address to publish {}:{}, maybe not the correct one", socketAddress, host, socketAddress.getPort()); return new IpPort(host, ipPort.getPort()); } return ipPort; } if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) { publicAddressSetting = NetUtils .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1)) .getHostAddress(); } String publishPortKey = PUBLISH_PORT.replace("{transport_name}", schema); int publishPortSetting = DynamicPropertyFactory.getInstance().getIntProperty(publishPortKey, 0).get(); int publishPort = publishPortSetting == 0 ? ipPort.getPort() : publishPortSetting; return new IpPort(publicAddressSetting, publishPort); }
From source file:com.mirth.connect.server.util.ConnectorUtil.java
public static ConnectionTestResponse testConnection(String host, int port, int timeout, String localAddr, int localPort) throws Exception { Socket socket = null;/*from w ww. j av a2s.c o m*/ InetSocketAddress address = null; InetSocketAddress localAddress = null; try { address = new InetSocketAddress(host, port); if (StringUtils.isBlank(address.getAddress().getHostAddress()) || (address.getPort() < 0) || (address.getPort() > 65534)) { throw new Exception(); } } catch (Exception e) { return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, "Invalid host or port."); } if (localAddr != null) { try { localAddress = new InetSocketAddress(localAddr, localPort); if (StringUtils.isBlank(localAddress.getAddress().getHostAddress()) || (localAddress.getPort() < 0) || (localAddress.getPort() > 65534)) { throw new Exception(); } } catch (Exception e) { return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, "Invalid local host or port."); } } try { socket = new Socket(); if (localAddress != null) { try { socket.bind(localAddress); } catch (Exception e) { return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, "Could not bind to local address: " + localAddress.getAddress().getHostAddress() + ":" + localAddress.getPort()); } } socket.connect(address, timeout); String connectionInfo = socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort() + " -> " + address.getAddress().getHostAddress() + ":" + address.getPort(); return new ConnectionTestResponse(ConnectionTestResponse.Type.SUCCESS, "Successfully connected to host: " + connectionInfo, connectionInfo); } catch (SocketTimeoutException ste) { return new ConnectionTestResponse(ConnectionTestResponse.Type.TIME_OUT, "Timed out connecting to host: " + address.getAddress().getHostAddress() + ":" + address.getPort()); } catch (Exception e) { return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, "Could not connect to host: " + address.getAddress().getHostAddress() + ":" + address.getPort()); } finally { if (socket != null) { socket.close(); } } }
From source file:com.offbynull.portmapper.pcp.PcpDiscovery.java
private static Set<InetAddress> discoverGateways() throws InterruptedException, IOException { final Set<InetAddress> foundGateways = Collections.synchronizedSet(new HashSet<InetAddress>()); Set<InetAddress> potentialGateways = NetworkUtils.getPotentialGatewayAddresses(); // port 5351 DatagramChannel unicastChannel = null; try {/* w w w . j a va 2 s . c om*/ unicastChannel = DatagramChannel.open(); unicastChannel.configureBlocking(false); unicastChannel.socket().bind(new InetSocketAddress(0)); } catch (IOException ioe) { IOUtils.closeQuietly(unicastChannel); throw ioe; } UdpCommunicator communicator = null; try { communicator = new UdpCommunicator(Collections.singletonList(unicastChannel)); communicator.startAsync().awaitRunning(); communicator.addListener(new UdpCommunicatorListener() { @Override public void incomingPacket(InetSocketAddress sourceAddress, DatagramChannel channel, ByteBuffer packet) { foundGateways.add(sourceAddress.getAddress()); } }); ByteBuffer outBuf = ByteBuffer.allocate(1100); MapPcpRequest mpr = new MapPcpRequest(ByteBuffer.allocate(12), 0, 0, 0, InetAddress.getByName("::"), 0L); mpr.dump(outBuf, InetAddress.getByAddress(new byte[4])); // should get back an error for this, but this // should be fine because all we're looking for is a response, not // nessecarily a correct response -- self address being sent is // 0.0.0.0 (IPV4) // // also, we need to pass in MAP because Apple's garbage routers // give back NATPMP responses when you pass in ANNOUNCE outBuf.flip(); for (InetAddress potentialGateway : potentialGateways) { communicator.send(unicastChannel, new InetSocketAddress(potentialGateway, 5351), outBuf.asReadOnlyBuffer()); } Thread.sleep(5000L); } finally { if (communicator != null) { communicator.stopAsync().awaitTerminated(); } } foundGateways.retainAll(potentialGateways); // just incase we get back some unsolicited responses return new HashSet<>(foundGateways); }
From source file:eu.stratosphere.nephele.net.NetUtils.java
/** * Returns InetSocketAddress that a client can use to * connect to the server. Server.getListenerAddress() is not correct when * the server binds to "0.0.0.0". This returns "127.0.0.1:port" when * the getListenerAddress() returns "0.0.0.0:port". * // ww w. j a va2 s.com * @param server * @return socket address that a client can use to connect to the server. */ public static InetSocketAddress getConnectAddress(Server server) { InetSocketAddress addr = server.getListenerAddress(); if (addr.getAddress().getHostAddress().equals("0.0.0.0")) { addr = new InetSocketAddress("127.0.0.1", addr.getPort()); } return addr; }
From source file:org.commoncrawl.util.TaskDataUtils.java
/** used to set task data connection info into job config BEFORE submitting the job to the JobTracker**/ public static final void initializeTaskDataJobConfig(JobConf jobconf, long jobId, InetSocketAddress connectionInfo) { jobconf.set("taskdata.service.connectionInfo", connectionInfo.getAddress().getHostAddress() + ":" + connectionInfo.getPort()); jobconf.set("taskdata.service.jobid", Long.toString(jobId)); }
From source file:org.apache.hadoop.hive.llap.LlapUtil.java
public static String getAmHostNameFromAddress(InetSocketAddress address, Configuration conf) { if (!HiveConf.getBoolVar(conf, HiveConf.ConfVars.LLAP_DAEMON_AM_USE_FQDN)) { return address.getHostName(); }/*from www.ja v a2 s. c om*/ InetAddress ia = address.getAddress(); // getCanonicalHostName would either return FQDN, or an IP. return (ia == null) ? address.getHostName() : ia.getCanonicalHostName(); }
From source file:org.elasticsearch.test.SecuritySingleNodeTestCase.java
private static RestClient createRestClient(Client client, RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback, String protocol) { NodesInfoResponse nodesInfoResponse = client.admin().cluster().prepareNodesInfo().get(); assertFalse(nodesInfoResponse.hasFailures()); assertEquals(nodesInfoResponse.getNodes().size(), 1); NodeInfo node = nodesInfoResponse.getNodes().get(0); assertNotNull(node.getHttp());/*from w w w.j ava2s . c o m*/ TransportAddress publishAddress = node.getHttp().address().publishAddress(); InetSocketAddress address = publishAddress.address(); final HttpHost host = new HttpHost(NetworkAddress.format(address.getAddress()), address.getPort(), protocol); RestClientBuilder builder = RestClient.builder(host); if (httpClientConfigCallback != null) { builder.setHttpClientConfigCallback(httpClientConfigCallback); } return builder.build(); }
From source file:org.apache.hadoop.thriftfs.ThriftUtils.java
/** * Creates a Thrift name node client./*from www . java 2 s. c om*/ * * @param conf the HDFS instance * @return a Thrift name node client. */ public static Namenode.Client createNamenodeClient(Configuration conf) throws Exception { String s = conf.get(NamenodePlugin.THRIFT_ADDRESS_PROPERTY, NamenodePlugin.DEFAULT_THRIFT_ADDRESS); // TODO(todd) use fs.default.name here if set to 0.0.0.0 - but share this with the code in // SecondaryNameNode that does the same InetSocketAddress addr = NetUtils.createSocketAddr(s); // If the NN thrift server is listening on the wildcard address (0.0.0.0), // use the external IP from the NN configuration, but with the port listed // in the thrift config. if (addr.getAddress().isAnyLocalAddress()) { InetSocketAddress nnAddr = NameNode.getAddress(conf); addr = new InetSocketAddress(nnAddr.getAddress(), addr.getPort()); } TTransport t = new TSocket(addr.getHostName(), addr.getPort()); t.open(); TProtocol p = new TBinaryProtocol(t); return new Namenode.Client(p); }
From source file:com.offbynull.portmapper.natpmp.NatPmpDiscovery.java
private static Set<InetAddress> discoverGateways() throws InterruptedException, IOException { final Set<InetAddress> foundGateways = Collections.synchronizedSet(new HashSet<InetAddress>()); Set<InetAddress> potentialGateways = NetworkUtils.getPotentialGatewayAddresses(); // port 5351 DatagramChannel unicastChannel = null; try {/*from w ww.ja va 2 s .c om*/ unicastChannel = DatagramChannel.open(); unicastChannel.configureBlocking(false); unicastChannel.socket().bind(new InetSocketAddress(0)); } catch (IOException ioe) { IOUtils.closeQuietly(unicastChannel); throw ioe; } UdpCommunicator communicator = null; try { communicator = new UdpCommunicator(Collections.singletonList(unicastChannel)); communicator.startAsync().awaitRunning(); communicator.addListener(new UdpCommunicatorListener() { @Override public void incomingPacket(InetSocketAddress sourceAddress, DatagramChannel channel, ByteBuffer packet) { new ExternalAddressNatPmpResponse(packet); // should error out if not valid foundGateways.add(sourceAddress.getAddress()); } }); ByteBuffer outBuf = ByteBuffer.allocate(16); ExternalAddressNatPmpRequest eanpr = new ExternalAddressNatPmpRequest(); eanpr.dump(outBuf); outBuf.flip(); for (InetAddress potentialGateway : potentialGateways) { communicator.send(unicastChannel, new InetSocketAddress(potentialGateway, 5351), outBuf.asReadOnlyBuffer()); } Thread.sleep(5000L); } finally { if (communicator != null) { communicator.stopAsync().awaitTerminated(); } } return new HashSet<>(foundGateways); }
From source file:org.apache.hadoop.hdfs.qjournal.server.JournalNodeJspHelper.java
/** * Returns the address of the host minimizing DNS lookups. * @param addr//w w w . j a v a 2 s.c o m * @return */ private static String getHostAddress(InetSocketAddress addr) { String hostToAppend = ""; if (addr.isUnresolved()) { hostToAppend = addr.getHostName(); } else { hostToAppend = addr.getAddress().getHostAddress(); } return hostToAppend; }