Thread.sleep (2000); } else { System.out.println (”Incoming connection
SocketChannel will return its peer Socket object. Calling getChannel() on that Socket returns the original SocketChannel. Although every SocketChannel object creates a peer Socket object, the reverse is not true. Socket objects created directly do not have associated SocketChannel objects, and their getChannel() methods return null. A newly created SocketChannel is open but not connected. Attempting an I/O operation on an unconnected SocketChannel object will throw a NotYetConnectedException. The socket can be connected by calling connect() directly on the channel or by calling the connect() method on the associated Socket object. Once a socket channel is connected, it remains connected until it closes. You can test whether a particular SocketChannel is currently connected by invoking the boolean isConnected() method. The second form of open(), which takes an InetSocketAddress argument, is a convenience method that connects before returning. This: SocketChannel socketChannel = SocketChannel.open (new InetSocketAddress (”somehost”, somePort)); is equivalent to this: SocketChannel socketChannel = SocketChannel.open(); socketChannel.connect (new InetSocketAddress (”somehost”, somePort)); If you choose to make the connection the traditional way by invoking connect() on the peer Socket object the traditional connection semantics apply. The thread will block until the connection is established, or until the supplied timeout expires. If you choose to make the connection by calling connect() directly on the channel, and the channel is in blocking mode (the default), the connection process is effectively the same. There is no version of connect() on SocketChannel that lets you provide a timeout value. Instead, SocketChannel provides concurrent connection when connect() is invoked in nonblocking mode: it initiates a connection to the requested address then returns immediately. If the return value from connect() is true, the connection was established immediately (this may happen for local loopback connections). If the connection cannot be established immediately, connect() will return false, and connection establishment proceeds concurrently. Stream-oriented sockets take time to set up because a packet dialog must take place between the two connecting systems to establish the state information needed to maintain the stream socket. Connecting to remote systems across the open Internet can be especially time-consuming. If a concurrent connection is underway on a SocketChannel, the isConnectPending() method returns true. 108
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services