Is there a way to read the inputstream for a specific amount of time?

后端 未结 4 1913
清歌不尽
清歌不尽 2021-01-19 18:18

I\'ve a situation where a thread opens a telnet connection to a target m/c and reads the data from a program which spits out the all the data in its buffer. After all the da

4条回答
  •  梦毁少年i
    2021-01-19 19:03

    If the server really dumps data forever, the client will never be blocked in a read operation. You might thus regularly check (between reads) if the current time minus the start time has exceeded your configurable delay, and stop reading if it has.

    If the client can be blocked in a synchronous read, waiting for the server to output something, then you might use a SocketChannel, and start a timer thread that interrupts the main reading thread, or shuts down its input, or closes the channel.

提交回复
热议问题