1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.example.haproxy;
18
19 import io.netty.bootstrap.Bootstrap;
20 import io.netty.buffer.Unpooled;
21 import io.netty.channel.Channel;
22 import io.netty.channel.EventLoopGroup;
23 import io.netty.channel.MultiThreadIoEventLoopGroup;
24 import io.netty.channel.nio.NioIoHandler;
25 import io.netty.channel.socket.nio.NioSocketChannel;
26 import io.netty.handler.codec.haproxy.HAProxyCommand;
27 import io.netty.handler.codec.haproxy.HAProxyMessage;
28 import io.netty.handler.codec.haproxy.HAProxyProtocolVersion;
29 import io.netty.handler.codec.haproxy.HAProxyProxiedProtocol;
30 import io.netty.util.CharsetUtil;
31
32 import static io.netty.example.haproxy.HAProxyServer.*;
33
34 public final class HAProxyClient {
35
36 private static final String HOST = System.getProperty("host", "127.0.0.1");
37
38 public static void main(String[] args) throws Exception {
39 EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
40 try {
41 Bootstrap b = new Bootstrap();
42 b.group(group)
43 .channel(NioSocketChannel.class)
44 .handler(new HAProxyHandler());
45
46
47 Channel ch = b.connect(HOST, PORT).sync().channel();
48
49 HAProxyMessage message = new HAProxyMessage(
50 HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, HAProxyProxiedProtocol.TCP4,
51 "127.0.0.1", "127.0.0.2", 8000, 9000);
52
53 ch.writeAndFlush(message).sync();
54 ch.writeAndFlush(Unpooled.copiedBuffer("Hello World!", CharsetUtil.US_ASCII)).sync();
55 ch.writeAndFlush(Unpooled.copiedBuffer("Bye now!", CharsetUtil.US_ASCII)).sync();
56 ch.close().sync();
57 } finally {
58 group.shutdownGracefully();
59 }
60 }
61 }