public abstract boolean isBlocking(); public abstract Object blockingLock();
Monday, October 30th, 2006serverChannel.configureBlocking (false); socket = serverChannel.accept(); serverChannel.configureBlocking (prevState); } // lock is now released, mode is allowed to change if (socket != null) { doSomethingWithTheSocket (socket); } 3.5.2 ServerSocketChannel Let’s begin the discussion of the socket channels classes with the simplest: ServerSocketChannel. This is the complete API of ServerSocketChannel: public abstract class ServerSocketChannel extends AbstractSelectableChannel { public static ServerSocketChannel open() throws IOException public abstract ServerSocket socket(); public abstract ServerSocket accept() throws IOException; public final int validOps() } The ServerSocketChannel class is a channel-based socket listener. It performs the same basic task as the familiar java.net.ServerSocket but adds channel semantics, including the ability to operate in nonblocking mode. Create a new ServerSocketChannel object with the static open() factory method, which returns a channel associated with an unbound java.net.ServerSocket object. This peer ServerSocket can be obtained by invoking the socket() method on the returned ServerSocketChannel object. The ServerSocket objects created as peers of ServerSocketChannels are tied to the channel implementation. They are sockets whose associated SocketImpl knows about channels. Channels cannot be wrapped around arbitrary Socket objects. Because ServerSocketChannel doesn’t have a bind() method, it’s necessary to fetch the peer socket and use it to bind to a port to begin listening for connections. Also use the peer ServerSocket API to set other socket options as needed. ServerSocketChannel ssc = ServerSocketChannel.open(); ServerSocket serverSocket = ssc.socket(); // Listen on port 1234 serverSocket.bind (new InetSocketAddress (1234)); The ServerSocketChannel has an accept() method, as does its peer java.net.ServerSocket object. Once you’ve created a ServerSocketChannel and used the peer socket to bind it, you can then invoke accept() on either. If you choose to invoke accept() on the ServerSocket, it will behave the same as any other ServerSocket: always blocking and 105
Hint: This post is supported by Gama web hosting hrvatska services