Java socket non blocking read

I am using datainputstream to read characters / data from the socket

I want to use it readUnsignedShort(); If there are no 2 bytes to read, an exception is thrown Should I inherit datainputstream and override the method of adding exceptions, or is there a simpler method?

Solution

If you want something fast and dirty, try InputStream available().

if (stream.available() < 2) {
    // throw it
}

If you want real non blocking reads and callbacks when data is available, I think Pablo's answer is better

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>