Skip to content

Commit c64cd0b

Browse files
committed
itstack-demo-netty fuzhengwei 微信公众号:bugstack虫洞栈
1 parent 8188dd9 commit c64cd0b

File tree

188 files changed

+9416
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+9416
-0
lines changed

itstack-demo-netty-1-01/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>itstack-demo-netty</artifactId>
7+
<groupId>org.itatack.demo</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>itstack-demo-netty-1-01</artifactId>
13+
14+
15+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.itstack.demo.netty.server;
2+
3+
import io.netty.channel.ChannelInitializer;
4+
import io.netty.channel.socket.SocketChannel;
5+
6+
/**
7+
* 虫洞栈:https://bugstack.cn
8+
* 公众号:bugstack虫洞栈 {获取学习源码}
9+
* Create by fuzhengwei on 2019
10+
*/
11+
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {
12+
13+
@Override
14+
protected void initChannel(SocketChannel channel) {
15+
System.out.println("链接报告开始");
16+
System.out.println("链接报告信息:有一客户端链接到本服务端");
17+
System.out.println("链接报告IP:" + channel.localAddress().getHostString());
18+
System.out.println("链接报告Port:" + channel.localAddress().getPort());
19+
System.out.println("链接报告完毕");
20+
}
21+
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.itstack.demo.netty.server;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelOption;
6+
import io.netty.channel.EventLoopGroup;
7+
import io.netty.channel.nio.NioEventLoopGroup;
8+
import io.netty.channel.socket.nio.NioServerSocketChannel;
9+
10+
/**
11+
* 虫洞栈:https://bugstack.cn
12+
* 公众号:bugstack虫洞栈 {获取学习源码}
13+
* Create by fuzhengwei on 2019
14+
*/
15+
public class NettyServer {
16+
17+
public static void main(String[] args) {
18+
new NettyServer().bing(7397);
19+
}
20+
21+
private void bing(int port) {
22+
//配置服务端NIO线程组
23+
EventLoopGroup parentGroup = new NioEventLoopGroup(); //NioEventLoopGroup extends MultithreadEventLoopGroup Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
24+
EventLoopGroup childGroup = new NioEventLoopGroup();
25+
try {
26+
ServerBootstrap b = new ServerBootstrap();
27+
b.group(parentGroup, childGroup)
28+
.channel(NioServerSocketChannel.class) //非阻塞模式
29+
.option(ChannelOption.SO_BACKLOG, 128)
30+
.childHandler(new MyChannelInitializer());
31+
ChannelFuture f = b.bind(port).sync();
32+
System.out.println("itstack-demo-netty server start done. {关注公众号:bugstack虫洞栈,获取源码}");
33+
f.channel().closeFuture().sync();
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
} finally {
37+
childGroup.shutdownGracefully();
38+
parentGroup.shutdownGracefully();
39+
}
40+
41+
}
42+
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.itstack.demo.netty.test;
2+
3+
/**
4+
* 虫洞栈:https://bugstack.cn
5+
* 公众号:bugstack虫洞栈 {获取学习源码}
6+
* Create by fuzhengwei on 2019
7+
*/
8+
public class ApiTest {
9+
10+
public static void main(String[] args) {
11+
System.out.println("hi!");
12+
}
13+
14+
}

itstack-demo-netty-1-02/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>itstack-demo-netty</artifactId>
7+
<groupId>org.itatack.demo</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>itstack-demo-netty-1-02</artifactId>
13+
14+
15+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.itstack.demo.netty.server;
2+
3+
import io.netty.channel.ChannelInitializer;
4+
import io.netty.channel.socket.SocketChannel;
5+
6+
/**
7+
* 虫洞栈:https://bugstack.cn
8+
* 公众号:bugstack虫洞栈 {获取学习源码}
9+
* Create by fuzhengwei on 2019
10+
*/
11+
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {
12+
13+
@Override
14+
protected void initChannel(SocketChannel channel) {
15+
16+
System.out.println("链接报告开始");
17+
System.out.println("链接报告信息:有一客户端链接到本服务端");
18+
System.out.println("链接报告IP:" + channel.localAddress().getHostString());
19+
System.out.println("链接报告Port:" + channel.localAddress().getPort());
20+
System.out.println("链接报告完毕");
21+
22+
//在管道中添加我们自己的接收数据实现方法
23+
channel.pipeline().addLast(new MyServerHandler());
24+
25+
}
26+
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.itstack.demo.netty.server;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.ChannelInboundHandlerAdapter;
6+
7+
import java.nio.charset.Charset;
8+
import java.nio.charset.StandardCharsets;
9+
import java.util.Date;
10+
11+
/**
12+
* 虫洞栈:https://bugstack.cn
13+
* 公众号:bugstack虫洞栈 {获取学习源码}
14+
* Create by fuzhengwei on 2019
15+
*/
16+
public class MyServerHandler extends ChannelInboundHandlerAdapter {
17+
18+
@Override
19+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
20+
//接收msg消息
21+
ByteBuf buf = (ByteBuf) msg;
22+
byte[] msgByte = new byte[buf.readableBytes()];
23+
buf.readBytes(msgByte);
24+
System.out.print(new Date() + "接收到消息:");
25+
System.out.println(new String(msgByte, Charset.forName("GBK")));
26+
}
27+
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.itstack.demo.netty.server;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelOption;
6+
import io.netty.channel.EventLoopGroup;
7+
import io.netty.channel.nio.NioEventLoopGroup;
8+
import io.netty.channel.socket.nio.NioServerSocketChannel;
9+
10+
/**
11+
* 虫洞栈:https://bugstack.cn
12+
* 公众号:bugstack虫洞栈 {获取学习源码}
13+
* Create by fuzhengwei on 2019
14+
*/
15+
public class NettyServer {
16+
17+
public static void main(String[] args) {
18+
new NettyServer().bing(7397);
19+
}
20+
21+
private void bing(int port) {
22+
//配置服务端NIO线程组
23+
EventLoopGroup parentGroup = new NioEventLoopGroup(); //NioEventLoopGroup extends MultithreadEventLoopGroup Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
24+
EventLoopGroup childGroup = new NioEventLoopGroup();
25+
try {
26+
ServerBootstrap b = new ServerBootstrap();
27+
b.group(parentGroup, childGroup)
28+
.channel(NioServerSocketChannel.class) //非阻塞模式
29+
.option(ChannelOption.SO_BACKLOG, 128)
30+
.childHandler(new MyChannelInitializer());
31+
ChannelFuture f = b.bind(port).sync();
32+
System.out.println("itstack-demo-netty server start done. {关注公众号:bugstack虫洞栈,获取源码}");
33+
f.channel().closeFuture().sync();
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
} finally {
37+
childGroup.shutdownGracefully();
38+
parentGroup.shutdownGracefully();
39+
}
40+
41+
}
42+
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.itstack.demo.netty.server.test;
2+
3+
/**
4+
* 虫洞栈:https://bugstack.cn
5+
* 公众号:bugstack虫洞栈 {获取学习源码}
6+
* Create by fuzhengwei on 2019
7+
*/
8+
public class ApiTest {
9+
}

itstack-demo-netty-1-03/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>itstack-demo-netty</artifactId>
7+
<groupId>org.itatack.demo</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>itstack-demo-netty-1-03</artifactId>
13+
14+
15+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.itstack.demo.netty.server;
2+
3+
import io.netty.channel.ChannelInitializer;
4+
import io.netty.channel.socket.SocketChannel;
5+
import io.netty.handler.codec.LineBasedFrameDecoder;
6+
import io.netty.handler.codec.string.StringDecoder;
7+
8+
import java.nio.charset.Charset;
9+
10+
/**
11+
* 虫洞栈:https://bugstack.cn
12+
* 公众号:bugstack虫洞栈 {获取学习源码}
13+
* Create by fuzhengwei on 2019
14+
*/
15+
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {
16+
17+
@Override
18+
protected void initChannel(SocketChannel channel) {
19+
/* 解码器 */
20+
// 基于换行符号
21+
channel.pipeline().addLast(new LineBasedFrameDecoder(1024));
22+
// 基于指定字符串【换行符,这样功能等同于LineBasedFrameDecoder】
23+
// e.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, false, Delimiters.lineDelimiter()));
24+
// 基于最大长度
25+
// e.pipeline().addLast(new FixedLengthFrameDecoder(4));
26+
// 解码转String,注意调整自己的编码格式GBK、UTF-8
27+
channel.pipeline().addLast(new StringDecoder(Charset.forName("GBK")));
28+
//在管道中添加我们自己的接收数据实现方法
29+
channel.pipeline().addLast(new MyServerHandler());
30+
}
31+
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.itstack.demo.netty.server;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
import io.netty.channel.socket.SocketChannel;
6+
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
9+
10+
/**
11+
* 虫洞栈:https://bugstack.cn
12+
* 公众号:bugstack虫洞栈 {获取学习源码}
13+
* Create by fuzhengwei on 2019
14+
*/
15+
public class MyServerHandler extends ChannelInboundHandlerAdapter {
16+
17+
@Override
18+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
19+
SocketChannel channel = (SocketChannel) ctx.channel();
20+
System.out.println("链接报告开始 {公众号:bugstack虫洞栈 >获取学习源码}");
21+
System.out.println("链接报告信息:有一客户端链接到本服务端");
22+
System.out.println("链接报告IP:" + channel.localAddress().getHostString());
23+
System.out.println("链接报告Port:" + channel.localAddress().getPort());
24+
System.out.println("链接报告完毕");
25+
}
26+
27+
@Override
28+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
29+
//接收msg消息{与上一章节相比,此处已经不需要自己进行解码}
30+
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息:" + msg);
31+
}
32+
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.itstack.demo.netty.server;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelOption;
6+
import io.netty.channel.EventLoopGroup;
7+
import io.netty.channel.nio.NioEventLoopGroup;
8+
import io.netty.channel.socket.nio.NioServerSocketChannel;
9+
10+
/**
11+
* 虫洞栈:https://bugstack.cn
12+
* 公众号:bugstack虫洞栈 {获取学习源码}
13+
* Create by fuzhengwei on 2019
14+
*/
15+
public class NettyServer {
16+
17+
public static void main(String[] args) {
18+
new NettyServer().bing(7397);
19+
}
20+
21+
private void bing(int port) {
22+
//配置服务端NIO线程组
23+
EventLoopGroup parentGroup = new NioEventLoopGroup(); //NioEventLoopGroup extends MultithreadEventLoopGroup Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
24+
EventLoopGroup childGroup = new NioEventLoopGroup();
25+
try {
26+
ServerBootstrap b = new ServerBootstrap();
27+
b.group(parentGroup, childGroup)
28+
.channel(NioServerSocketChannel.class) //非阻塞模式
29+
.option(ChannelOption.SO_BACKLOG, 128)
30+
.childHandler(new MyChannelInitializer());
31+
ChannelFuture f = b.bind(port).sync();
32+
System.out.println("itstack-demo-netty server start done. {关注公众号:bugstack虫洞栈,获取源码}");
33+
f.channel().closeFuture().sync();
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
} finally {
37+
childGroup.shutdownGracefully();
38+
parentGroup.shutdownGracefully();
39+
}
40+
41+
}
42+
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.itstack.demo.netty.server.test;
2+
3+
/**
4+
* 虫洞栈:https://bugstack.cn
5+
* 公众号:bugstack虫洞栈 {获取学习源码}
6+
* Create by fuzhengwei on 2019
7+
*/
8+
public class ApiTest {
9+
}

itstack-demo-netty-1-04/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>itstack-demo-netty</artifactId>
7+
<groupId>org.itatack.demo</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>itstack-demo-netty-1-04</artifactId>
13+
14+
15+
</project>

0 commit comments

Comments
 (0)