繁体   English   中英

知道如何在读取套接字数据(InputStream)时检测到断开连接

[英]Knowing How to Detect a Disconnection when Reading Socket Data (InputStream)

我有一个不断从InputStream读取数据的线程。 InputStream数据来自蓝牙套接字。 以前,我没有在InputStream read语句周围使用if(mmInStream.available()> 0),并且当蓝牙套接字消失(有人关闭了设备)时,mmInStream.read会抛出IOException,然后我可以处理我的断开逻辑。 确定何时发生断开连接的最佳方法是什么?

0xEE的第一个字节告诉我数据包的首部,第二个告诉我读取的长度。

public void run() {
            byte[] tempBuffer = new byte[1024];
            byte[] buffer = null;
            int byteRead=0;
        long timeout=0;
        long wait=100;

            while (true) {
                try {
                timeout = System.currentTimeMillis() + wait;
                    if(mmInStream.available() > 0) {
                        while((mmInStream.available() > 0) && (tempBuffer[0] != (byte) 0xEE) && (System.currentTimeMillis() < timeout)){
                        byteRead = mmInStream.read(tempBuffer, 0, 1);
                    }
                    if(tempBuffer[0] == (byte) 0xEE){
                        timeout = System.currentTimeMillis() + wait; 
                        while(byteRead<2 && (System.currentTimeMillis() < timeout)){
                            byteRead += mmInStream.read(tempBuffer, 1, 1); 
                        }
                    }
                    timeout = System.currentTimeMillis() + wait; 
                    while((byteRead<tempBuffer[1]) && (System.currentTimeMillis() < timeout)){
                        byteRead += mmInStream.read(tempBuffer, byteRead, tempBuffer[1]-byteRead); 
                    }
                    }

                    if(byteRead > 0){
                        //do something with the bytes read in               
                    } 
                }

                catch (IOException e) {
                    bluetoothConnectionLost();
                    break;
                }
            }

        }

您不需要所有带有available()的恶意键。 只需使用setSoTimeout设置读取超时,读取,检测返回-1的读取,如果> 0,则使用读取返回的计数,而不是假设缓冲区已满,捕获SocketTimeoutException来检测读取超时,并捕获IOException来检测其他损坏。

看完文档后,我认为是这样的:

public void run() {
    byte[] tempBuffer = new byte[1024];
    int byteRead = 0;

    while (true) {
        try {
            bytesRead = mmInStream.read(tempBuffer, 0, tempBuffer.length);
            if (bytesRead < 0)
                // End of stream.
                break;

            // Do something with the bytes read in. There are bytesRead bytes in tempBuffer.
        } catch (IOException e) {
            bluetoothConnectionLost();
            break;
        }
    }
}

我认为是这样的:

 void fun(){
   isOpen = true;
   try{
      InputStream stream = socket.getInputStream();
      while(isOpen){
         byte[] buf = new byte[8];
         int pos = stream.read(buf);
         if (pos < 0) {
            throw new IOException();
         }
         //dosomething...
      }
  }catch(IOException e) {
    isOpen  = false;
  }finally{
    //do dispose here
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM