Created
June 16, 2014 13:47
-
-
Save crised/55753298c58b03d82d6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FlushHandler extends ChannelOutboundHandlerAdapter { | |
@Override | |
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { | |
ctx.flush(); | |
System.out.println("Flushed"); | |
} | |
} | |
public class OutboundHandler extends ChannelOutboundHandlerAdapter { | |
@Override | |
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception { | |
System.out.println("Bind happened"); | |
System.out.println(localAddress); | |
InetSocketAddress ipSocketAddress = new InetSocketAddress("255.255.255.255", 7686); | |
ByteBuf byteBuf = Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8); | |
DatagramPacket udpPacket = new DatagramPacket(byteBuf, ipSocketAddress); | |
ctx.write(udpPacket); | |
} | |
} | |
public class MyChannelInitializer extends ChannelInitializer { | |
@Override | |
protected void initChannel(Channel channel) throws Exception { | |
ChannelPipeline pipeline = channel.pipeline(); | |
pipeline.addLast("SecondOutboundHandler", new FlushHandler()); | |
pipeline.addLast("FirstOutboundHandler", new OutboundHandler()); | |
} | |
} | |
public class Client { | |
public static void main(String[] args) throws Exception { | |
EventLoopGroup group = new NioEventLoopGroup(); | |
try { | |
Bootstrap b = new Bootstrap(); | |
b.group(group) | |
.channel(NioDatagramChannel.class) | |
.option(ChannelOption.SO_BROADCAST, true) | |
.handler(new MyChannelInitializer()); | |
b.bind(7000); | |
} finally { | |
group.shutdownGracefully(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment