finally { Log.i("bai", " finally socket finally isConnected : "+ socket.isConnected()); Log.i("bai", "socket info : "+ socket.toString() ); Log.i("bai", "socket isConnected : "+ socket.isConnected()); if (null != socket) { try { // if(dis != null) { // dis.close(); // } Log.i("bai", "socket isConnected null != socket: "+ socket.isConnected()); Log.i("bai", "socket.isClosed(): "+ socket.isClosed()); socket.setSoTimeout(0); } catch(SocketException se) { Log.i("bai",se.getMessage()); throw new PushException(se, ErrorType.Err_Read); } catch(IOException ie) { throw new PushException(ie, ErrorType.Err_Read); } } }
这个finally是在read socket结束时执行,不过让人奇怪的是在socket不为空,还连接着的情况下,我设置socket永不超时就发现了setsockopt failed: EBADF (Bad file number)问题,导致socket异常关闭。网上也没找到详细资料,请各位道友帮忙看下,very thank you!!! 08-22 08:34:11.452: I/bai(6466): [SocketRead_08:29:07-227] finally socket finally isConnected : true(pushagent/SSLPushChannel.java:344) 08-22 08:34:11.452: I/bai(6466): [SocketRead_08:29:07-227]socket info : Socket[address=/122.11.38.26,port=5223,localPort=60280](pushagent/SSLPushChannel.java:345) 08-22 08:34:11.452: I/bai(6466): [SocketRead_08:29:07-227]socket isConnected : true(pushagent/SSLPushChannel.java:346) 08-22 08:34:11.452: I/bai(6466): [SocketRead_08:29:07-227]socket isConnected null != socket: true(pushagent/SSLPushChannel.java:352) 08-22 08:34:11.452: I/bai(6466): [SocketRead_08:29:07-227]socket.isClosed(): false(pushagent/SSLPushChannel.java:353) 08-22 08:34:11.462: I/bai(6466): [SocketRead_08:29:07-227]setsockopt failed: EBADF (Bad file number)(pushagent/SSLPushChannel.java:356) 08-22 08:34:11.462: I/bai(6466): [ 08-22 08:34:11.471 6466:0x19ad 直接socket.close不行吗?为何还要加socket.setSoTimeout(0);呢?
如果要终止socket输入或者输出,可以加上: if(null != socket && socket.isConnected) { socket.shutdownInput(); socket.shutdownOutput(); } 原因是由于java 每次GC时,垃圾回收器会将session重用中的ParcelFileDescriptor 参数被回收,导致push连接的SSLsocket成为假连接,此时socket.isConnected()=true。但是实际上socket连接已经成为不可用的或者说是坏掉的,合理保存下次参数再在下次使用就可以了。
|