1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.objectecho;
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.ChannelPipeline;
23 import org.jboss.netty.channel.ChannelPipelineFactory;
24 import org.jboss.netty.channel.Channels;
25 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
26 import org.jboss.netty.example.echo.EchoClient;
27 import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
28 import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
29
30
31
32
33
34
35
36
37
38 public class ObjectEchoClient {
39
40 public static void main(String[] args) throws Exception {
41
42 if (args.length < 2 || args.length > 3) {
43 System.err.println(
44 "Usage: " + ObjectEchoClient.class.getSimpleName() +
45 " <host> <port> [<first message size>]");
46 return;
47 }
48
49
50 final String host = args[0];
51 final int port = Integer.parseInt(args[1]);
52 final int firstMessageSize;
53
54 if (args.length == 3) {
55 firstMessageSize = Integer.parseInt(args[2]);
56 } else {
57 firstMessageSize = 256;
58 }
59
60
61 ClientBootstrap bootstrap = new ClientBootstrap(
62 new NioClientSocketChannelFactory(
63 Executors.newCachedThreadPool(),
64 Executors.newCachedThreadPool()));
65
66
67 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
68 public ChannelPipeline getPipeline() throws Exception {
69 return Channels.pipeline(
70 new ObjectEncoder(),
71 new ObjectDecoder(),
72 new ObjectEchoClientHandler(firstMessageSize));
73 }
74 });
75
76
77 bootstrap.connect(new InetSocketAddress(host, port));
78 }
79 }