这可能是一个设计问题,但无论如何我想知道。
示例:我有一个数据连接,并且还在我的Android设备/手机上连接到我的本地网络。我想在本地网络上访问 deviceB 。
如果:我的本地网络网关与外部世界有连接,我的Android设备可以访问 deviceB 。外部连接工作正常。
如果:我的本地网络网关没有连接到外部世界,我的Android设备无法访问 deviceB 。外部连接工作正常。
此示例的deviceB 是本地网络服务器http:// {localipaddress}
已编辑:
LAN IP范围10.1.1 /
deviceB addr:10.1.1.154
android wifi addr:10.1.1.111
我也试过在我的Android手机上使用浏览器(chrome)并且结果相同
12-04 16:46:54.370 30997-31204/com.linuxjet.apps.agave D/isReachable: Connection to 10.1.1.154 failed
12-04 16:46:54.371 30997-31204/com.linuxjet.apps.agave W/System.err: java.net.SocketTimeoutException
12-04 16:46:54.392 30997-31204/com.linuxjet.apps.agave W/System.err: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:484)
12-04 16:46:54.392 30997-31204/com.linuxjet.apps.agave W/System.err: at java.net.PlainSocketImpl.-wrap0(PlainSocketImpl.java)
12-04 16:46:54.392 30997-31204/com.linuxjet.apps.agave W/System.err: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
12-04 16:46:54.392 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.okio.Okio$2.read(Okio.java:135)
12-04 16:46:54.392 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
12-04 16:46:54.392 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:904)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getHeaders(HttpURLConnectionImpl.java:150)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:194)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at java.net.URLConnection.getContentType(URLConnection.java:325)
12-04 16:46:54.393 30997-31204/com.linuxjet.apps.agave W/System.err: at java.net.URLConnection.getContent(URLConnection.java:193)
12-04 16:46:54.394 30997-31204/com.linuxjet.apps.agave W/System.err: at com.linuxjet.apps.agave.Tools.isReachable(Tools.java:299)
12-04 16:46:54.394 30997-31204/com.linuxjet.apps.agave W/System.err: at com.linuxjet.apps.agave.services.ConnectionService.rebuildISY(ConnectionService.java:107)
12-04 16:46:54.394 30997-31204/com.linuxjet.apps.agave W/System.err: at com.linuxjet.apps.agave.services.ConnectionService.onHandleIntent(ConnectionService.java:91)
12-04 16:46:54.394 30997-31204/com.linuxjet.apps.agave W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
12-04 16:46:54.394 30997-31204/com.linuxjet.apps.agave W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
12-04 16:46:54.394 30997-31204/com.linuxjet.apps.agave W/System.err: at android.os.Looper.loop(Looper.java:148)
12-04 16:46:54.394 30997-31204/com.linuxjet.apps.agave W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
这是我用来测试连接的代码:
public static boolean isReachable(String host, int port, int timeout, Boolean ssl) {
URL url = null;
try {
url = new URL((ssl ? "https://" : "http://") + host+":"+port);
//open a connection to that source
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection();
if (ssl) {
TrustAllSSLSocketFactory sslfactory;
try {
sslfactory = new TrustAllSSLSocketFactory();
((HttpsURLConnection) urlConnect).setSSLSocketFactory(sslfactory);
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
}
}
urlConnect.setReadTimeout(timeout);
urlConnect.setConnectTimeout(timeout);
urlConnect.setRequestMethod("GET");
urlConnect.setDoInput(true);
urlConnect.connect();
urlConnect.getContent();
urlConnect.disconnect();
Log.d("isReachable","Connection to " + url.getHost() + " succeeded");
return true;
//trying to retrieve data from the source. If there
//is no connection, this line will fail
} catch (IOException e) {
Log.d("isReachable", "Connection to " + url.getHost() + " failed");
return false;
}
}