UDP Server send consecutive back to client

382 views
Skip to first unread message

Tuan Nguyen

unread,
Jul 25, 2014, 1:47:01 PM7/25/14
to ne...@googlegroups.com
Hi everybody
I'm try to setup a simple UDP server-client, so far, i can send msg and get response from server.
However, i want to server to keep track of client and send data whenever it wants. Since UDP is connection-less, so i keep the Socketaddress from datagrampacket.

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    System.out.println(packet.sender().toString());
}

But from here, how can i setup an system to send data to client through socketaddress?
Thanks

Alexander Gehret

unread,
Jul 26, 2014, 2:32:52 AM7/26/14
to ne...@googlegroups.com
As I understand it you just want to send Data? :) Then you can just use
ctx.writeandflush()

Or did I get anything wrong and udp is handled different from tcp?

signature.asc

Alexander Gehret

unread,
Jul 26, 2014, 2:32:55 AM7/26/14
to ne...@googlegroups.com
Otherwise I guess you could just send something like this

channel.write(new NettyElementInfo(), 
                new InetSocketAddress("255.255.255.255", 9555));
(and replace the broadcast adress of course with your correct destination address)
signature.asc

Tuan Nguyen

unread,
Jul 26, 2014, 4:19:33 AM7/26/14
to ne...@googlegroups.com
Thank for your reply Alexander
The ChannelContextHandler only available when server receive msg from client, and i can use it to write msg back to client, ofc.
However, when want to tell client about some update information, i don't know how to do it.

What you suggest here is create a new Channel to send data? Can you give me some snippet code please?  Thanks

THUFIR HAWAT

unread,
Jul 27, 2014, 5:57:59 PM7/27/14
to ne...@googlegroups.com
you have:


@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
System.out.println(packet.sender().toString());
}


in your handler.

why don't you create new datagram as described in a previous post?


private DatagramPacket getNext(String host, int port) {
DatagramPacket packet = new DatagramPacket(
Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
new InetSocketAddress(host, port));
return packet;
}

and then your handler can just write it to wherever you like. This is adapted from the handler for the QOTM (Quote of the Moment):

https://github.com/netty/netty/tree/master/example/src/main/java/io/netty/example


while it's from the client handler, it could just as easily go in your server handler.


-Thufir

Tuan Nguyen

unread,
Jul 28, 2014, 1:44:34 AM7/28/14
to ne...@googlegroups.com
THUFIR, Thank for your reply
Yes, i can create a new DatagramPacket with the InetAddress i got, however, i confuse with the way to send it back to client WITHOUT ChannelHandlerContext. Since i want to send data back to client whenever i want, not when i get a msg.

Btw, is this group take too much to get approve post?

Alexander Gehret

unread,
Jul 29, 2014, 10:24:09 PM7/29/14
to ne...@googlegroups.com
You could as well just save the ctx in something like an array or
hashmap, like "connected clients" and then use all ctx to write back to
each client.

signature.asc

THUFIR HAWAT

unread,
Jul 30, 2014, 5:11:59 AM7/30/14
to ne...@googlegroups.com
The QOTM server sends a datagram, outside of the handler. HTH
Reply all
Reply to author
Forward
0 new messages