File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes NIO Server: listening on client's side socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "NIO Server: listening on client Watch "NIO Server: listening on client New topic
Author

NIO Server: listening on client's side socket

Piotrek Tempes
Greenhorn

Joined: Oct 19, 2009
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Piotrek Tempes
Greenhorn

Joined: Oct 19, 2009
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

A Selector is not a server mechanism, it is a listener mechanism. You can register any SelectableChannel at a Selector, including the client SocketChannel.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: NIO Server: listening on client's side socket