I am having java server and just one client at the time.
My advice is to create a ExecutorService and start a thread with it.
There is a example here : 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
. It's read
method reads into a 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
.
It is a lot harder to use though - there is no InputStream
and you need to manage the 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. Closing the socket will interrupt a blocked reader.
Some older questions to help you with the SocketChannel if you want to go that way:
The server should always use a read timeout. You can vary it after the first request and response.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.