Hi Hez,

1. Are you expecting the line

acceptor.bind(new InetSocketAddress(port));

to block until an incoming connection is accepted? (i.e. similar to
ServerSocket.accept())

2. or are you saying that, after the line is executed, the programme terminates
completely, and no longer listens on that port? (Check netstat)

if (1), well actually, this non-blocking is actually a feature of MINA (and
Java NIO). After the bind, the execution continues into the next line, and
"event" related to this bound port will be handled by a Handler object (similar
to event listeners and message-driven beans.) What you can do is to implement a
subclass of org.apache.mina.core.service.IoHandlerAdapter, e.g.

static class TestHandler extends IoHandlerAdapter {
    
    @Override
    public void messageReceived(IoSession session, Object message) {
        
        System.out.println("Message Received");
    }
    
    @Override
    public void sessionOpened(IoSession session) {
        
        System.out.println("Incoming Connection Accepted");
    }
}

then, in your code segment,

acceptor.setHandler(new TestHandler());

before binding the acceptor to the port.



HTH,
Edwin



--- hezjing <[EMAIL PROTECTED]> wrote:

> Hi
> I have the following UDP server code,
> 
>         NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
>         // temporary set to IoHandlerAdapter ...
>         acceptor.setHandler(new IoHandlerAdapter());
> 
>         DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
>         chain.addLast("logger", new LoggingFilter());
> 
>         DatagramSessionConfig sessionConfig = acceptor.getSessionConfig();
>         sessionConfig.setReuseAddress(true);
> 
>         acceptor.bind(new InetSocketAddress(port));
> 
> 
> The problem now is the code is executed successfully and terminated after
> the bind().
> I think the program should stay and listen to the specified port after the
> bind()?



      Try cool new emoticons, skins, plus more space for friends. 
Download Yahoo! Messenger Singapore now!
http://sg.messenger.yahoo.com

Reply via email to