Java – bufferedinputstream and blocking

I'm using bufferedinputstream to read from the socket The contents of bufferedinputstream are as follows:

socketInput.read(replyBuffer,7);

It is instantiated by

socketInput = new BufferedInputStream(mySocket.getInputStream());

Mysocket is defined as private socket mysocket;

Mysocket is instantiated by mysocket = new socket (IPAddress, port);

I have confirmed that mysocket is connected to my device I can send data to my device; However, I didn't receive my equipment for an unknown reason, but it's not a problem

If no data is read, I want my bufferedinputstream to return after 100ms Can bufferedinputstream be set to perform this operation? Now, it stops indefinitely

Solution

Using a buffered stream to read from a socket is usually a bad idea for this reason: if it does not see enough data to fill its internal buffer (it will be greater than 7 characters!), It will wait forever There is no way to make it time out Using socketinputstream directly, the problem disappears

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
分享
二维码
< <上一篇
下一篇>>