Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
eb6dd06f
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看板
提交
eb6dd06f
编写于
4月 27, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:bytebuf数据合并
上级
7f70f425
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
145 addition
and
0 deletion
+145
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_11.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_11.java
+56
-0
src/main/java/com/kwan/shuyu/heima/netty_08_socket/TestClient.java
...java/com/kwan/shuyu/heima/netty_08_socket/TestClient.java
+43
-0
src/main/java/com/kwan/shuyu/heima/netty_08_socket/TestServer.java
...java/com/kwan/shuyu/heima/netty_08_socket/TestServer.java
+46
-0
未找到文件。
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_11.java
0 → 100644
浏览文件 @
eb6dd06f
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.*
;
import
lombok.extern.slf4j.Slf4j
;
/**
* ByteBuf使用Unpooled方式合并
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public
class
ByteBuf_11
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buf1
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
5
);
buf1
.
writeBytes
(
new
byte
[]{
1
,
2
,
3
,
4
,
5
});
ByteBuf
buf2
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
5
);
buf2
.
writeBytes
(
new
byte
[]{
6
,
7
,
8
,
9
,
10
});
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf1
));
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf2
));
// 当包装 ByteBuf 个数超过一个时, 底层使用了 CompositeByteBuf
ByteBuf
buf3
=
Unpooled
.
wrappedBuffer
(
buf1
,
buf2
);
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf3
));
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 |..... |
* +--------+-------------------------------------------------+----------------+
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 06 07 08 09 0a |..... |
* +--------+-------------------------------------------------+----------------+
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 06 07 08 09 0a |.......... |
* +--------+-------------------------------------------------+----------------+
*/
ByteBuf
buf4
=
Unpooled
.
wrappedBuffer
(
new
byte
[]{
1
,
2
,
3
},
new
byte
[]{
4
,
5
,
6
});
System
.
out
.
println
(
buf4
.
getClass
());
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf4
));
/**
* class io.netty.buffer.CompositeByteBuf
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 06 |...... |
* +--------+-------------------------------------------------+----------------+
*/
}
}
src/main/java/com/kwan/shuyu/heima/netty_08_socket/TestClient.java
0 → 100644
浏览文件 @
eb6dd06f
package
com.kwan.shuyu.heima.netty_08_socket
;
import
java.io.*
;
import
java.net.Socket
;
/**
* 客户端读写
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 16:58
*/
public
class
TestClient
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
Socket
s
=
new
Socket
(
"localhost"
,
8888
);
new
Thread
(()
->
{
try
{
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
s
.
getInputStream
()));
while
(
true
)
{
System
.
out
.
println
(
reader
.
readLine
());
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}).
start
();
new
Thread
(()
->
{
try
{
BufferedWriter
writer
=
new
BufferedWriter
(
new
OutputStreamWriter
(
s
.
getOutputStream
()));
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
writer
.
write
(
String
.
valueOf
(
i
));
writer
.
newLine
();
writer
.
flush
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}).
start
();
}
}
src/main/java/com/kwan/shuyu/heima/netty_08_socket/TestServer.java
0 → 100644
浏览文件 @
eb6dd06f
package
com.kwan.shuyu.heima.netty_08_socket
;
import
java.io.*
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
/**
* 服务端读写
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 16:58
*/
public
class
TestServer
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
ServerSocket
ss
=
new
ServerSocket
(
8888
);
Socket
s
=
ss
.
accept
();
new
Thread
(()
->
{
try
{
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
s
.
getInputStream
()));
while
(
true
)
{
System
.
out
.
println
(
reader
.
readLine
());
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}).
start
();
new
Thread
(()
->
{
try
{
BufferedWriter
writer
=
new
BufferedWriter
(
new
OutputStreamWriter
(
s
.
getOutputStream
()));
// 例如在这个位置加入 thread 级别断点,可以发现即使不写入数据,也不妨碍前面线程读取客户端数据
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
writer
.
write
(
String
.
valueOf
(
i
));
writer
.
newLine
();
writer
.
flush
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}).
start
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录