我记得在java network programming2(oreilly)一书说的
java是不可以实现ping的(p18页)
Slightly more of an issue is that Java does not provide direct access to the IP layer below TCP and UDP, so it can't be used to write programs such as ping or traceroute.
不知道你是不是说的这个ping
如果真的要实现的话,估计要调用native method了
java是不可以实现ping的(p18页)
Slightly more of an issue is that Java does not provide direct access to the IP layer below TCP and UDP, so it can't be used to write programs such as ping or traceroute.
不知道你是不是说的这个ping
如果真的要实现的话,估计要调用native method了
(顺便说,这个ping就是局域网内的ping),至于jbuilder,我还不会,不过,我会赶紧去看;谢谢各位的支持。
********************************************************
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class SystemCommand
{
/**
* @param shellCommand
*
*/
public List execute(String shellCommand)
{
try
{
start(shellCommand);
List vResult= new ArrayList();
DataInputStream in= new DataInputStream(p.getInputStream());
BufferedReader reader= new BufferedReader(new InputStreamReader(in));
String line=null;
do
{
line= reader.readLine();
if (line == null)
{
break;
} else
{
vResult.add(line);
}
} while (true);
reader.close();
return vResult;
} catch (Exception e)
{
//error
return null;
}
}
/**
* @param shellCommand
*
*/
public void start(String shellCommand)
{
try
{
if (p != null)
{
kill();
}
Runtime sys= Runtime.getRuntime();
p= sys.exec(shellCommand);
} catch (Exception e)
{
System.out.println(e.toString());
}
}
/**
kill this process
*/
public void kill()
{
if (p != null)
{
p.destroy();
p= null;
}
}
private Process p=null;
}
***********************************************************************]
//在这里执行ping
import java.util.List;
import java.util.Iterator;
public class exePing
{
//网卡物理地址长度
static private final int MAC_LENGTH= 16;
public static void main(String[] args)
{
System.out.println(getPing("172.18.10.10"));
} static public String getPing(String ip){
SystemCommand shell = new SystemCommand();
String cmd = "cmd.exe /c ping "+ip;
List result = shell.execute(cmd);
return result.toString();
}
}