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