• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NIO Server: listening on client's side socket

 
Piotrek Tempes
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I write out the java nio server, which is a kind of auction house. My task is to implement this with an infinite pool of items using the following definitions:

- auctioners can join the auction at any time
- The informations about the item and it's current price is send to newly come auctioners
- If the price has changed course this information is sent to all auctioners
- At the end of the auction information about the winner is mailed to all users, who participated in the auction

And the question - is there some kind of "socket listener" that would let the client know if something appeared on the socket? Do you have ideas on how we could implement it well? (Of course, always remains an infinite loop and check if there is something, but it is inelegant and inefficient)

thanks for any suggestions,
regards
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a Selector. You then call select() in an infinite loop; when this method returns you know that at least one of the channels it is listening for has data.
 
Piotrek Tempes
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid you misunderstood me... I have a selector in server's code. What I meant by asking this question was scanning the socket at client's side. Unless You suggest to make another selector in client's code... Is it possible? If I make a selector and socket channels in at client's side then wouldn't the client be another server? Is that what you meant? If so, then how to bind corresponding socket channels? Because I will have couple of clients and each of them will have exactly one socket connected to corresponding server's socket channel.
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Selector is not a server mechanism, it is a listener mechanism. You can register any SelectableChannel at a Selector, including the client SocketChannel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic