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.MultiThreadIoEventLoopGroup;
24 import io.netty.channel.nio.NioIoHandler;
25 import io.netty.channel.socket.DatagramPacket;
26 import io.netty.channel.socket.nio.NioDatagramChannel;
27 import io.netty.util.CharsetUtil;
28 import io.netty.util.internal.SocketUtils;
29
30
31
32
33
34
35
36 public final class QuoteOfTheMomentClient {
37
38 static final int PORT = Integer.parseInt(System.getProperty("port", "7686"));
39
40 public static void main(String[] args) throws Exception {
41
42 EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
43 try {
44 Bootstrap b = new Bootstrap();
45 b.group(group)
46 .channel(NioDatagramChannel.class)
47 .option(ChannelOption.SO_BROADCAST, true)
48 .handler(new QuoteOfTheMomentClientHandler());
49
50 Channel ch = b.bind(0).sync().channel();
51
52
53 ch.writeAndFlush(new DatagramPacket(
54 Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
55 SocketUtils.socketAddress("255.255.255.255", PORT))).sync();
56
57
58
59
60 if (!ch.closeFuture().await(5000)) {
61 System.err.println("QOTM request timed out.");
62 }
63 } finally {
64 group.shutdownGracefully();
65 }
66 }
67 }