UDP server with 2 ports (one for read and one for write) - getting hold of a connected SocketAddress ?

"이희승 (Trustin Lee)" trustin at gmail.com
Thu Nov 11 04:09:45 EST 2010


Jerome Lacoste wrote:
> My questions summarizes to: how to I get hold of a connected
> SocketAddress when no communication has yet taken place on that
> handler ?
> 
> I managed to make a simple UDP server/client and a pipeline that
> encode/decode messages. The server binds to a port, the client
> connects to it, sends a message and the server replies to the client
> which prints it out. All fine.
> 
> I am now trying to create a server with 2 ports. One for reading
> incoming messages, one for writing outgoing messages. Just trying to
> simulate a piece of low level hardware I am supposed to talk to.
> 
> Unfortunately in all my attempts I don't manage to find a Channel I
> can write to. I need the RemoteAddress of the client, but as I haven't
> received a message from it on the write channel. So it seems there's
> no way to get the connected Socket. I've tried to reuse the Channel
> obtained after the bind, but (obviously?) that one isn't connected
> (even after the client has connected to it).

To send a message, a datagram channel doesn't need to be connected - use
Channel.write(Object, SocketAddress);

    ch.write(msg, destination);

Once you receive a message from the other port, you will be able to get
the remote address of the peer.  Then you can store it somewhere like
handler's member variable, and use it later when you call
ch.write(Object, SocketAddress).

HTH

-- 
Trustin Lee - http://gleamynode.net/


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


More information about the netty-users mailing list