定时调用isAlive服务, 连续N次不能, 认为服务没开. 

解决方案 »

  1.   

    不好意思,能否给个小例子呢?使用Socket怎么样呢?
    类如这样的程序:
    public class WebServerTest
    {
    private String host = ""; private int port; public WebServerTest(String host, int port)
    {
    this.host = host;
    this.port = port;
    } public void testWebInfo() throws UnknownHostException, IOException
    {
    Socket s = new Socket(host, port);
    s.setSoTimeout(5 * 1000);
    if(s.isBound())
    {
    System.out.println("OK ");
    }
    // System.out.println(s.getInetAddress().getHostName());
    // System.out.println(s.getInetAddress().getHostAddress());
    s.close();
    } public static void main(String[] args)
    {
    try
    {
    WebServerTest t = new WebServerTest("172.16.8.214", 8080);
    t.testWebInfo();
    }
    catch(UnknownHostException e)
    {
    System.out.println("This is UnknownHostException Error");
    }
    catch(IOException e)
    {
    System.out.println("This is IOException Error");
    }
    }
    }上面的程序如果不能使用,请各位大侠指教,
    最好能给点例子。
      

  2.   

    我做了测试,
    我把本机的网线拔掉,s.setSoTimeout(5 * 1000);能在5秒钟,给出错误的反应。
    但是,我把172.16.8.214机器的网线拔掉,等待的时间竟超过了10秒之多。好像时间的设置并没有什么作用。请各位指教。
      

  3.   

    用ping的方式来做
    String ip = "127.0.0.1";
    Process p = Runtime.getRuntime().exec("ping -n 1 " + ip);
    int status = p.waitFor();
    System.out.println(ip + " is " + (status==0 ? "alive" : "dead"));或者使用java.net.InetAddress class isReachable() method
      

  4.   

    能不能提供web服务除了服务器硬件以外还和你的应用服务器有关系吧所以写一个简单的jsp调用就可以了。
      

  5.   

    setSoTimeout 最好的解决方案。
    至于你说的,对方网线拔掉,造成延缓,你可以多测试几次。1 先把对方的网拔掉再测试
    2 连续测试,在中间拔掉网线整体看,还是很准确的。
      

  6.   

    谢谢各位的回复:
    用ping的方式,或是java.net.InetAddress的isReachable()方法
    只能测试某台机器是否存在或者说是否能够到达。
    就下面的例子来说:
    在局域网中,如果“172.16.8.218”这个地址本身就不存在的话,
    程序能正常返回false,
    如果这时把本机网线拔掉,netAddress.isReachable()方法一直返回true;
    以上的现象,不知道为什么。
    有那位强人能给解释一下。public final class InetAddressTest
    {
    public static void main(String[] args)
    {
    try
    {
    System.out.println(isReachable("172.16.8.218"));
    }
    catch(IOException e)
    {
    e.printStackTrace();
    }
    } private static boolean isReachable(String host) throws IOException
    {
    boolean reachable = false; InetAddress netAddress = InetAddress.getByName(host);
    if(netAddress.isReachable(5 * 1000))
    {
    System.out.println("Host Name : " + netAddress.getHostName());
    reachable = true;
    }
    else
    {
    System.out.println("不能连接到指定的机器!");
    }
    System.out.println("Method:isReachable() is executed, the result is " + reachable); return reachable;
    }
    }
    我还是希望能使用Socket,来完成这项判断。
    3楼的例子不知道还要写什么其它的完善的地方,请各位指教。
    帖子等一段时间在结吧,请各位见谅。