JAVA使用InetAddress指定端口 [英] JAVA Specifying port with InetAddress

查看:695
本文介绍了JAVA使用InetAddress指定端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用InetAddress来确定我的服务器是否在线。

I am using InetAddress to determine if my server is online.

如果服务器处于离线状态,它将重启服务器。

If the server is offline it will restart the server.

如果服务器在线,这个过程每5分钟循环再次检查一次。

This process loops every 5 minutes to check once again if the server is online.

它工作正常,但现在我需要弄清楚如何指定我想在检查服务器状态而不是默认端口80时使用端口43594。

It works fine but now I need to figure out how to specify that I want to use port 43594 when checking the server status instead of the default port 80.

谢谢!这是我的代码:

import java.net.InetAddress;
public class Test extends Thread {
    public static void main(String args[]) {
        try {
            while (true) {
                try
                {
                    InetAddress address = InetAddress.getByName("cloudnine1999.no-ip.org");
                    boolean reachable = address.isReachable(10000);
                    if(reachable){
                        System.out.println("Online");
                    }
                    else{
                        System.out.println("Offline: Restarting Server...");
                        Runtime.getRuntime().exec("cmd /c start start.bat");
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                Thread.sleep(5 * 60 * 1000);
            }
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

编辑:

好的,所以我接受了一些人的建议,我把它变成了这个。但现在当我取消注释这一行..
Runtime.getRuntime()。exec(cmd / c start start.bat);

Okay so I took someones advice and I made it into this. But now when I uncomment this line.. Runtime.getRuntime().exec("cmd /c start start.bat");

我收到此错误..

错误:未报告的异常IOException;必须被抓住或宣布被抛出

这是我目前的代码:

import java.net.*;
import java.io.*;
public class Test extends Thread {
    public static void main(String args[]) {
        try {
            while (true) {
                SocketAddress sockaddr = new InetSocketAddress("cloudnine1999.no-ip.org", 43594);
                Socket socket = new Socket();
                boolean online = true;
                try {
                    socket.connect(sockaddr, 10000);
                }
                catch (IOException IOException) {
                    online = false;
        }
                if(!online){
            System.out.println("OFFLINE: Restarting Server..");
            //Runtime.getRuntime().exec("cmd /c start start.bat");
        }
                if(online){
                    System.out.println("ONLINE");
                }
                Thread.sleep(1 * 10000);
            }
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


推荐答案

正如我在评论中已经提到的,根据 Javadoc isReachable未以允许您控制所选端口的方式实现。实际上,如果系统权限允许这样做,它只会ping机器(ICMP请求)。

As I already mentioned in the comments, according to the Javadoc isReachable isn't implemented in a way that would allow you to control the selected port. Actually, if it is allowed to do so by system privileges it will just ping the machine (ICMP request).

手动完成(即使用套接字)肯定会起作用并且不会更复杂和/或更长时间:

Doing it manually (ie, using a socket) will certainly work and isn't really more complicated and/or longer:

SocketAddress sockaddr = new InetSocketAddress("cloudnine1999.no-ip.org", 43594);
// Create your socket
Socket socket = new Socket();
boolean online = true;
// Connect with 10 s timeout
try {
    socket.connect(sockaddr, 10000);
} catch (SocketTimeoutException stex) {
    // treating timeout errors separately from other io exceptions
    // may make sense
    online=false;
} catch (IOException iOException) {
    online = false;    
} finally {
    // As the close() operation can also throw an IOException
    // it must caught here
    try {
        socket.close();
    } catch (IOException ex) {
        // feel free to do something moderately useful here, eg log the event
    }

}
// Now, in your initial version all kinds of exceptions were swallowed by
// that "catch (Exception e)".  You also need to handle the IOException
// exec() could throw:
if(!online){
    System.out.println("OFFLINE: Restarting Server..");
    try {
        Runtime.getRuntime().exec("cmd /c start start.bat");
    } catch (IOException ex) {
         System.out.println("Restarting Server FAILED due to an exception " + ex.getMessage());
    }
}        

编辑:忘记处理 IOException 这也意味着服务器无法正常运行,添加

forgot to handle IOException which also means the server isn't functioning, added

EDIT2:添加了close()可以抛出的IOException的处理

added the handling of the IOException that close() can throw

EDIT3:和exec()的异常处理

and exception handling for exec()

这篇关于JAVA使用InetAddress指定端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆