我试图找出我的网络上的特定主机是否可访问.我的java代码如下:
InetAddress adr = InetAddress.getByName(host);
if(adr.isReachable(3000)){
System.out.println(host + " is reachable");
}
Run Code Online (Sandbox Code Playgroud)
这非常有效,但是如果我将超时降低到500毫秒,它将不再指定主机可达.我计划在循环中检查相当多的主机,因此具有低超时非常重要.如果我从Windows命令行手动ping主机,则需要不到10毫秒.
那么为什么Java方法需要更高的超时才能成功?有没有其他选择isReachable()?
我正在使用InetAddress来确定我的服务器是否在线.
如果服务器处于脱机状态,它将重新启动服务器.
此过程每5分钟循环一次,以便再次检查服务器是否在线.
它工作正常但现在我需要弄清楚如何在检查服务器状态而不是默认端口80时指定我想使用端口43594.
谢谢!这是我的代码:
import java.net.InetAddress;
public class Test extends Thread {
public static void main(String args[]) {
try {
while (true) {
try
{
InetAddress address = InetAddress.getByName("cloudnine1999.no-ip.org");
boolean reachable = address.isReachable(10000);
if(reachable){
System.out.println("Online");
}
else{
System.out.println("Offline: Restarting Server...");
Runtime.getRuntime().exec("cmd /c start start.bat");
}
}
catch (Exception e)
{
e.printStackTrace();
}
Thread.sleep(5 * 60 * 1000);
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
好的,所以我接受了一些人的建议,我把它做成了这个.但现在当我取消注释这一行..
Runtime.getRuntime().exec("cmd /c start start.bat");
我收到这个错误..
error: unreported exception IOException; must …
我正在乱用Java中的DNS服务 - 我特意尝试查找所有google.com地址并将其显示在数组中,类似于使用nslookup运行查找:
nslookup -q=TXT _netblocks.google.com 8.8.8.8
Run Code Online (Sandbox Code Playgroud)
我正在使用InetAddress这个,但继续得到异常错误.由于错误引用了"未知主机",我认为InetAddress不能读取TXT记录(如果我使用google.com它可以工作,但这并不显示完整的IP范围).以下是我的代码:
InetAddress dnsresult[] = InetAddress.getAllByName("_netblocks.google.com");
for (int i=0; i<dnsresult.length; i++)
System.out.println (dnsresult[i]);
Run Code Online (Sandbox Code Playgroud)
如果有人能指出我正确的方向,我将不胜感激.
-JK
我有一个Java应用程序需要通过套接字连接到两台不同的机器上的两个不同的服务器.一台服务器已配置为侦听IPv4连接,而另一台服务器已配置为侦听IPv6连接.
现在,假设"host1"是侦听IPv4连接的服务器的计算机名称,而"host2"是侦听IPv6连接的服务器的计算机名称.我需要Inet4Address为"host1"和Inet6Address"host2"创建一个到每个服务器的套接字连接,如下所示:
Inet4Address inet4 = (Inet4Address)InetAddress.getByName("host1");
InetSocketAddress soc4 = new InetSocketAddress(inet4, 7777);
...
Inet6Address inet6 = (Inet6Address)InetAddress.getByName("host2");
InetSocketAddress soc6 = new InetSocketAddress(inet6, 7777);
...
Run Code Online (Sandbox Code Playgroud)
但是,出于向后兼容性原因,默认情况下,JVM优先使用IPv6地址上的IPv4地址.因此,在上面的代码中,第一次尝试连接到"host1"成功,但第二次尝试连接到"host2"失败,因为InetAddress.getByName("host2")返回an Inet4Address而不是Inet6Address.
我知道我可以将系统属性设置java.net.preferIPv6Addresses为true以优先选择IPv4上的IPv6地址,但这反过来导致第二次尝试连接到"host2"成功,但第一次尝试连接到"host1"失败(!)因为InetAddress.getByName("host1")返回一个Inet6Address代替Inet4Address.
系统属性java.net.preferIPv6Addresses只被读取一次(参见InetAddress line 212-218),因此即使我将其值设置为true后将其值更改为false也不会产生任何影响.
那么在这种情况下我能做些什么呢?这似乎是一个普遍的问题,所以必须有一种方法可以做到这一点.
请注意,我当然可以使用InetAddress.getByAddress(),并提供每台机器的IP地址,而不是明确要回的Inet4Address和Inet6Address,但我并不想这样做,除非我真的要.请其他解决方案.
哦,我正在使用java 1.6.0_19以防万一.
谢谢!
我正在尝试用Java获取我的机器(Windows 7 x64)的完全限定名称.在我的机器上,我更新了c:\ Windows\system32\drivers\etc\hosts文件,使其具有如下条目:
10.44.2.167 myserver myserver.domain.com
Run Code Online (Sandbox Code Playgroud)
我们所有的系统在\ etc\hosts文件中都有一个条目(以上格式),我无法更改.
以下代码始终返回"myserver",我永远无法获得完全限定的名称.
InetAddress addr = InetAddress.getLocalHost();
String fqName = addr.getCanonicalHostName();
Run Code Online (Sandbox Code Playgroud)
我如何用Java实现这一目标?
谢谢,
Shreyas
我可以使用inetaddress类获取局域网中的所有设备IP地址.我需要做的是反向查找ip-address并在网络中找到设备名称,如:"Jimmie的Macbook"
我的代码块能够通过本地网络范围获取所有IP地址:
private ArrayList<String> scanSubNet(String subnet) {
ArrayList<String> hosts = new ArrayList<>();
InetAddress inetAddress;
for (int i = 1; i <= 255; i++) {
try {
inetAddress = InetAddress.getByName(subnet + String.valueOf(i));
if (inetAddress.isReachable(1000)) {
hosts.add(inetAddress.getHostName());
Log.d(TAG, InetAddress.getByAddress(inetAddress.getAddress()).getHostAddress());
Log.d(TAG, InetAddress.getByAddress(inetAddress.getAddress()).getCanonicalHostName());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return hosts;
}
Run Code Online (Sandbox Code Playgroud)
我称之为我的方法;
ArrayList<String> subnetList = scanSubNet("192.168.1.");ArrayList<String> subnetList = scanSubNet("192.168.1.");
Run Code Online (Sandbox Code Playgroud)
在Log.d(TAG,我试图用反向dns获取设备名称.但两行都给我输出为ip-address(非设备名称为字符串)
有没有机会成功呢?
此致,Onder.
我正在使用Clojure,但我可以阅读Java,所以这不是Clojure特定的问题.这甚至似乎都不适用于Java.
我正在尝试使用isReachable实现一些'ping'功能.我正在使用的代码是这样的:
(.isReachable (java.net.InetAddress/getByName "www.microsoft.com") 5000)
Run Code Online (Sandbox Code Playgroud)
我的一位好朋友翻译成Java:
public class NetTest {
public static void main (String[] args) throws Exception{
String host = "acidrayne.net";
InetAddress a = InetAddress.getByName(host);
System.out.println(a.isReachable(10000));
}
}
Run Code Online (Sandbox Code Playgroud)
这两个都返回false.我想我一定是做错了,但谷歌的研究告诉我的不同之处.我很困惑!
我试图使用Java代码获取我的域中的Windows机器的FQDN.
我试过InetAddress.getByName("machine-1").getCanonicalHostName()但只返回机器名称.
另一方面,如果我ping"machine-1",我会获得完整的域名.
你知道怎么做吗?
在我的 Jboss 服务器上运行的应用程序中,我们与另一个应用程序建立 http 连接。这两个应用程序都位于同一数据中心。我们使用 VIP 来连接到应用程序。这在生产中工作得很好,除了当我们得到“java.net.UnknownHostException”时的很小一部分。这个百分比非常低(~0.2%),但考虑到我们收到的请求量巨大,实际数量不容忽视。
该错误是间歇性的,并且在时间和频率上都不遵循规律。有时我们一天会收到 200 次,有时我们会连续 3-4 天都没有收到。有时我们甚至会在预期流量较低时(夜间 1-3 点)得到它。
AFAIK,当 DNS 无法解析给定主机名的 IP 时,就会出现这种特殊的异常。我们检查了 DNS 设置/配置等,一切看起来都很好。不想直接在属性文件或 /etc/hosts 文件中使用 IP 地址。
此时,我不确定下一步可以做什么来进一步调试。此处的任何帮助/指导将不胜感激。
Caused by: java.net.UnknownHostException: <VIP Name>
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[?:1.6.0_25]
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:850) ~[?:1.6.0_25]
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201) ~[?:1.6.0_25]
at java.net.InetAddress.getAllByName0(InetAddress.java:1154) ~[?:1.6.0_25]
at java.net.InetAddress.getAllByName(InetAddress.java:1084) ~[?:1.6.0_25]
at java.net.InetAddress.getAllByName(InetAddress.java:1020) ~[?:1.6.0_25]
at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:242) ~[httpclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:130) ~[httpclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149) ~[httpclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121) ~[httpclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573) ~[httpclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425) ~[httpclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820) ~[httpclient-4.1.2.jar:4.1.2]
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754) ~[httpclient-4.1.2.jar:4.1.2]
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:88) ~[spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46) ~[spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE] …Run Code Online (Sandbox Code Playgroud) 对于这个链接了使用设置来产生IP地址的代码.
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
System.out.println(iface.getDisplayName() + " " + ip);
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
我已经实现了确切的代码来获取IP地址,但它提供了IPv4和IPv6地址.以下是生成的值.
Qualcomm Atheros AR5BWB222 Wireless Network Adapter 192.168.1.5
Qualcomm Atheros AR5BWB222 Wireless Network Adapter fe80:0:0:0:a874:xxxx:xxxx:9150%wlan0
Run Code Online (Sandbox Code Playgroud)
(IPv6地址编辑)
有什么方法我只能获得IPv4值,而不是两者兼而有之?
inetaddress ×10
java ×9
dns ×3
ping ×2
android ×1
clojure ×1
exception ×1
fqdn ×1
ip-address ×1
ipv4 ×1
ipv6 ×1
sockets ×1
unknown-host ×1
windows ×1