public static String toAddressString(InetSocketAddress address) { return address.getAddress().getHostAddress() + ":" + address.getPort(); }
public String getClientAddress() { if (null == peer.clientAddr) { return ""; } return peer.clientAddr.getHostString() + ":" + peer.clientAddr.getPort(); }
public MysqlConnector(InetSocketAddress address, String username, String password){ String addr = address.getHostString(); int port = address.getPort(); this.address = new InetSocketAddress(addr, port); this.username = username; this.password = password; }
private InetSocketAddress createInetSocketAddress(InetSocketAddress resolved, String host) { byte[] addr = NetUtil.createByteArrayFromIpAddressString(resolved.getAddress().getHostAddress()); try { return new InetSocketAddress(InetAddress.getByAddress(host, addr), resolved.getPort()); } catch (UnknownHostException e) { throw new RuntimeException(e); } }
/** {@inheritDoc} */ @Override public Collection<InetSocketAddress> getExternalAddresses(InetSocketAddress addr) throws IgniteCheckedException { InetSocketAddress inetSockAddr = inetSockAddrMap.get(addr); if (inetSockAddr != null) return Collections.singletonList(inetSockAddr); InetAddress inetAddr = inetAddrMap.get(addr.getAddress()); if (inetAddr != null) return Collections.singletonList(new InetSocketAddress(inetAddr, addr.getPort())); return Collections.emptyList(); }
public static InetSocketAddress computeRemoteAddr(InetSocketAddress remoteAddr, InetSocketAddress osRemoteAddr) { if (osRemoteAddr != null) { if (PlatformDependent.javaVersion() >= 7) { try { // Only try to construct a new InetSocketAddress if we using java >= 7 as getHostString() does not // exists in earlier releases and so the retrieval of the hostname could block the EventLoop if a // reverse lookup would be needed. return new InetSocketAddress(InetAddress.getByAddress(remoteAddr.getHostString(), osRemoteAddr.getAddress().getAddress()), osRemoteAddr.getPort()); } catch (UnknownHostException ignore) { // Should never happen but fallback to osRemoteAddr anyway. } } return osRemoteAddr; } return remoteAddr; } }
private static InetSocketAddress getClientAddress(Map<Long, QuorumServer> quorumPeers, long myid, int clientPort) throws IOException { QuorumServer quorumServer = quorumPeers.get(myid); if (null == quorumServer) { throw new IOException("No QuorumServer correspoding to myid " + myid); } if (null == quorumServer.clientAddr) { return new InetSocketAddress(clientPort); } if (quorumServer.clientAddr.getPort() != clientPort) { throw new IOException("QuorumServer port " + quorumServer.clientAddr.getPort() + " does not match with given port " + clientPort); } return quorumServer.clientAddr; }
public static String formatInetAddr(InetSocketAddress addr) { InetAddress ia = addr.getAddress(); if (ia == null) { return String.format("%s:%s", addr.getHostString(), addr.getPort()); } if (ia instanceof Inet6Address) { return String.format("[%s]:%s", ia.getHostAddress(), addr.getPort()); } else { return String.format("%s:%s", ia.getHostAddress(), addr.getPort()); } } }
@Override public Object getParameter(String name) { if (name.equals(ConnRouteParams.DEFAULT_PROXY)) { Proxy proxy = client.proxy(); if (proxy == null) { return null; } InetSocketAddress address = (InetSocketAddress) proxy.address(); return new HttpHost(address.getHostName(), address.getPort()); } throw new IllegalArgumentException(name); }
@Override public Object run() throws UnknownHostException { final InetSocketAddress resolvedAddress = new InetSocketAddress(InetAddress.getByName(address.getHostName()), address.getPort()); exchange.setDestinationAddress(resolvedAddress); return null; } });
@Override public void serialize(InetSocketAddress value, JsonGenerator jgen, SerializerProvider provider) throws IOException { InetAddress addr = value.getAddress(); String str = addr == null ? value.getHostName() : addr.toString().trim(); int ix = str.indexOf('/'); if (ix >= 0) { if (ix == 0) { // missing host name; use address str = addr instanceof Inet6Address ? "[" + str.substring(1) + "]" // bracket IPv6 addresses with : str.substring(1); } else { // otherwise use name str = str.substring(0, ix); } } jgen.writeString(str + ":" + value.getPort()); }
@Override public MasterSlaveEntry getEntry(InetSocketAddress address) { for (MasterSlaveEntry entry : client2entry.values()) { InetSocketAddress addr = entry.getClient().getAddr(); if (addr.getAddress().equals(address.getAddress()) && addr.getPort() == address.getPort()) { return entry; } } return null; }
/** * Encodes an IP address and port to be included in URL. in particular, this method makes * sure that IPv6 addresses have the proper formatting to be included in URLs. * * @param address The socket address with the IP address and port. * @return The proper URL string encoded IP address and port. */ public static String socketAddressToUrlString(InetSocketAddress address) { if (address.isUnresolved()) { throw new IllegalArgumentException("Address cannot be resolved: " + address.getHostString()); } return ipAddressAndPortToUrlString(address.getAddress(), address.getPort()); }
private InetSocketAddress createInetSocketAddress(InetSocketAddress resolved, String host) { byte[] addr = NetUtil.createByteArrayFromIpAddressString(resolved.getAddress().getHostAddress()); try { return new InetSocketAddress(InetAddress.getByAddress(host, addr), resolved.getPort()); } catch (UnknownHostException e) { throw new RuntimeException(e); } }
public static String toAddressString(InetSocketAddress address) { return address.getAddress().getHostAddress() + ":" + address.getPort(); }
private static InetSocketAddress getMulticastControlAddress(final InetSocketAddress endpointAddress) throws UnknownHostException { final byte[] addressAsBytes = endpointAddress.getAddress().getAddress(); validateDataAddress(addressAsBytes); addressAsBytes[addressAsBytes.length - 1]++; return new InetSocketAddress(getByAddress(addressAsBytes), endpointAddress.getPort()); }
public static InetSocketAddress computeRemoteAddr(InetSocketAddress remoteAddr, InetSocketAddress osRemoteAddr) { if (osRemoteAddr != null) { if (PlatformDependent.javaVersion() >= 7) { try { // Only try to construct a new InetSocketAddress if we using java >= 7 as getHostString() does not // exists in earlier releases and so the retrieval of the hostname could block the EventLoop if a // reverse lookup would be needed. return new InetSocketAddress(InetAddress.getByAddress(remoteAddr.getHostString(), osRemoteAddr.getAddress().getAddress()), osRemoteAddr.getPort()); } catch (UnknownHostException ignore) { // Should never happen but fallback to osRemoteAddr anyway. } } return osRemoteAddr; } return remoteAddr; } }
@Override public void operationComplete(Future<List<InetAddress>> future) throws Exception { if (future.isSuccess()) { List<InetAddress> inetAddresses = future.getNow(); List<InetSocketAddress> socketAddresses = new ArrayList<InetSocketAddress>(inetAddresses.size()); for (InetAddress inetAddress : inetAddresses) { socketAddresses.add(new InetSocketAddress(inetAddress, unresolvedAddress.getPort())); } promise.setSuccess(socketAddresses); } else { promise.setFailure(future.cause()); } } });
private InetSocketAddress resolve(InetSocketAddress address) { try { String curHostString = address.getHostString(); List<InetAddress> resolvedAddresses = new ArrayList<>(Arrays.asList(this.resolver.getAllByName(curHostString))); if (resolvedAddresses.isEmpty()) { return address; } Collections.shuffle(resolvedAddresses); return new InetSocketAddress(resolvedAddresses.get(0), address.getPort()); } catch (UnknownHostException e) { LOG.error("Unable to resolve address: {}", address.toString(), e); return address; } }
private void send(final String name, final String value) { try { String formatted = String.format("%s:%s|g", name, value); byte[] data = formatted.getBytes(StandardCharsets.UTF_8); socket.send(new DatagramPacket(data, data.length, this.address)); } catch (IOException e) { LOG.error("unable to send packet to statsd at '{}:{}'", address.getHostName(), address.getPort()); } }