Java 为什么可以';我找不到可以到达特定主机的接口?
我想找到可用于加入特定远程主机的网络接口,为此我编写了以下代码:Java 为什么可以';我找不到可以到达特定主机的接口?,java,network-programming,icmp,Java,Network Programming,Icmp,我想找到可用于加入特定远程主机的网络接口,为此我编写了以下代码: public static void main(String[] args) throws IOException { InetAddress t = InetAddress.getByName("10.10.11.101"); // generic "icmp/ping" test System.out.println(t.isReachable(5000)); // same thing b
public static void main(String[] args) throws IOException
{
InetAddress t = InetAddress.getByName("10.10.11.101");
// generic "icmp/ping" test
System.out.println(t.isReachable(5000));
// same thing but for each interface
final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for(final NetworkInterface netint : Collections.list(nets))
{
if(netint.isUp() && !netint.isLoopback())
{
System.out.println(t.isReachable(netint, 0, 5000) + " - " + netint);
}
}
}
正如您所看到的,generic isReachable告诉我可以访问指定的主机,但是由于未知的原因,在每个接口上尝试一个接一个地访问时,不会返回一个匹配项。这很奇怪(在本例中,这应该是必须返回true的eth4)
是虫子吗?如何执行此任务(即使使用库)
谢谢。好的,所以我尝试了另一种方法来查找界面,下面是我如何完成的:
public static void main(String[] args) throws IOException
{
final InetAddress addr = InetAddress.getByName("10.10.11.8");
final Socket s = new Socket(addr, 80);
System.out.println(searchInterface(s.getLocalAddress().getHostAddress()));
}
public static NetworkInterface searchInterface(final String interf)
{
try
{
final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for(final NetworkInterface netint : Collections.list(nets))
{
if(netint.isUp())
{
final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for(final InetAddress inetAddress : Collections.list(inetAddresses))
{
if(inetAddress.getHostAddress().equals(interf))
{
return netint;
}
}
}
}
}
catch(final SocketException e)
{
}
return null;
}
publicstaticvoidmain(字符串[]args)引发IOException
{
最终InetAddress addr=InetAddress.getByName(“10.10.11.8”);
最终插座s=新插座(地址80);
System.out.println(searchInterface(s.getLocalAddress().getHostAddress());
}
公共静态网络接口搜索接口(最终字符串interf)
{
尝试
{
最终枚举nets=NetworkInterface.getNetworkInterfaces();
用于(最终网络接口netint:Collections.list(nets))
{
如果(netint.isUp())
{
最终枚举inetAddresses=netint.getInetAddresses();
对于(最终InetAddress InetAddress:Collections.list(inetAddresses))
{
if(inetAddress.getHostAddress().equals(interf))
{
返回netint;
}
}
}
}
}
捕获(最终SocketException e)
{
}
返回null;
}
这不是最好的方法,因为您必须知道远程主机上的有效开放端口,但对于我的问题,这仍然是有效的。可能服务器端有一个“技巧”,因此它不允许您经常ping。这是我唯一想到的事情。尝试使用cmd中的ping,因为我读到这个协议我没有很好地用Java实现。事实似乎不是这样,好像我禁用了第一个isReachable测试,但循环一个仍然失败。
public static void main(String[] args) throws IOException
{
final InetAddress addr = InetAddress.getByName("10.10.11.8");
final Socket s = new Socket(addr, 80);
System.out.println(searchInterface(s.getLocalAddress().getHostAddress()));
}
public static NetworkInterface searchInterface(final String interf)
{
try
{
final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for(final NetworkInterface netint : Collections.list(nets))
{
if(netint.isUp())
{
final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for(final InetAddress inetAddress : Collections.list(inetAddresses))
{
if(inetAddress.getHostAddress().equals(interf))
{
return netint;
}
}
}
}
}
catch(final SocketException e)
{
}
return null;
}