我正在使用这部分代码来ping java中的ip地址,但只有ping localhost成功,而对于其他主机,程序说主机无法访问.我禁用了防火墙,但仍然遇到此问题
public static void main(String[] args) throws UnknownHostException, IOException {
String ipAddress = "127.0.0.1";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
ipAddress = "173.194.32.38";
inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
Run Code Online (Sandbox Code Playgroud)
输出是:
发送Ping请求到127.0.0.1
主机可达
发送Ping请求到173.194.32.38无法
访问主机
可能重复:
如何检查java中是否存在Internet连接?
我想看看是否有人在使用Java时有一种简单的方法来检测是否存在互联网连接.当前的应用程序在Windows的WinInit DLL中使用了"InternetGetConnectedState"方法,但我的应用程序需要跨平台进行mac操作,这种方式不起作用.我根本不知道JNI要么在Java中使用DLL而且它变得令人沮丧.
只有我能想到的方法是打开一个到网站的URL连接,如果失败,则返回false.我的另一种方式是下面,但我不知道这是否一般稳定.如果我拔下网络电缆,我在尝试创建InetAddress时会收到UnknownHostException.否则,如果连接电缆,我会得到一个有效的InetAddress对象.我还没有在Mac上测试下面的代码.
感谢您提供的任何示例或建议.
更新:最终代码块位于底部.我决定接受HTTP请求的建议(在这种情况下是Google).它很简单,并向站点发送请求以返回数据.如果我无法从连接中获取任何内容,则没有互联网.
public static boolean isInternetReachable()
{
try {
InetAddress address = InetAddress.getByName("java.sun.com");
if(address == null)
{
return false;
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
最终代码块:
//checks for connection to the internet through dummy request
public static boolean isInternetReachable()
{
try {
//make a URL to a known source
URL …Run Code Online (Sandbox Code Playgroud) 我想测试是否可以使用Java访问远程系统,或者换句话说使用Java"发送ping".例如,此功能应封装在具有布尔值的方法中
public boolean isReachable(String ip) {
// what to do :-)
}
Run Code Online (Sandbox Code Playgroud)
我已经测试了Java Process类,但我不认为这是最好的方法,因为OutputBuffers的输出处理很复杂.
Process proc = Runtime.getRuntime().exec("ping " + ip);
Run Code Online (Sandbox Code Playgroud)
另一种可能性是创建一个套接字连接并处理抛出的异常,但如果远程系统是一个"裸"的unix系统,另一边可能没有Socket :-)另外,我希望能够设置一个超时时,无法访问远程系统.
那怎么能这样呢?谢谢!
我正在创建一个应用程序,它将实现"ping"命令的一些功能.问题是,我不知道在ANDROID中使用哪些库/库.有人有任何想法吗?
我访问了这些stackoverflow链接,但他们没有太大的帮助.
作为一项任务,我必须在局域网上找到所有活着的计算机.我正在使用类的isReachable功能InetAddress.但问题是没有任何东西可以显示给我.所以我试图isReachable使用谷歌的IP,但仍然无法访问.
这是代码:
import java.net.*;
public class alive{
public static void main(String args[]){
try{
InetAddress ia = InetAddress.getByAddress(new byte[]{(byte)209, (byte)85, (byte)153, (byte)104});
boolean b = ia.isReachable(10000);
if(b){
System.out.println("Reachable");
}
else{
System.out.println("Unrachable");
}
}catch(Exception e){
System.out.println("Exception: " + e.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出是: Unreachable
当我从url.openStream()中捕获异常时,我不想告诉互联网不可用的困难方式.
有一种简单的方法可以判断计算机是否通过Java连接到Internet?在这种情况下,"连接到互联网"意味着能够从特定网址下载数据.
如果我尝试从它下载但它不可用,那么该程序会挂起一点.我不想那么挂.因此,我需要一种快速的方式来查询网站是否可用.
我想找到我当前使用Java代码连接的本地网络中设备的所有IP地址.有用的实用程序Advanced IP Scanner能够在我的子网中找到各种IP地址192.168.178/24:
根据这个答案,我按以下方式构建了我的代码:
import java.io.IOException;
import java.net.InetAddress;
public class IPScanner
{
public static void checkHosts(String subnet) throws IOException
{
int timeout = 100;
for (int i = 1; i < 255; i++)
{
String host = subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout))
{
System.out.println(host + " is reachable");
}
}
}
public static void main(String[] arguments) throws IOException
{
checkHosts("192.168.178");
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不会打印出任何结果,这意味着无法访问任何IP地址.为什么?我的本地网络中有设备,如Advanced IP Scanner扫描中所见.
我正在研究一个需要始终具有互联网连接的Java项目.
我希望我的程序在某些时间间隔(例如5或10秒)继续检查互联网连接,并在没有检测到互联网连接时显示消息.
我曾尝试使用isReachable方法来实现此功能,下面是代码 -
try
{
InetAddress add = InetAddress.getByName("www.google.com");
if(add.isReachable(3000)) System.out.println("Yes");
else System.out.println("No");
}
catch (UnknownHostException e)
{
System.out.println("unkownhostexception");
}
catch (IOException e)
{
System.out.println("IoException");
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码总是返回"否".这段代码有什么问题?
谢谢