Channel open event for UDP and TCP on bind

"Trustin Lee (이희승)" trustin at gmail.com
Wed Jan 6 08:25:37 EST 2010


Hi Frederic,

fps wrote:
> Hi
> 
> I've started using JBoss Netty recently after a 3 year spell with Apache
> MINA so there are still things I'm trying to get my head around.

Welcome! :)

> I'm experiencing a difference in behaviour for channel open events between
> UDP and TCP.
> 
> For example using the following code:
> 
> UDP (using NIO)
> ===============
> ConnectionlessBootstrap b = new ConnectionlessBootstrap(new
> NioDatagramChannelFactory(Executors.newCachedThreadPool());
> ChannelPipeline p = b.getPipeline();
> p.addLast("handler", new MyHandler());
> b.setOption("broadcast", "false");
> b.bind(new InetSocketAddress(53001));
> 
> TCP (using NIO)
> ===============
> ServerBootstrap b = new ServerBootstrap(new
> NioSocketChannelFactory(Executors.newCachedThreadPool(),
> Executors.newCachedThreadPool());
> ChannelPipeline p = b.getPipeline();
> p.addLast("handler", new MyHandler());
> b.setOption("reuseAddress", "true");
> b.bind(new InetSocketAddress(53000));
> 
> I get a channel open event triggered by the bind for the UDP version and no
> channnel open event for the TCP version...
> MyHandler defines a channelOpen(...) method which logs the event.
> 
> I don't really understand why? Is there a reason for that?

It is an expected behavior.

In a TCP server, a channel is open only when a client connects to it.
To be more precise, a server socket gets the initial channelOpen event
on bind, but it is hidden by ServerBootstrap.  What you deal with in
your handler is the channel accepted by the server socket.

In a UDP server, a channel is open immediately on bind and you can
receive and send the UDP packets without accepting anything because
there is no notion of connection in UDP.

Simply, think about writing a TCP or UDP server by yourself.  When you
write a TCP server, you accept a new socket from a server socket and
then use the accepted socket to communicate with the client.  When you
write a UDP server, you simply create a socket bound to a port and then
keep using it to communicate.

HTH,
Trustin
-- 
what we call human nature in actuality is human habit
http://gleamynode.net/


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
Url : http://lists.jboss.org/pipermail/netty-users/attachments/20100106/07a8d38b/attachment.bin 


More information about the netty-users mailing list