1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.qotm;
17
18 import java.net.InetSocketAddress;
19 import java.util.concurrent.Executors;
20
21 import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
22 import org.jboss.netty.channel.ChannelPipeline;
23 import org.jboss.netty.channel.ChannelPipelineFactory;
24 import org.jboss.netty.channel.Channels;
25 import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory;
26 import org.jboss.netty.channel.socket.DatagramChannel;
27 import org.jboss.netty.channel.socket.DatagramChannelFactory;
28 import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
29 import org.jboss.netty.handler.codec.string.StringDecoder;
30 import org.jboss.netty.handler.codec.string.StringEncoder;
31 import org.jboss.netty.util.CharsetUtil;
32
33
34
35
36
37
38
39
40
41
42
43 public class QuoteOfTheMomentClient {
44
45 public static void main(String[] args) throws Exception {
46 DatagramChannelFactory f =
47 new NioDatagramChannelFactory(Executors.newCachedThreadPool());
48
49 ConnectionlessBootstrap b = new ConnectionlessBootstrap(f);
50
51
52 b.setPipelineFactory(new ChannelPipelineFactory() {
53 public ChannelPipeline getPipeline() throws Exception {
54 return Channels.pipeline(
55 new StringEncoder(CharsetUtil.ISO_8859_1),
56 new StringDecoder(CharsetUtil.ISO_8859_1),
57 new QuoteOfTheMomentClientHandler());
58 }
59 });
60
61
62 b.setOption("broadcast", "true");
63
64
65
66
67
68
69
70
71
72
73
74 b.setOption(
75 "receiveBufferSizePredictorFactory",
76 new FixedReceiveBufferSizePredictorFactory(1024));
77
78 DatagramChannel c = (DatagramChannel) b.bind(new InetSocketAddress(0));
79
80
81 c.write("QOTM?", new InetSocketAddress("255.255.255.255", 8080));
82
83
84
85
86 if (!c.getCloseFuture().awaitUninterruptibly(5000)) {
87 System.err.println("QOTM request timed out.");
88 c.close().awaitUninterruptibly();
89 }
90
91 f.releaseExternalResources();
92 }
93 }