1
votes

I am having java server and just one client at the time.

  • client connects and sends card ID (blocking read on server side is suitable, because only 1 client at the time)
  • if card doesn't exist in database it just sends back 0 and close the socket (no problem)
  • if card does exist sends back 1
  • now client has to send PIN to the server, but there has to be some timeout, let's say 10s. Here i cannot use blocking read, what should i do? Socket setSoTimeout is not an option, because first read is blocking but second one should not be.
3
Maybe you shuold consider threading. - px5x2
Can you share some code so we can see how your doing things? If you just want your program to wait ten seconds before doing something particular you could use: - ObedMarsh

3 Answers

0
votes

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

0
votes

The server should always use a read timeout. You can vary it after the first request and response.

0
votes

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: