1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.discard;
17
18 import java.net.InetSocketAddress;
19 import java.util.concurrent.Executors;
20
21 import org.jboss.netty.bootstrap.ClientBootstrap;
22 import org.jboss.netty.channel.ChannelFuture;
23 import org.jboss.netty.channel.ChannelPipeline;
24 import org.jboss.netty.channel.ChannelPipelineFactory;
25 import org.jboss.netty.channel.Channels;
26 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
27
28
29
30
31 public class DiscardClient {
32
33 private final String host;
34 private final int port;
35 private final int firstMessageSize;
36
37 public DiscardClient(String host, int port, int firstMessageSize) {
38 this.host = host;
39 this.port = port;
40 this.firstMessageSize = firstMessageSize;
41 }
42
43 public void run() {
44
45 ClientBootstrap bootstrap = new ClientBootstrap(
46 new NioClientSocketChannelFactory(
47 Executors.newCachedThreadPool(),
48 Executors.newCachedThreadPool()));
49
50
51 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
52 public ChannelPipeline getPipeline() throws Exception {
53 return Channels.pipeline(
54 new DiscardClientHandler(firstMessageSize));
55 }
56 });
57
58
59 ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
60
61
62 future.getChannel().getCloseFuture().awaitUninterruptibly();
63
64
65 bootstrap.releaseExternalResources();
66 }
67
68 public static void main(String[] args) throws Exception {
69
70 if (args.length < 2 || args.length > 3) {
71 System.err.println(
72 "Usage: " + DiscardClient.class.getSimpleName() +
73 " <host> <port> [<first message size>]");
74 return;
75 }
76
77
78 final String host = args[0];
79 final int port = Integer.parseInt(args[1]);
80 final int firstMessageSize;
81 if (args.length == 3) {
82 firstMessageSize = Integer.parseInt(args[2]);
83 } else {
84 firstMessageSize = 256;
85 }
86
87 new DiscardClient(host, port, firstMessageSize).run();
88 }
89 }