简体   繁体   English

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

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

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. 我有一种情况,一个线程打开一个到目标m / c的telnet连接,并从程序中读取数据,该程序吐出缓冲区中的所有数据。 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). 有时,目标程序不打印任何标记,它继续转储数据,我的线程继续读取它(目标程序没有打印标记)。

So i want to read the data only for a specific period of time (say 15 mins/configurable). 所以我想只在特定的时间段内读取数据(例如15分钟/可配置)。 Is there any way to do this at the java API level? 有没有办法在java API级别执行此操作?

Use another thread to close the connection after 15 mins. 15分钟后使用另一个线程关闭连接。 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). 或者,您可以在每次读取后检查是否已经过了15分钟,然后只是停止读取并清除连接,但这只有在您确定远程服务器将继续发送数据时才会起作用(如果读取不会阻止无限期)。

Generally, no. 一般来说,没有。 Input streams don't provide timeout functinality. 输入流不提供超时功能。

However, in your specific case, that is, reading data from a socket, yes. 但是,在您的特定情况下,即从套接字读取数据,是的。 What you need to do is set the SO_TIMEOUT on your socket to a non-zero value (the timeout you need in millisecs). 您需要做的是将套接字上的SO_TIMEOUT设置为非零值(以毫秒为单位的超时)。 Any read operations that block for the amount of time specified will throw a SocketTimeoutException . 阻塞指定时间的任何读取操作都将抛出SocketTimeoutException

Watch out though, as even though your socket connection is still valid after this, continuing to read from it may bring unexpected result, as you've already half consumed your data. 但请注意,即使您的套接字连接在此之后仍然有效,继续读取它可能会带来意想不到的结果,因为您已经消耗了一半的数据。 The easiest way to handle this is to close the connection but if you keep track of how much you've read already, you can choose to recover and continue reading. 处理此问题的最简单方法是关闭连接,但如果您已经记录了已阅读的内容,则可以选择恢复并继续阅读。

If you're using a Java Socket for your communication, you should have a look at the setSoTimeout(int) method. 如果您使用Java Socket进行通信,则应该查看setSoTimeout(int)方法。

The read() operation on the socket will block only for the specified time. 套接字上的read()操作将仅在指定的时间内阻塞。 After that, if no information is received, a java.net.SocketTimeoutException will be raised and if treated correctly, the execution will continue. 之后,如果没有收到任何信息,将引发java.net.SocketTimeoutException ,如果处理正确,执行将继续。

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. 如果客户端可以在同步读取中被阻塞,等待服务器输出内容,那么您可以使用SocketChannel ,并启动一个中断主读取线程的计时器线程,或关闭其输入,或关闭通道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM