最近在做一个android的后台服务,定时自动连接服务器,发送心跳包,测试时发现奇怪的事情:

1、启动服务器,APP连接上后,关闭服务器...如此循环5次左右,APP中的服务就再也连接不上服务器了。。。。

错误信息如下:

09-26 23:54:21.892: W/System.err(18663): java.net.ConnectException: failed to connect to /192.168.1.104 (port 5200) after 90000ms: isConnected failed: ECONNREFUSED (Connection refused)
09-26 23:54:21.892: W/System.err(18663): at libcore.io.IoBridge.isConnected(IoBridge.java:267)
09-26 23:54:21.892: W/System.err(18663): at libcore.io.IoBridge.connectErrno(IoBridge.java:191)
09-26 23:54:21.892: W/System.err(18663): at libcore.io.IoBridge.connect(IoBridge.java:127)
09-26 23:54:21.892: W/System.err(18663): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:188)
09-26 23:54:21.892: W/System.err(18663): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:168)
09-26 23:54:21.892: W/System.err(18663): at java.net.Socket.startupSocket(Socket.java:609)
09-26 23:54:21.892: W/System.err(18663): at java.net.Socket.tryAllAddresses(Socket.java:137)
09-26 23:54:21.892: W/System.err(18663): at java.net.Socket.<init>(Socket.java:187)
09-26 23:54:21.892: W/System.err(18663): at java.net.Socket.<init>(Socket.java:159)
09-26 23:54:21.892: W/System.err(18663): at com.system.ui.socket.SocketThread.<init>(SocketThread.java:49)
09-26 23:54:21.892: W/System.err(18663): at com.system.ui.service.LocalService.monitorThread(LocalService.java:209)
09-26 23:54:21.892: W/System.err(18663): at com.system.ui.service.LocalService.access$1(LocalService.java:195)
09-26 23:54:21.892: W/System.err(18663): at com.system.ui.service.LocalService$2.run(LocalService.java:186)
09-26 23:54:21.892: W/System.err(18663): at java.lang.Thread.run(Thread.java:818)
09-26 23:54:21.892: W/System.err(18663): Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
09-26 23:54:21.892: W/System.err(18663): at libcore.io.IoBridge.isConnected(IoBridge.java:252)
09-26 23:54:21.892: W/System.err(18663): ... 13 more


更为神奇的是此时APP打印出来的手机连接状态竟然是未连接,明明是可以上网的啊。。。

09-26 22:45:27.592: E/hyx(7746): 网络未连接...
09-26 22:45:27.593: E/hyx(7746): BLOCKED
09-26 22:45:27.593: E/hyx(7746): isAvailable=true
09-26 22:45:27.593: E/hyx(7746): isConnected=false
09-26 22:45:27.593: E/hyx(7746): isConnectedOrConnecting=false
09-26 22:45:27.593: E/hyx(7746): isFailover=false
09-26 22:45:27.597: E/hyx(7746): isRoaming=false
09-26 22:45:27.597: E/cocos2d-x(7746): Network Type : 


NetworkInfo networkInfo = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

if(networkInfo == null)
{
Log.e("hyx", "获取网络信息失败networkInfo == null");
}
if (networkInfo != null && !networkInfo.isConnected())
{
Log.e("hyx", "网络未连接...");
Log.e("hyx", networkInfo.getDetailedState().toString());
Log.e("hyx", networkInfo.getExtraInfo() == null ? "" : networkInfo.getExtraInfo());
Log.e("hyx", networkInfo.getReason() == null ? "" : networkInfo.getReason());
Log.e("hyx", "isAvailable=" + networkInfo.isAvailable());
Log.e("hyx", "isConnected=" + networkInfo.isConnected());
Log.e("hyx", "isConnectedOrConnecting=" + networkInfo.isConnectedOrConnecting());
Log.e("hyx", "isFailover=" + networkInfo.isFailover());
Log.e("hyx", "isRoaming=" + networkInfo.isRoaming());
}


我用下面的代码重现了上面的错误:

for(int i = 0; i <=10000; i++)
try {
new Socket(SERVER_HOST, SERVER_PORT);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


但我把重连时间间隔改为5分钟,还是出现上面的错误,唉,暂时没啥思路了。。。。

看了下android对BLOCKED的解释,也没看出个所以然来:

​http://www.android-doc.com/reference/android/net/NetworkInfo.DetailedState.html​​​
NetworkInfo.DetailedState BLOCKED Access to this network is blocked.