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
35
36
37
38
39
40 public class EchoClient {
41
42 public static void main(String[] args) throws Exception {
43
44 if (args.length < 2 || args.length > 3) {
45 System.err.println(
46 "Usage: " + EchoClient.class.getSimpleName() +
47 " <host> <port> [<first message size>]");
48 return;
49 }
50
51
52 final String host = args[0];
53 final int port = Integer.parseInt(args[1]);
54 final int firstMessageSize;
55 if (args.length == 3) {
56 firstMessageSize = Integer.parseInt(args[2]);
57 } else {
58 firstMessageSize = 256;
59 }
60
61
62 ClientBootstrap bootstrap = new ClientBootstrap(
63 new NioClientSocketChannelFactory(
64 Executors.newCachedThreadPool(),
65 Executors.newCachedThreadPool()));
66
67
68 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
69 public ChannelPipeline getPipeline() throws Exception {
70 return Channels.pipeline(
71 new EchoClientHandler(firstMessageSize));
72 }
73 });
74
75
76 ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
77
78
79 future.getChannel().getCloseFuture().awaitUninterruptibly();
80
81
82 bootstrap.releaseExternalResources();
83 }
84 }