有没有办法在特定的时间内读取输入流?

编程入门 行业动态 更新时间:2024-10-22 09:44:41
本文介绍了有没有办法在特定的时间内读取输入流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到这样一种情况:线程打开到目标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).

更多推荐

有没有办法在特定的时间内读取输入流?

本文发布于:2023-11-27 07:21:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间内   没有办法   在特定

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!