users@grizzly.java.net

Re: Seeking Some Guidance on Using Grizzly / NIO

From: Edwin Lee <edwin11_1979_at_yahoo.com.sg>
Date: Thu, 30 Apr 2009 11:54:22 +0800 (SGT)

Hi guys, i think i've got to take it one step at a time... :p This is what i have now... what it currently tries to do is: 1. Listen for incoming connections on port 8888 2. Whenever a new incoming connection is accepted, establish a corresponding outgoing connection (in this case, to localhost:9999). package com.ncs.amaretto.proxy.core; import java..io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; import com.sun.grizzly.Controller; import com.sun.grizzly.DefaultCallbackHandler; import com.sun.grizzly.TCPConnectorHandler; import com.sun.grizzly.TCPSelectorHandler; public final class GrizzlyApplication { public static final void main(final String[] args) throws Exception { Controller controller = new Controller(); TCPSelectorHandler selectorHandler = new MyTCPSelectorHandler(controller); selectorHandler.setPort(8888); controller.addSelectorHandler(selectorHandler); controller.start(); } private static final class MyTCPSelectorHandler extends TCPSelectorHandler { private final Controller controller; MyTCPSelectorHandler(final Controller controller) { this.controller = controller; } @Override public final SelectableChannel acceptWithoutRegistration(final SelectionKey key) throws IOException { SelectableChannel channel = super.acceptWithoutRegistration(key); System.out.println("channel.getClass().getName() = " + channel.getClass().getName()); if (channel instanceof SocketChannel) { SocketChannel socketChannel = (SocketChannel) channel; Socket socket = socketChannel.socket(); System.out.println("socket.getInetAddress().getHostAddress() = " + socket.getInetAddress().getHostAddress()); System.out.println("socket.getPort() = " + socket.getPort()); } TCPConnectorHandler connectorHandler = new TCPConnectorHandler(); connectorHandler.setController(this.controller); connectorHandler.connect((new InetSocketAddress("localhost", 9999)), (new DefaultCallbackHandler(connectorHandler)), this); SelectableChannel outChannel = connectorHandler.getUnderlyingChannel(); System.out.println("outChannel = " + outChannel); return channel; } } } Questions: 1. The line connectorHandler.connect((new InetSocketAddress("localhost", 9999)), (new DefaultCallbackHandler(connectorHandler)), this); takes very long to establish a connection (~ 10 sec). Is there anything i've done wrongly? 2. The line SelectableChannel outChannel = connectorHandler.getUnderlyingChannel(); returns outChannel as NULL. How can i get hold of the channel associated with the outgoing connection? Thanks and Regards, Edwin New Email names for you! Get the Email name you&#39;ve always wanted on the new @ymail and @rocketmail. Hurry before someone else does! http://mail.promotions.yahoo.com/newdomains/sg/