Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
052c633a
N
netty-demo
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
netty-demo
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
netty-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
052c633a
编写于
8月 24, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:netty入栈出栈
上级
fd60895c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
193 addition
and
0 deletion
+193
-0
netty-02-induction/src/main/java/com/kwan/shuyu/demo/Client.java
...2-induction/src/main/java/com/kwan/shuyu/demo/Client.java
+42
-0
netty-02-induction/src/main/java/com/kwan/shuyu/demo/EchoExceptionHandler.java
...c/main/java/com/kwan/shuyu/demo/EchoExceptionHandler.java
+12
-0
netty-02-induction/src/main/java/com/kwan/shuyu/demo/EchoOutSelfHandler.java
...src/main/java/com/kwan/shuyu/demo/EchoOutSelfHandler.java
+31
-0
netty-02-induction/src/main/java/com/kwan/shuyu/demo/EchoServerHandler.java
.../src/main/java/com/kwan/shuyu/demo/EchoServerHandler.java
+20
-0
netty-02-induction/src/main/java/com/kwan/shuyu/demo/Server.java
...2-induction/src/main/java/com/kwan/shuyu/demo/Server.java
+88
-0
未找到文件。
netty-02-induction/src/main/java/com/kwan/shuyu/demo/Client.java
0 → 100644
浏览文件 @
052c633a
package
com.kwan.shuyu.demo
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.channel.ChannelInitializer
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
io.netty.handler.codec.string.StringEncoder
;
import
java.net.InetSocketAddress
;
/**
* 客户端
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 16:42
*/
public
class
Client
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
//1.启动类
new
Bootstrap
()
// 2.添加 EventLoop
.
group
(
new
NioEventLoopGroup
())
//3.选择客户端channel实现
.
channel
(
NioSocketChannel
.
class
)
// 4.添加处理器
.
handler
(
new
ChannelInitializer
<
NioSocketChannel
>()
{
@Override
//在连接建立后被调用
protected
void
initChannel
(
NioSocketChannel
ch
)
throws
Exception
{
ch
.
pipeline
().
addLast
(
new
StringEncoder
());
}
})
//连接到服务器
.
connect
(
new
InetSocketAddress
(
"localhost"
,
8080
))
.
sync
()
.
channel
()
//发送数据
.
writeAndFlush
(
"hello world"
)
;
}
}
\ No newline at end of file
netty-02-induction/src/main/java/com/kwan/shuyu/demo/EchoExceptionHandler.java
0 → 100644
浏览文件 @
052c633a
package
com.kwan.shuyu.demo
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
public
class
EchoExceptionHandler
extends
ChannelInboundHandlerAdapter
{
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
super
.
channelRead
(
ctx
,
msg
);
throw
new
RuntimeException
(
"抛出入站异常了"
);
}
}
\ No newline at end of file
netty-02-induction/src/main/java/com/kwan/shuyu/demo/EchoOutSelfHandler.java
0 → 100644
浏览文件 @
052c633a
package
com.kwan.shuyu.demo
;
import
io.netty.buffer.Unpooled
;
import
io.netty.channel.ChannelFutureListener
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelOutboundHandlerAdapter
;
import
io.netty.channel.ChannelPromise
;
import
io.netty.util.CharsetUtil
;
public
class
EchoOutSelfHandler
extends
ChannelOutboundHandlerAdapter
{
@Override
public
void
write
(
ChannelHandlerContext
ctx
,
Object
msg
,
ChannelPromise
promise
)
throws
Exception
{
System
.
out
.
println
(
"执行1"
);
super
.
write
(
ctx
,
msg
,
promise
);
ctx
.
write
(
Unpooled
.
copiedBuffer
(
"Netty rocks from Server!"
,
CharsetUtil
.
UTF_8
));
promise
.
setSuccess
();
promise
.
addListener
((
ChannelFutureListener
)
future
->
{
if
(
future
.
isSuccess
())
{
System
.
out
.
println
(
"消息出站了"
);
}
else
{
future
.
cause
().
printStackTrace
();
}
});
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
{
System
.
out
.
println
(
"im in"
);
cause
.
printStackTrace
();
}
}
\ No newline at end of file
netty-02-induction/src/main/java/com/kwan/shuyu/demo/EchoServerHandler.java
0 → 100644
浏览文件 @
052c633a
package
com.kwan.shuyu.demo
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandler
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
io.netty.util.CharsetUtil
;
@ChannelHandler
.
Sharable
public
class
EchoServerHandler
extends
ChannelInboundHandlerAdapter
{
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
{
ByteBuf
in
=
(
ByteBuf
)
msg
;
System
.
out
.
println
(
"Server received:"
+
in
.
toString
(
CharsetUtil
.
UTF_8
));
//入栈处理器最后的写入,供出栈处理器读取 ctx.channel()相当于 NioSocketChannel ch
ctx
.
channel
().
writeAndFlush
(
ctx
.
alloc
().
buffer
().
writeBytes
(
msg
.
toString
().
getBytes
()));
}
}
netty-02-induction/src/main/java/com/kwan/shuyu/demo/Server.java
0 → 100644
浏览文件 @
052c633a
package
com.kwan.shuyu.demo
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.ChannelInitializer
;
import
io.netty.channel.ChannelPipeline
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioServerSocketChannel
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
lombok.extern.slf4j.Slf4j
;
/**
* 测试pipeline,入栈和出栈
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 02:45
*/
@Slf4j
public
class
Server
{
public
static
void
main
(
String
[]
args
)
{
EchoServerHandler
serverHandler
=
new
EchoServerHandler
();
new
ServerBootstrap
()
.
group
(
new
NioEventLoopGroup
())
.
channel
(
NioServerSocketChannel
.
class
)
.
childHandler
(
new
ChannelInitializer
<
NioSocketChannel
>()
{
@Override
protected
void
initChannel
(
NioSocketChannel
ch
)
throws
Exception
{
ChannelPipeline
pipeline
=
ch
.
pipeline
()
// .addLast(new EchoExceptionHandler())
.
addLast
(
serverHandler
)
.
addLast
(
new
EchoOutSelfHandler
());
// // 1.通过 channel 拿到 pipeline
// //添加处理器head->h1->h2->h3->h4->h5->h6->tail
// ChannelPipeline pipeline = ch.pipeline();
// //2. 添加处理器head -> h1 -> tail
// pipeline.addLast("h1", new ChannelInboundHandlerAdapter() {
// @Override
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// log.info("1");
// ByteBuf buf = (ByteBuf) msg;
// final String name = buf.toString(Charset.defaultCharset());
// super.channelRead(ctx, name);
// }
// });
// pipeline.addLast("h2", new ChannelInboundHandlerAdapter() {
// @Override
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// log.info("2");
// final Student student = new Student(msg.toString());
// super.channelRead(ctx, student);
// }
// });
// pipeline.addLast("h3", new ChannelInboundHandlerAdapter() {
// @Override
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// log.info("3");
// log.info("h3结果{},class={}", msg, msg.getClass());
// //入栈处理器最后的写入,供出栈处理器读取
// ch.writeAndFlush(ctx.alloc().buffer().writeBytes("server...".getBytes()));
// }
// });
// pipeline.addLast("h4", new ChannelOutboundHandlerAdapter() {
// @Override
// public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// log.info("4");
// super.write(ctx, msg, promise);
// }
// });
// pipeline.addLast("h5", new ChannelOutboundHandlerAdapter() {
// @Override
// public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// log.info("5");
// super.write(ctx, msg, promise);
// }
// });
// pipeline.addLast("h6", new ChannelOutboundHandlerAdapter() {
// @Override
// public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// log.info("6");
// super.write(ctx, msg, promise);
// }
// });
}
}).
bind
(
8080
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录