java如何判断ip地址? 如何通过java判断给定的ip地址是否可连接? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 尝试建立根据特定ip地址和端口号去建立socket连接,看出异常没,就知道ip地址是否可连接了 代码差不多这样子public class ConnectFriendly { public static void main(String[] argv) { String server_name = argv.length == 1 ? argv[0] : "localhost"; int tcp_port = 80; try { Socket sock = new Socket(server_name, tcp_port); /* Finally, we can read and write on the socket. */ System.out.println(" *** Connected to " + server_name + " ***"); /* ... */ sock.close( ); } catch (UnknownHostException e) { System.err.println(server_name + " Unknown host"); return; } catch (NoRouteToHostException e) { System.err.println(server_name + " Unreachable" ); return; } catch (ConnectException e) { System.err.println(server_name + " connect refused"); return; } catch (java.io.IOException e) { System.err.println(server_name + ' ' + e.getMessage( )); return; } }} 谢谢,我测试了,为什么连其它机器或本机都显示 connect refused 呢? 2楼的方法不行,ping和socket都不是一层的东西,机器没开80端口你怎么连得上?用下面的方法吧 try ...{ InetAddress address = InetAddress.getByName("127.0.0.1"); System.out.println(address.isReachable(5000)); } catch (UnknownHostException e) ...{ e.printStackTrace(); } catch (IOException e) ...{ e.printStackTrace(); } 楼上的方法我试了,但是有防火墙的机器就连不通(能ping通),不知道有什么解决方法。可不可以通过特定端口号进行连接测试? Runtime r = Runtime.getRuntime();String ip = "172.16.41.108";int timeout = 1000;String pingCommand = "ping " + ip + " -w " + timeout;try { Process p = r.exec(pingCommand); if (p == null) { System.out.println("Failed."); }BufferedReader in = new BufferedReader(new InputStreamReader(p. getInputStream())); String line = null; while ( (line = in.readLine()) != null) { System.out.println(line); if (line.startsWith("Reply from")) { System.out.println("Conected."); } } in.close();} catch (Exception ex) { System.out.println("Failed."); } 晕,没排版。 Runtime r = Runtime.getRuntime(); String ip = "172.16.41.108"; int timeout = 1000; String pingCommand = "ping " + ip + " -w " + timeout; try { Process p = r.exec(pingCommand); if (p == null) { System.out.println("Failed."); } BufferedReader in = new BufferedReader(new InputStreamReader(p .getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); if (line.startsWith("Reply from")) { System.out.println("Conected."); } } in.close(); } catch (Exception ex) { System.out.println("Failed."); } 用在linux上import java.lang.Runtime;import java.io.*;public class myTest { private BufferedReader out; public myTest() { } public String myTest(String IP){ String proOut = ""; String pLog = ""; try{ Process pro = Runtime.getRuntime().exec("ping "+IP+" -w 1"); out = new BufferedReader(new InputStreamReader(pro.getInputStream())); pro.waitFor(); String record = out.readLine(); while(record != null){ proOut = proOut + record + "\n"; record = out.readLine(); } out.close(); pro.destroy(); //ping p = new ping(); if (this.result(proOut).equals("running")){ pLog = "Server "+IP+" is running."; }else{ pLog = "Server "+IP+" doesn't run."; } //pLog = p.result(proOut); }catch(Exception e){ pLog = "Script executing error!"; } return pLog; } public String result(String s){ if (s.indexOf("bytes from") != -1){ return "running"; }else{ return "not run"; } } } 不好意思,加入格式。import java.lang.Runtime;import java.io.*;public class myTest { private BufferedReader out; public myTest() { } public String myTest(String IP){ String proOut = ""; String pLog = ""; try{ Process pro = Runtime.getRuntime().exec("ping "+IP+" -w 1"); out = new BufferedReader(new InputStreamReader(pro.getInputStream())); pro.waitFor(); String record = out.readLine(); while(record != null){ proOut = proOut + record + "\n"; record = out.readLine(); } out.close(); pro.destroy(); //ping p = new ping(); if (this.result(proOut).equals("running")){ pLog = "Server "+IP+" is running."; }else{ pLog = "Server "+IP+" doesn't run."; } //pLog = p.result(proOut); }catch(Exception e){ pLog = "Script executing error!"; } return pLog; } public String result(String s){ if (s.indexOf("bytes from") != -1){ return "running"; }else{ return "not run"; } } } SWING 透明按钮问题 关于ArrayList的一个问题 考试题目 JDK环境变量是配置 如何清除JTable中的内容 大家好.新手怎么学ajax呀? 一个简单的问题(函数参数的返回值)如下 如何去掉Applet中的"Java Applet Window" 如何对List<Map> 进行排序? 为何applet在jbuilder7下的appletviewer能看,在网页中却出err new Thread().sleep Thread().sleep Thread().currentThread().sleep 同一库b表内容修改a表也作相应修改如何实现
用下面的方法吧 try ...{
InetAddress address = InetAddress.getByName("127.0.0.1");
System.out.println(address.isReachable(5000));
} catch (UnknownHostException e) ...{
e.printStackTrace();
} catch (IOException e) ...{
e.printStackTrace();
}
可不可以通过特定端口号进行连接测试?
String ip = "172.16.41.108";
int timeout = 1000;
String pingCommand = "ping " + ip + " -w " + timeout;
try {
Process p = r.exec(pingCommand);
if (p == null)
{
System.out.println("Failed.");
}
BufferedReader in = new BufferedReader(new InputStreamReader(p.
getInputStream()));
String line = null;
while ( (line = in.readLine()) != null)
{
System.out.println(line);
if (line.startsWith("Reply from"))
{
System.out.println("Conected.");
}
}
in.close();
}
catch (Exception ex)
{
System.out.println("Failed.");
}
Runtime r = Runtime.getRuntime();
String ip = "172.16.41.108";
int timeout = 1000;
String pingCommand = "ping " + ip + " -w " + timeout;
try {
Process p = r.exec(pingCommand);
if (p == null) {
System.out.println("Failed.");
}
BufferedReader in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
if (line.startsWith("Reply from")) {
System.out.println("Conected.");
}
}
in.close();
} catch (Exception ex) {
System.out.println("Failed.");
}
import java.io.*;public class myTest {
private BufferedReader out;
public myTest() {
}
public String myTest(String IP){
String proOut = "";
String pLog = "";
try{
Process pro = Runtime.getRuntime().exec("ping "+IP+" -w 1");
out = new BufferedReader(new InputStreamReader(pro.getInputStream()));
pro.waitFor();
String record = out.readLine();
while(record != null){
proOut = proOut + record + "\n";
record = out.readLine();
}
out.close();
pro.destroy();
//ping p = new ping();
if (this.result(proOut).equals("running")){
pLog = "Server "+IP+" is running.";
}else{
pLog = "Server "+IP+" doesn't run.";
}
//pLog = p.result(proOut);
}catch(Exception e){
pLog = "Script executing error!";
}
return pLog;
}
public String result(String s){
if (s.indexOf("bytes from") != -1){
return "running";
}else{
return "not run";
}
}
}
import java.lang.Runtime;
import java.io.*;public class myTest {
private BufferedReader out;
public myTest() {
}
public String myTest(String IP){
String proOut = "";
String pLog = "";
try{
Process pro = Runtime.getRuntime().exec("ping "+IP+" -w 1");
out = new BufferedReader(new InputStreamReader(pro.getInputStream()));
pro.waitFor();
String record = out.readLine();
while(record != null){
proOut = proOut + record + "\n";
record = out.readLine();
}
out.close();
pro.destroy();
//ping p = new ping();
if (this.result(proOut).equals("running")){
pLog = "Server "+IP+" is running.";
}else{
pLog = "Server "+IP+" doesn't run.";
}
//pLog = p.result(proOut);
}catch(Exception e){
pLog = "Script executing error!";
}
return pLog;
}
public String result(String s){
if (s.indexOf("bytes from") != -1){
return "running";
}else{
return "not run";
}
}
}