有没有办法在特定的时间内读取输入流? [英] Is there a way to read the inputstream for a specific amount of time?

查看:127
本文介绍了有没有办法在特定的时间内读取输入流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这样一种情况:线程打开到目标m / c的telnet连接,并从程序中读取数据,该程序吐出缓冲区中的所有数据。刷新完所有数据后,目标程序将打印一个标记。我的线程一直在寻找这个标记来关闭连接(成功读取)。

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 data is flushed out, the target program prints a marker. My thread keeps looking for this marker to close the connection (successful read).

有时,目标程序不会打印任何标记,它会继续转储数据和我的线程继续阅读它(目标程序没有打印标记)。

Some times, the target program does not print any marker, it keeps on dumping the data and my thread keeps on reading it (no marker is printed by the target program).

所以我想在特定的时间段内读取数据(比如说15分钟) /配置)。有没有办法在java API级别执行此操作?

So i want to read the data only for a specific period of time (say 15 mins/configurable). Is there any way to do this at the java API level?

推荐答案

使用另一个线程在15分钟后关闭连接。或者,您可以在每次读取后检查是否已经过了15分钟,然后只是停止读取并清除连接,但这只有在您确定远程服务器将继续发送数据时才会起作用(如果不会读取将阻止无限期地。)

Use another thread to close the connection after 15 mins. Alternatively, you could check after each read if 15mins have passed and then simply stop reading and cleanup the connection, but this would only work if you're sure the remote server will continue to send data (if it doesn't the read will block indefinitely).

这篇关于有没有办法在特定的时间内读取输入流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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