当前位置: 代码迷 >> Android >> 很奇怪:android中使用socket出现错误setsockopt failed: EBADF (Bad file number)
  详细解决方案

很奇怪:android中使用socket出现错误setsockopt failed: EBADF (Bad file number)

热度:235   发布时间:2016-05-01 13:19:56.0
很奇怪:android中使用socket出现异常setsockopt failed: EBADF (Bad file number)
Java code
 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!!!
Java code
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 E/connect cause :com.huawei.android.pushagent.datatype.PushException: java.net.SocketExceptio[SocketRead_08:29:07-227]ssl_push_thread(pushagent/SSLPushChannel.java:254)


------解决方案--------------------
直接socket.close不行吗?为何还要加socket.setSoTimeout(0);呢?

如果要终止socket输入或者输出,可以加上:
Java code
if(null != socket && socket.isConnected){socket.shutdownInput();socket.shutdownOutput();}
  相关解决方案