Skip to content

Commit fc1ce39

Browse files
author
yimkong
committed
bootstrap class
1 parent d31e3e0 commit fc1ce39

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/.idea/
2+
/netty-study.iml

src/main/java/TimeClient.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1+
import io.netty.bootstrap.Bootstrap;
2+
import io.netty.channel.ChannelFuture;
3+
import io.netty.channel.ChannelInitializer;
4+
import io.netty.channel.ChannelOption;
5+
import io.netty.channel.nio.NioEventLoopGroup;
6+
import io.netty.channel.socket.SocketChannel;
7+
import io.netty.channel.socket.nio.NioSocketChannel;
8+
19
/**
210
* author yg
311
* description
412
* date 2020/1/28
513
*/
614
public class TimeClient {
7-
public static void main(String[] args) {
15+
private void connect(int port, String host) throws Exception {
16+
NioEventLoopGroup group = new NioEventLoopGroup();
17+
try {
18+
Bootstrap b = new Bootstrap();
19+
b.group(group)
20+
.channel(NioSocketChannel.class)
21+
.option(ChannelOption.TCP_NODELAY, true)
22+
.handler(new ChannelInitializer<SocketChannel>() {
23+
@Override
24+
protected void initChannel(SocketChannel ch) throws Exception {
25+
ch.pipeline().addLast(new TimeClientHandler());
26+
}
27+
});
28+
ChannelFuture sync = b.connect(host, port).sync();
29+
sync.channel().closeFuture().sync();
30+
} finally {
31+
group.shutdownGracefully();
32+
}
33+
34+
}
835

36+
public static void main(String[] args) throws Exception {
37+
int port = 8080;
38+
new TimeClient().connect(port,"localhost");
939
}
1040
}

src/main/java/TimeServer.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1+
import io.netty.bootstrap.ServerBootstrap;
2+
import io.netty.channel.ChannelFuture;
3+
import io.netty.channel.ChannelInitializer;
4+
import io.netty.channel.ChannelOption;
5+
import io.netty.channel.nio.NioEventLoopGroup;
6+
import io.netty.channel.socket.SocketChannel;
7+
import io.netty.channel.socket.nio.NioServerSocketChannel;
8+
19
/**
210
* author yg
311
* description
412
* date 2020/1/28
513
*/
614
public class TimeServer {
7-
public static void main(String[] args) {
15+
private void bind(int port) throws Exception{
16+
NioEventLoopGroup boss = new NioEventLoopGroup();
17+
NioEventLoopGroup worker = new NioEventLoopGroup();
18+
try {
19+
ServerBootstrap b = new ServerBootstrap();
20+
b.group(boss, worker)
21+
.channel(NioServerSocketChannel.class)
22+
.option(ChannelOption.SO_BACKLOG, 1024)
23+
.childHandler(new ChildChannelHandler());
24+
ChannelFuture sync = b.bind(port).sync();
25+
sync.channel().closeFuture().sync();
26+
} finally {
27+
boss.shutdownGracefully();
28+
worker.shutdownGracefully();
29+
}
30+
31+
}
32+
33+
private class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
34+
35+
@Override
36+
protected void initChannel(SocketChannel ch) throws Exception {
37+
ch.pipeline().addLast(new TimeServerHandler());
38+
}
39+
}
840

41+
public static void main(String[] args) throws Exception {
42+
int port=8080;
43+
new TimeServer().bind(port);
944
}
1045
}

0 commit comments

Comments
 (0)