UDP Client/Server

"이희승 (Trustin Lee)" trustin at gmail.com
Mon Jun 1 00:17:43 EDT 2009


Please note that UDP is a connectionless transport.  Your channel did
bind but didn't connect to a remote address, so
Channel.getRemoteAddress() will return null because it's not connected
at all.  It's exactly same with how java.net.DatagramSocket works.  Once
packet is received, it does have an information about where it came from
so that we know the remote address from the MessageEvent.

And thanks for reporting the issue with OioDatagramChannel.connect().
I've just checked in the fix:

  * https://jira.jboss.org/jira/browse/NETTY-163

Trustin

On 31-May-2009 07:07, Jiang Bian wrote:
> 
> Well... it's probably my bad... but I don't really get the reason.
> it's ok to use e.getRemoteAddress(), but not
> e.getChannel().getRemoteAddress()
> 
> Is it because UDP doesn't have a channel?... doesn't really make sense to
> me...
> 
> Thanks,
> 
> Jiang
> 
> 
> 
> Jiang Bian wrote:
>>
>> <p>
>> I have been scratching my head off to figure out the problem.
>> I am writing a distributed file system and trying to use netty's UDP as
>> the network layer.
>> </p>
>> <p>
>> Here is my problem:
>> I have a UDP client send a request (request to send a file including some
>> basic file information) to the server, and the server should respond with
>> and ACK.
>> </p>
>> The NodeServerHandler extends SimpleChannelHandler, and here is how its
>> setup:
>> <pre>
>>         DatagramChannelFactory f = new OioDatagramChannelFactory(Executors
>> 		.newCachedThreadPool());
>>
>> 	b = new ConnectionlessBootstrap(f);
>> 	ChannelPipeline p = b.getPipeline();
>> 	p.addLast("handler", new NodeServerHandler(this.node));
>>
>> 	b.setOption("broadcast", "false");
>> 	b.setOption("sendBufferSize", 65536);
>> 	b.setOption("receiveBufferSize", 65536);
>>
>>         b.bind(new InetSocketAddress(this.port));
>> </pre>
>>
>> Here is the client part:
>> <pre>
>>         f = new
>> OioDatagramChannelFactory(Executors.newCachedThreadPool());
>>
>> 	b = new ConnectionlessBootstrap(f);
>> 	ChannelPipeline p = b.getPipeline();
>> 	p.addLast("logger", new LoggingHandler());
>> 	p.addLast("handler", this);
>> 	b.setOption("broadcast", "false");
>> 	b.setOption("sendBufferSize", 65536);
>> 	b.setOption("receiveBufferSize", 65536);
>>
>> 	c = (DatagramChannel) b.bind(new InetSocketAddress(0));
>> </pre>
>> ...
>>
>> sending a request from the client:
>> <pre>
>>             ChannelBuffer buffer =
>> JigDFSProtocolRequestFactory.requestToSendFileSlice(
>> 		    fileHash, fileHash, parentNodeID, 1, encryptionKey,
>> 		    totalNumber);
>>
>> 	    c.write(buffer, new InetSocketAddress(this.toNode
>> 		    .getHost(), this.toNode.getPort()));
>> </pre>
>> and it does go through and my server handler get the message in
>> messageReceived function, however, when I try to write back a response it
>> failed, and I find out the problem is here:
>> <pre>
>> ChannelBuffer buffer = JigDFSProtocolRequestFactory.ackSaveSliceReq();
>> e.getChannel().write(buffer, e.getChannel().getRemoteAddress());
>> </pre>
>> and the problem is caused by:
>> <pre>
>> e.getChannel().getRemoteAddress() returns NULL... 
>> </pre>
>> I can't figure out why it's none... and also, I was trying to do 
>> <pre>
>> c = (DatagramChannel) b.bind(new InetSocketAddress(0));
>> c.connect(new InetSocketAddress(this.toNode
>> 		    .getHost(), this.toNode.getPort()));
>> </pre>
>> <p>but it hangs on the connect() forever...
>> I know it's not necessary to connect, but why it doesn't work?
>> </p>
>>
>> <p>
>> I did built my code based on the Quote Sever example...but...
>> </p>
>>
>> <p>
>> Also, I know UDP in netty is not NIO, but is there any plan to have NIO
>> UDP in the future and any timelines?
>> </p>
>>
>> <p>
>> Any suggestion will be appreciated! 
>> Thanks very much in advance!
>> </p>
>> Jiang
>>
>>
>>
>>
>>
> 


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


More information about the netty-users mailing list