1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.qotm;
17
18 import io.netty.bootstrap.Bootstrap;
19 import io.netty.buffer.Unpooled;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelOption;
22 import io.netty.channel.EventLoopGroup;
23 import io.netty.channel.nio.NioEventLoopGroup;
24 import io.netty.channel.socket.DatagramPacket;
25 import io.netty.channel.socket.nio.NioDatagramChannel;
26 import io.netty.util.CharsetUtil;
27 import io.netty.util.internal.SocketUtils;
28
29
30
31
32
33
34
35 public final class QuoteOfTheMomentClient {
36
37 static final int PORT = Integer.parseInt(System.getProperty("port", "7686"));
38
39 public static void main(String[] args) throws Exception {
40
41 EventLoopGroup group = new NioEventLoopGroup();
42 try {
43 Bootstrap b = new Bootstrap();
44 b.group(group)
45 .channel(NioDatagramChannel.class)
46 .option(ChannelOption.SO_BROADCAST, true)
47 .handler(new QuoteOfTheMomentClientHandler());
48
49 Channel ch = b.bind(0).sync().channel();
50
51
52 ch.writeAndFlush(new DatagramPacket(
53 Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
54 SocketUtils.socketAddress("255.255.255.255", PORT))).sync();
55
56
57
58
59 if (!ch.closeFuture().await(5000)) {
60 System.err.println("QOTM request timed out.");
61 }
62 } finally {
63 group.shutdownGracefully();
64 }
65 }
66 }