相当于切断套接字的字节link

The byte equivalent to cut the socket link

通常当我使用

r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));

那我用

int nextChar = 0;
while ((nextChar=r.read()) != -1) 
{   

}

现在我要使用字节级

r = new DataInputStream(new BufferedInputStream(receivedSocketConn1.getInputStream()));

所以我读到的第一个字节是这个

try {
    while(true){
        if (r.readByte() != 0x7E) // start byte
        {
            // ah oh, something went wrong!!
            receivedSocketConn1.close();
            return;
        }

        int bodyLen = r.readUnsignedShort();       // message body nature (body length)

        byte serialNum1 = r.readByte();// message serial number
        byte[] messageBody = new byte[20];    // message body
        r.readFully(messageBody);

        if (r.readByte() != 0x7E) // end byte
        {
            // ah oh, something went wrong!!
            receivedSocketConn1.close();
            return;
        }
    }
}
catch (SocketTimeoutException ex)  
{ 
    System.out.println("SocketTimeoutException has been caught");
    ex.printStackTrace();
}  
catch (IOException ex)  
{ 
    System.out.println("IOException has been caught");
    ex.printStackTrace();
} 
finally
{
    try 
    {
        if ( w != null ) 
        {
            w.close();
            r.close();
            receivedSocketConn1.close();
        }
        else 
        {
            System.out.println("MyError:w is null in finally close");
        }
    }
}

你看到它总是在我执行 7e 之后进入 finally 块,然后它切割 link。我想保留 link 一段时间。但在此之前,我想 运行 一个 for 循环来查找此场景中的 -1。我该如何实施?

这种情况下你不需要处理-1

如果您阅读文档,它会说:

InputStream::read()

Returns:
the next byte of data, or -1 if the end of the stream is reached.

DataInputStream::readByte()

Throws:
EOFException - if this input stream has reached the end.
IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

所有 DataInputStream 阅读方法也是如此。

因此,您所要做的就是正常地从 DataInputStream 中读取值,并在套接字被对等方关闭时让它抛出异常。你已经在这样做了(EOFException extends IOException 并且会被卡在你的 catch (IOException ex) block 中)。你想多了。

也就是说,如果读取 0x7E 字节抛出异常(哪个 readByte() 调用失败?抛出哪个异常?),那么你做错了什么。例如,此问题基于 code I gave you yesterday for another question,但您在此问题中显示的代码基于早期代码是不完整的。该遗漏很容易导致第二个 if (r.readByte() != 0x7E) 评估为 false 并关闭连接。

尝试更像这样的东西:

w = new DataOutputStream(new BufferedOutputStream(receivedSocketConn1.getOutputStream()));
r = new DataInputStream(new BufferedInputStream(receivedSocketConn1.getInputStream()));

try
{
    while(true)
    {
        if (r.readByte() != 0x7E) // start byte
            throw new RuntimeException("Incorrect start byte detected");

        int messageID = r.readUnsignedShort();     // message ID
        int bodyLen = r.readUnsignedShort();       // message body nature (body length)
        byte[] phoneNum = new byte[6];
        r.readFully(phoneNum);                     // device phone number
        int serialNum = r.readUnsignedShort();     // message serial number
        byte[] messageBody = new byte[bodyLen];    // message body
        r.readFully(messageBody);
        byte checkCode = r.readByte();             // check code

        if (r.readByte() != 0x7E) // end byte
            throw new RuntimeException("Incorrect end byte detected");

        // TODO: validate checkCode if needed...
        // ... 
        // if (checkCode is not valid)
        //    throw new RuntimeException("Bad checkCode value");

        // process message data as needed...
    }
}
catch (SocketTimeoutException ex)  
{ 
    System.out.println("SocketTimeoutException has been caught");
    ex.printStackTrace();
}  
catch (EOFException ex)  
{ 
    System.out.println("Socket has been closed");
}  
catch (IOException ex)  
{ 
    System.out.println("IOException has been caught");
    ex.printStackTrace();
} 
catch (RuntimeException ex)
{ 
    System.out.println(ex.getMessage());
    ex.printStackTrace();
} 
finally
{
    w.close();
    r.close();
    receivedSocketConn1.close();
}