知道如何在读取套接字数据(InputStream)时检测到断开连接 [英] Knowing How to Detect a Disconnection when Reading Socket Data (InputStream)

查看:105
本文介绍了知道如何在读取套接字数据(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()来使用所有这些malarkey.只需使用setSoTimeout设置读取超时,读取,检测返回-1的读取,如果大于0,则使用读取返回的计数,而不是假设缓冲区已满,捕获SocketTimeoutException来检测读取超时,并捕获IOException来检测其他损坏. >

I have a thread that is constantly reading data from an InputStream. The InputStream data is coming from a Bluetooth socket. Previously, I wasn't using the if(mmInStream.available() > 0) around the InputStream read statement and when the bluetooth socket went away (someone turned off the device), the mmInStream.read would throw a IOException and then I could process my disconnection logic. What is the best way to determine when a disconnect has occurred?

First byte of 0xEE tells me its the leader of the data packet and the second tells me the length to read.

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;
                }
            }

        }

解决方案

You don't need all this malarkey with available(). Just set a read timeout with setSoTimeout, read, detect read returning -1, use the count returned by read if > 0 rather than assuming the buffer got filled, catch SocketTimeoutException to detect read timeouts, and catch IOException to detect other breakages.

这篇关于知道如何在读取套接字数据(InputStream)时检测到断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆