[英]Java server socket data input stream read timeout
I am having java server and just one client at the time. 我当时有Java服务器,只有一个客户端。
My advice is to create a ExecutorService and start a thread with it. 我的建议是创建一个ExecutorService并启动一个线程。
There is a example here : http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html 这里有一个示例: http : //docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
The correct (but not the easiest) way to to this is to use java.nio.channels.SocketChannel
. 正确的方法(但不是最简单的方法)是使用
java.nio.channels.SocketChannel
。 It's read
method reads into a ByteBuffer
. 它的
read
方法读入ByteBuffer
。 You combine it with a java.nio.channels.Selector
to read from multiple sockets without blocking (the selector helps you find out which one has data available) but in your case you may simply be happy with the SocketChannel
. 您可以将其与
java.nio.channels.Selector
结合使用,以不受阻塞地从多个套接字读取(选择器可帮助您找到哪个套接字有可用数据),但对于您而言,您可能对SocketChannel
感到满意。
It is a lot harder to use though - there is no InputStream
and you need to manage the ByteBuffer
. 但是,使用起来要困难得多-没有
InputStream
,您需要管理ByteBuffer
。
Another alternative is to start a watchdog Thread
that sleeps for the duration of your timeout and then closes the Socket
if the client hasn't sent the PIN yet. 另一种选择是启动一个监视程序
Thread
,该Thread
在超时期间一直处于休眠状态,如果客户端尚未发送PIN,则关闭Socket
。 Closing the socket will interrupt a blocked reader. 关闭插槽将中断读取器的阻塞。
Some older questions to help you with the SocketChannel if you want to go that way: 如果您想通过一些更老的问题来帮助您使用SocketChannel,请执行以下操作:
The server should always use a read timeout. 服务器应始终使用读取超时。 You can vary it after the first request and response.
您可以在第一次请求和响应后更改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.