我正在开发一个连接到服务器的应用程序。到目前为止,如果服务器可用,登录和数据传输工作正常。服务器不可用时会出现问题。在这种情况下,该方法发送登录请求并等待响应。
有谁知道如何检查服务器是否可用(可见)?
必须实现的简单逻辑的伪代码如下:
答案 0 :(得分:48)
他可能需要Java代码,因为他正在使用Android。 Java相当 - 我相信在Android上运行 - 应该是:
InetAddress.getByName(host).isReachable(timeOut)
答案 1 :(得分:18)
通过简单的ping式测试,这对我有用:
static public boolean isURLReachable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://192.168.1.13"); // Change to "http://google.com" for www test.
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(10 * 1000); // 10 s.
urlc.connect();
if (urlc.getResponseCode() == 200) { // 200 = "OK" code (http connection is fine).
Log.wtf("Connection", "Success !");
return true;
} else {
return false;
}
} catch (MalformedURLException e1) {
return false;
} catch (IOException e) {
return false;
}
}
return false;
}
不要忘记在线程中运行此函数(不在主线程中)。
答案 2 :(得分:5)
你可以使用
InetAddress.getByName(host).isReachable(timeOut)
但是当主机没有在tcp 7上应答时,它无法正常工作。您可以通过此功能检查主机在该端口上是否可用所需的内容:
public static boolean isHostReachable(String serverAddress, int serverTCPport, int timeoutMS){
boolean connected = false;
Socket socket;
try {
socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(serverAddress, serverTCPport);
socket.connect(socketAddress, timeoutMS);
if (socket.isConnected()) {
connected = true;
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
socket = null;
}
return connected;
}
答案 3 :(得分:3)
public static boolean IsReachable(Context context) {
// First, check we have any sort of connectivity
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
boolean isReachable = false;
if (netInfo != null && netInfo.isConnected()) {
// Some sort of connection is open, check if server is reachable
try {
URL url = new URL("http://www.google.com");
//URL url = new URL("http://10.0.2.2");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(10 * 1000);
urlc.connect();
isReachable = (urlc.getResponseCode() == 200);
} catch (IOException e) {
//Log.e(TAG, e.getMessage());
}
}
return isReachable;
}
尝试一下,为我工作,不要忘记actived android.permission.ACCESS_NETWORK_STATE
答案 4 :(得分:2)
你在使用HTTP吗?然后,您可以在HTTP连接上设置超时,如下所示:
private void setupHttpClient() {
BasicHttpParams httpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(httpParams, CONNECTION_TIMEOUT);
//...
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(
httpParams, schemeRegistry);
this.httpClient = new DefaultHttpClient(cm, httpParams);
}
如果您随后执行请求,则会在给定的超时后收到异常。
答案 5 :(得分:2)
public boolean isConnectedToServer(String url, int timeout) {
try{
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.connect();
return true;
} catch (Exception e) {
// Handle your exceptions
return false;
}
}
答案 6 :(得分:0)
哦,不,不,Java中的代码不起作用:InetAddress.getByName(“fr.yahoo.com”)。isReachable(200)虽然在LogCat中我看到了它的IP地址(相同的20000毫秒)超时)。
似乎使用'ping'命令很方便,例如:
Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("ping fr.yahoo.com -c 1"); // other servers, for example proc.waitFor(); int exit = proc.exitValue(); if (exit == 0) { // normal exit /* get output content of executing the ping command and parse it * to decide if the server is reachable */ } else { // abnormal exit, so decide that the server is not reachable ... }