本文整理汇总了Java中java.net.InetAddress.getAllByName方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddress.getAllByName方法的具体用法?Java InetAddress.getAllByName怎么用?Java InetAddress.getAllByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.InetAddress
的用法示例。
在下文中一共展示了InetAddress.getAllByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getServer
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getServer() {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ArrayList<String> endPoints = new ArrayList<String>();
try {
Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
String hostname = InetAddress.getLocalHost().getHostName();
InetAddress[] addresses = InetAddress.getAllByName(hostname);
for (Iterator<ObjectName> i = objs.iterator(); i.hasNext(); ) {
ObjectName obj = i.next();
String scheme = mbs.getAttribute(obj, "scheme").toString();
String port = obj.getKeyProperty("port");
for (InetAddress addr : addresses) {
String host = addr.getHostAddress();
String ep = scheme + "://" + host + ":" + port;
endPoints.add(ep);
}
}
} catch (Exception e) {
return "";
}
if (endPoints.size() > 0) {
return endPoints.get(0);
} else {
return "";
}
}
示例2: HostAddresses
import java.net.InetAddress; //导入方法依赖的package包/类
public HostAddresses(PrincipalName serverPrincipal)
throws UnknownHostException, KrbException {
String[] components = serverPrincipal.getNameStrings();
if (serverPrincipal.getNameType() != PrincipalName.KRB_NT_SRV_HST ||
components.length < 2)
throw new KrbException(Krb5.KRB_ERR_GENERIC, "Bad name");
String host = components[1];
InetAddress addr[] = InetAddress.getAllByName(host);
HostAddress hAddrs[] = new HostAddress[addr.length];
for (int i = 0; i < addr.length; i++) {
hAddrs[i] = new HostAddress(addr[i]);
}
addresses = hAddrs;
}
示例3: getAllLocalHostIP
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* 获得本地所有的IP地址
*/
public static String[] getAllLocalHostIP() {
String[] ret = null;
try {
/** 获得主机名 */
String hostName = getLocalHostName();
if (hostName.length() > 0) {
/** 在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。 */
InetAddress[] addrs = InetAddress.getAllByName(hostName);
if (addrs.length > 0) {
ret = new String[addrs.length];
for (int i = 0; i < addrs.length; i++) {
/** .getHostAddress() 返回 IP 地址字符串(以文本表现形式)。 */
ret[i] = addrs[i].getHostAddress();
}
}
}
} catch (Exception ex) {
ret = null;
}
return ret;
}
示例4: findIp
import java.net.InetAddress; //导入方法依赖的package包/类
private String findIp() {
try {
InetAddress a[] = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
final StringBuilder buff = new StringBuilder();
for (int i = 0; i < a.length; ++i) {
buff.append(a[i].getHostAddress());
if (i < a.length - 1) {
buff.append(","); //$NON-NLS-1$
}
}
return buff.toString();
}
// FIXME: review error message
catch (UnknownHostException e) {
return null;
}
}
示例5: selectAddress
import java.net.InetAddress; //导入方法依赖的package包/类
@Override
public Optional<Address> selectAddress(Optional<String> addressSelectionContext)
{
checkArgument(!addressSelectionContext.isPresent(), "addressSelectionContext should not be set");
List<HostAndPort> result = new ArrayList<>();
for (HostAndPort address : addresses) {
try {
for (InetAddress ip : InetAddress.getAllByName(address.getHost())) {
result.add(HostAndPort.fromParts(ip.getHostAddress(), address.getPort()));
}
}
catch (UnknownHostException ignored) {
}
}
if (result.isEmpty()) {
return Optional.empty();
}
HostAndPort hostAndPort = result.get(ThreadLocalRandom.current().nextInt(result.size()));
return Optional.of(() -> hostAndPort);
}
示例6: getProxyAddresses
import java.net.InetAddress; //导入方法依赖的package包/类
protected Set<String> getProxyAddresses() throws ServletException {
long now = System.currentTimeMillis();
synchronized(this) {
if(proxyAddresses == null || (lastUpdate + updateInterval) >= now) {
proxyAddresses = new HashSet<>();
for (String proxyHost : proxyHosts) {
try {
for(InetAddress add : InetAddress.getAllByName(proxyHost)) {
if (LOG.isDebugEnabled()) {
LOG.debug("proxy address is: {}", add.getHostAddress());
}
proxyAddresses.add(add.getHostAddress());
}
lastUpdate = now;
} catch (UnknownHostException e) {
LOG.warn("Could not locate {} - skipping", proxyHost, e);
}
}
if (proxyAddresses.isEmpty()) {
throw new ServletException("Could not locate any of the proxy hosts");
}
}
return proxyAddresses;
}
}
示例7: connect
import java.net.InetAddress; //导入方法依赖的package包/类
private Socket connect() throws GeneralSecurityException, MessagingException, IOException {
Exception connectException = null;
InetAddress[] inetAddresses = InetAddress.getAllByName(settings.getHost());
for (InetAddress address : inetAddresses) {
try {
return connectToAddress(address);
} catch (IOException e) {
Timber.w(e, "Could not connect to %s", address);
connectException = e;
}
}
throw new MessagingException("Cannot connect to host", connectException);
}
示例8: ensurePreferIPv4
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Checks whether we are running with java.net.preferIPv4Stack=true
*/
public void ensurePreferIPv4() throws IOException {
InetAddress[] addrs = InetAddress.getAllByName("localhost");
for (InetAddress addr : addrs) {
LOG.info("resolved localhost as:" + addr);
Assert.assertEquals(4, addr.getAddress().length); //ensure 4 byte ipv4 address
}
}
示例9: StaticHostProvider
import java.net.InetAddress; //导入方法依赖的package包/类
/**
* Constructs a SimpleHostSet.
*
* @param serverAddresses
* possibly unresolved ZooKeeper server addresses
* @throws UnknownHostException
* @throws IllegalArgumentException
* if serverAddresses is empty or resolves to an empty list
*/
public StaticHostProvider(Collection<InetSocketAddress> serverAddresses)
throws UnknownHostException {
for (InetSocketAddress address : serverAddresses) {
InetAddress ia = address.getAddress();
InetAddress resolvedAddresses[] = InetAddress.getAllByName((ia!=null) ? ia.getHostAddress():
address.getHostName());
for (InetAddress resolvedAddress : resolvedAddresses) {
// If hostName is null but the address is not, we can tell that
// the hostName is an literal IP address. Then we can set the host string as the hostname
// safely to avoid reverse DNS lookup.
// As far as i know, the only way to check if the hostName is null is use toString().
// Both the two implementations of InetAddress are final class, so we can trust the return value of
// the toString() method.
if (resolvedAddress.toString().startsWith("/")
&& resolvedAddress.getAddress() != null) {
this.serverAddresses.add(
new InetSocketAddress(InetAddress.getByAddress(
address.getHostName(),
resolvedAddress.getAddress()),
address.getPort()));
} else {
this.serverAddresses.add(new InetSocketAddress(resolvedAddress.getHostAddress(), address.getPort()));
}
}
}
if (this.serverAddresses.isEmpty()) {
throw new IllegalArgumentException(
"A HostProvider may not be empty!");
}
Collections.shuffle(this.serverAddresses);
}
示例10: getIP
import java.net.InetAddress; //导入方法依赖的package包/类
public static String getIP() throws UnknownHostException{
InetAddress localaddr = InetAddress.getLocalHost () ;
InetAddress[] localaddrs = InetAddress.getAllByName(localaddr.getHostName () ) ;
for ( int i=0 ; i<localaddrs.length ; i++ ) {
if ( ! localaddrs[ i ].equals( localaddr ) ){
String host=localaddrs[i].getHostAddress ();
return host;
}
}
return null;
}
示例11: getLocalIP
import java.net.InetAddress; //导入方法依赖的package包/类
private String getLocalIP() {
try {
String hostName = InetAddress.getLocalHost().getHostName();
InetAddress[] addresses = InetAddress.getAllByName(hostName);
String ip = "";
for (InetAddress address : addresses) {
ip += address.getHostAddress() + ",";
}
return ip.endsWith(",") ? ip.substring(0, ip.length() - 1) : ip;
} catch (UnknownHostException e) {
return null;
}
}
示例12: getAddresses
import java.net.InetAddress; //导入方法依赖的package包/类
public static String[] getAddresses(String hostName) {
try {
InetAddress[] ret = InetAddress.getAllByName(hostName);
String[] strArr = new String[ret.length];
for (int i = 0; i < strArr.length; i++) {
strArr[i] = ret[i].getHostAddress();
}
return strArr;
} catch (UnknownHostException e) {
e.printStackTrace();
return new String[0];
}
}
示例13: setUp
import java.net.InetAddress; //导入方法依赖的package包/类
protected void setUp() throws Exception {
super.setUp();
if (System.getProperty("hadoop.log.dir") == null) {
System.setProperty("hadoop.log.dir", "/tmp");
}
int taskTrackers = 2;
int dataNodes = 2;
String proxyUser = System.getProperty("user.name");
String proxyGroup = "g";
StringBuilder sb = new StringBuilder();
sb.append("127.0.0.1,localhost");
for (InetAddress i : InetAddress.getAllByName(InetAddress.getLocalHost().getHostName())) {
sb.append(",").append(i.getCanonicalHostName());
}
JobConf conf = new JobConf();
conf.set("dfs.block.access.token.enable", "false");
conf.set("dfs.permissions", "true");
conf.set("hadoop.security.authentication", "simple");
conf.set("hadoop.proxyuser." + proxyUser + ".hosts", sb.toString());
conf.set("hadoop.proxyuser." + proxyUser + ".groups", proxyGroup);
String[] userGroups = new String[]{proxyGroup};
UserGroupInformation.createUserForTesting(proxyUser, userGroups);
UserGroupInformation.createUserForTesting("u1", userGroups);
UserGroupInformation.createUserForTesting("u2", new String[]{"gg"});
dfsCluster = new MiniDFSCluster.Builder(conf).numDataNodes(dataNodes)
.build();
FileSystem fileSystem = dfsCluster.getFileSystem();
fileSystem.mkdirs(new Path("/tmp"));
fileSystem.mkdirs(new Path("/user"));
fileSystem.mkdirs(new Path("/hadoop/mapred/system"));
fileSystem.setPermission(new Path("/tmp"), FsPermission.valueOf("-rwxrwxrwx"));
fileSystem.setPermission(new Path("/user"), FsPermission.valueOf("-rwxrwxrwx"));
fileSystem.setPermission(new Path("/hadoop/mapred/system"), FsPermission.valueOf("-rwx------"));
String nnURI = fileSystem.getUri().toString();
int numDirs = 1;
String[] racks = null;
String[] hosts = null;
mrCluster = new MiniMRCluster(0, 0, taskTrackers, nnURI, numDirs, racks, hosts, null, conf);
ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
}
示例14: addRouterFirewallRule
import java.net.InetAddress; //导入方法依赖的package包/类
public void addRouterFirewallRule(String server, NetworkModel model, String name, String hostname, String[] ports) {
try {
InetAddress ips[] = InetAddress.getAllByName(hostname);
//This Comparator taken from https://thilosdevblog.wordpress.com/2010/09/15/sorting-ip-addresses-in-java/
/**
* LGPL
*/
Arrays.sort(ips, new Comparator<InetAddress>() {
@Override
public int compare(InetAddress adr1, InetAddress adr2) {
byte[] ba1 = adr1.getAddress();
byte[] ba2 = adr2.getAddress();
// general ordering: ipv4 before ipv6
if(ba1.length < ba2.length) return -1;
if(ba1.length > ba2.length) return 1;
// we have 2 ips of the same type, so we have to compare each byte
for(int i = 0; i < ba1.length; i++) {
int b1 = unsignedByteToInt(ba1[i]);
int b2 = unsignedByteToInt(ba2[i]);
if(b1 == b2)
continue;
if(b1 < b2)
return -1;
else
return 1;
}
return 0;
}
private int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
}
});
String serverIp = model.getServerModel(server).getIP();
String cleanName = server.replaceAll("-", "_");
String ingressChain = cleanName + "_ingress";
String egressChain = cleanName + "_egress";
for (String router : model.getRouters()) {
for (InetAddress ip : ips) {
if (!ip.getHostAddress().contains(":")) { //no IPv6, please
for (String port : ports) {
model.getServerModel(router).getFirewallModel().addFilter(server + "_allow_in_" + port, ingressChain,
"-s " + ip.getHostAddress()
+ " -d " + serverIp
+ " -p tcp"
+ " --sport " + port
+ " -j ACCEPT");
model.getServerModel(router).getFirewallModel().addFilter(server + "_allow_out_" + port, egressChain,
"-d " + ip.getHostAddress()
+ " -s " + serverIp
+ " -p tcp"
+ " --dport " + port
+ " -j ACCEPT");
}
}
}
}
}
catch (UnknownHostException e) {
e.printStackTrace();
}
}
示例15: main
import java.net.InetAddress; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
InetAddress addrs[] = InetAddress.getAllByName(args[0]);
for (int i=0; i<addrs.length; i++) {
System.out.println(addrs[i].getHostAddress());
}
}