forked from csm19910701/java-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloClient.java
More file actions
41 lines (35 loc) · 1.36 KB
/
Copy pathHelloClient.java
File metadata and controls
41 lines (35 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package netty;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
/**
* Created by 李恒名 on 2017/8/8.
*/
public class HelloClient {
public static void main(String args[]) {
// Client服务启动器
ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// 设置一个处理服务端消息和各种消息事件的类(Handler)
bootstrap.setPipelineFactory(() -> Channels.pipeline(new HelloClientHandler()));
// 连接到本地的8000端口的服务端
bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000));
}
private static class HelloClientHandler extends SimpleChannelHandler {
/**
* 当绑定到服务端的时候触发,打印"Hello world, I am client."
*
* @alia OneCoder
* @author lihzh
*/
@Override
public void channelConnected(ChannelHandlerContext ctx,
ChannelStateEvent e) {
System.out.println("Hello world, I am client.");
}
}
}