Thread.sleep (2000); } else { System.out.println (”Incoming connection
Thread.sleep (2000); } else { System.out.println (”Incoming connection from: ” + sc.socket().getRemoteSocketAddress()); buffer.rewind(); sc.write (buffer); sc.close(); } } } } The final method listed previously, validOps(), is used with selectors. Selectors are discussed in detail in Chapter 4, and validOps() is covered in that discussion. 3.5.3 SocketChannel Let’s move on to SocketChannel, which is the most commonly used socket channel class: public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel { // This is a partial API listing public static SocketChannel open() throws IOException public static SocketChannel open (InetSocketAddress remote) throws IOException public abstract Socket socket(); public abstract boolean connect (SocketAddress remote) throws IOException; public abstract boolean isConnectionPending(); public abstract boolean finishConnect() throws IOException; public abstract boolean isConnected(); public final int validOps() } The Socket and SocketChannel classes encapsulate point-to-point, ordered network connections similar to those provided by the familiar TCP/IP connections we all know and love. A SocketChannel acts as the client, initiating a connection to a listening server. It cannot receive until connected and then only from the address to which the connection was made. (As with ServerSocketChannel, discussion of the validOps() method will be deferred to Chapter 4 when we examine selectors. The common read/write methods are not listed here either; refer to the section Section 3.1.2 for details.) Every SocketChannel object is created in tandem with a peer java.net.Socket object. The static open() method creates a new SocketChannel object. Invoking socket() on the new 107
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services