List of usage examples for io.netty.channel.socket.nio NioDatagramChannel writeAndFlush
@Override
public ChannelFuture writeAndFlush(Object msg)
From source file:qotm.QuoteOfTheMomentClient.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//from w w w .jav a 2s . c o m Bootstrap b = new Bootstrap(); b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true) .handler(new ChannelInitializer<NioDatagramChannel>() { @Override protected void initChannel(NioDatagramChannel ch) throws Exception { //ch.pipeline().addLast(new UdpEncoder()); //ch.pipeline().addLast(new QuoteOfTheMomentClientHandler()); } }); Channel ch = b.bind(0).sync().channel(); while (true) { // Broadcast the QOTM request to port 8080. ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), SocketUtils.socketAddress("192.168.0.23", 124))).sync(); // Broadcast the QOTM request to port 8080. ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), SocketUtils.socketAddress("192.168.0.23", 123))).sync(); } /*ch.writeAndFlush(new DatagramPacket( Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), SocketUtils.socketAddress("192.168.0.23", 12345))).sync();*/ // QuoteOfTheMomentClientHandler will close the DatagramChannel when a // response is received. If the channel is not closed within 5 seconds, // print an error message and quit. /*if (!ch.closeFuture().await(5000)) { System.err.println("QOTM request timed out."); }*/ } finally { group.shutdownGracefully(); } }