Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
c9d7445a
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看板
提交
c9d7445a
编写于
4月 19, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:写入操作
上级
a716d09d
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
326 addition
and
17 deletion
+326
-17
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_01_block/NioClient.java
...kwan/shuyu/heima/netty_03_nio/nio_01_block/NioClient.java
+1
-1
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_02_no_block/NioClient.java
...n/shuyu/heima/netty_03_nio/nio_02_no_block/NioClient.java
+1
-1
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioClient.java
...n/shuyu/heima/netty_03_nio/nio_03_selector/NioClient.java
+1
-1
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioServer.java
...n/shuyu/heima/netty_03_nio/nio_03_selector/NioServer.java
+7
-3
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_04_attachment/NioClient.java
...shuyu/heima/netty_03_nio/nio_04_attachment/NioClient.java
+27
-0
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_04_attachment/NioServer.java
...shuyu/heima/netty_03_nio/nio_04_attachment/NioServer.java
+29
-11
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_05_attach/NioClient.java
...wan/shuyu/heima/netty_03_nio/nio_05_attach/NioClient.java
+28
-0
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_05_attach/NioServer.java
...wan/shuyu/heima/netty_03_nio/nio_05_attach/NioServer.java
+103
-0
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_06_write/NioClient.java
...kwan/shuyu/heima/netty_03_nio/nio_06_write/NioClient.java
+32
-0
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_06_write/NioServer.java
...kwan/shuyu/heima/netty_03_nio/nio_06_write/NioServer.java
+97
-0
未找到文件。
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_01_block/NioClient.java
浏览文件 @
c9d7445a
...
...
@@ -19,7 +19,7 @@ public class NioClient {
//创建SocketChannel
SocketChannel
sc
=
SocketChannel
.
open
();
//建立和服务端的连接
sc
.
connect
(
new
InetSocketAddress
(
8080
));
sc
.
connect
(
new
InetSocketAddress
(
"localhost"
,
8080
));
System
.
out
.
println
(
"waiting .............."
);
}
}
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_02_no_block/NioClient.java
浏览文件 @
c9d7445a
...
...
@@ -19,7 +19,7 @@ public class NioClient {
//创建SocketChannel
SocketChannel
sc
=
SocketChannel
.
open
();
//建立和服务端的连接
sc
.
connect
(
new
InetSocketAddress
(
8080
));
sc
.
connect
(
new
InetSocketAddress
(
"localhost"
,
8080
));
System
.
out
.
println
(
"waiting .............."
);
}
}
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioClient.java
浏览文件 @
c9d7445a
...
...
@@ -19,7 +19,7 @@ public class NioClient {
//创建SocketChannel
SocketChannel
sc
=
SocketChannel
.
open
();
//建立和服务端的连接
sc
.
connect
(
new
InetSocketAddress
(
8080
));
sc
.
connect
(
new
InetSocketAddress
(
"localhost"
,
8080
));
System
.
out
.
println
(
"waiting .............."
);
}
}
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioServer.java
浏览文件 @
c9d7445a
...
...
@@ -55,9 +55,13 @@ public class NioServer {
try
{
final
SocketChannel
channel
=
(
SocketChannel
)
key
.
channel
();
ByteBuffer
bf
=
ByteBuffer
.
allocate
(
16
);
channel
.
read
(
bf
);
bf
.
flip
();
ByteBufferUtil
.
debugRead
(
bf
);
final
int
read
=
channel
.
read
(
bf
);
if
(
read
==
-
1
)
{
key
.
cancel
();
}
else
{
bf
.
flip
();
ByteBufferUtil
.
debugRead
(
bf
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
key
.
cancel
();
//取消事件处理
...
...
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_04_attachment/NioClient.java
0 → 100644
浏览文件 @
c9d7445a
package
com.kwan.shuyu.heima.netty_03_nio.nio_04_attachment
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.nio.channels.SocketChannel
;
import
java.nio.charset.Charset
;
/**
* NioClient 客户端
* <p>
* 发消息:sc.write(Charset.defaultCharset().encode("hi!"))
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 18:22
*/
public
class
NioClient
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
//创建SocketChannel
SocketChannel
sc
=
SocketChannel
.
open
();
//建立和服务端的连接
sc
.
connect
(
new
InetSocketAddress
(
"localhost"
,
8080
));
sc
.
write
(
Charset
.
defaultCharset
().
encode
(
"hello\nworld\n"
));
System
.
in
.
read
();
//阻塞住
}
}
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_0
3_selector/NioServerAttachment
.java
→
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_0
4_attachment/NioServer
.java
浏览文件 @
c9d7445a
package
com.kwan.shuyu.heima.netty_03_nio.nio_0
3_selector
;
package
com.kwan.shuyu.heima.netty_03_nio.nio_0
4_attachment
;
import
com.kwan.shuyu.until.ByteBufferUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.IOException
;
...
...
@@ -12,15 +11,36 @@ import java.nio.channels.ServerSocketChannel;
import
java.nio.channels.SocketChannel
;
import
java.util.Iterator
;
import
static
com
.
kwan
.
shuyu
.
until
.
ByteBufferUtil
.
debugAll
;
/**
* NioServer 服务端 NioServer
Attachment 附件传递
* NioServer 服务端 NioServer
附件传递和处理消息边界
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 18:22
*/
@Slf4j
public
class
NioServerAttachment
{
public
class
NioServer
{
private
static
void
split
(
ByteBuffer
source
)
{
source
.
flip
();
final
int
limit
=
source
.
limit
();
for
(
int
i
=
0
;
i
<
limit
;
i
++)
{
//找到完整的消息
if
(
source
.
get
(
i
)
==
'\n'
)
{
final
int
len
=
i
+
1
-
source
.
position
();
final
ByteBuffer
target
=
ByteBuffer
.
allocate
(
len
);
for
(
int
j
=
0
;
j
<
len
;
j
++)
{
//从source读取,向target写入
target
.
put
(
source
.
get
());
}
debugAll
(
target
);
}
}
source
.
compact
();
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
Selector
selector
=
Selector
.
open
();
//预设ByteBuffer,并分配空间,为了读取数据
...
...
@@ -49,7 +69,7 @@ public class NioServerAttachment {
final
SocketChannel
sc
=
channel
.
accept
();
sc
.
configureBlocking
(
false
);
ByteBuffer
bf
=
ByteBuffer
.
allocate
(
16
);
//将一个ByteBuffer作为附件绑定到SelectionKey
//将一个ByteBuffer作为附件绑定到SelectionKey
,处理消息边界
final
SelectionKey
scKey
=
sc
.
register
(
selector
,
0
,
bf
);
scKey
.
interestOps
(
SelectionKey
.
OP_READ
);
log
.
info
(
"sc={}"
,
sc
);
...
...
@@ -60,13 +80,11 @@ public class NioServerAttachment {
//获取附件的ByteBuffer
final
ByteBuffer
bf
=
(
ByteBuffer
)
key
.
attachment
();
final
int
read
=
channel
.
read
(
bf
);
if
(
read
==
-
1
){
}
else
{
if
(
read
==
-
1
)
{
key
.
cancel
();
}
else
{
split
(
bf
);
//多次读取,处理消息边界
}
bf
.
flip
();
ByteBufferUtil
.
debugRead
(
bf
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
key
.
cancel
();
//取消事件处理
...
...
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_05_attach/NioClient.java
0 → 100644
浏览文件 @
c9d7445a
package
com.kwan.shuyu.heima.netty_03_nio.nio_05_attach
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.nio.channels.SocketChannel
;
import
java.nio.charset.Charset
;
/**
* NioClient 客户端
* <p>
* 发消息:sc.write(Charset.defaultCharset().encode("hi!"))
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 18:22
*/
public
class
NioClient
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
//创建SocketChannel
SocketChannel
sc
=
SocketChannel
.
open
();
//建立和服务端的连接
sc
.
connect
(
new
InetSocketAddress
(
"localhost"
,
8080
));
sc
.
write
(
Charset
.
defaultCharset
().
encode
(
"0123\n456789abcdef"
));
sc
.
write
(
Charset
.
defaultCharset
().
encode
(
"0123456789abcdef333333333\n"
));
System
.
in
.
read
();
//阻塞住
}
}
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_05_attach/NioServer.java
0 → 100644
浏览文件 @
c9d7445a
package
com.kwan.shuyu.heima.netty_03_nio.nio_05_attach
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.SelectionKey
;
import
java.nio.channels.Selector
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.channels.SocketChannel
;
import
java.util.Iterator
;
import
static
com
.
kwan
.
shuyu
.
until
.
ByteBufferUtil
.
debugAll
;
/**
* NioServer 服务端 NioServer 附件传递和处理消息边界
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 18:22
*/
@Slf4j
public
class
NioServer
{
private
static
void
split
(
ByteBuffer
source
)
{
source
.
flip
();
final
int
limit
=
source
.
limit
();
for
(
int
i
=
0
;
i
<
limit
;
i
++)
{
//找到完整的消息
if
(
source
.
get
(
i
)
==
'\n'
)
{
final
int
len
=
i
+
1
-
source
.
position
();
final
ByteBuffer
target
=
ByteBuffer
.
allocate
(
len
);
for
(
int
j
=
0
;
j
<
len
;
j
++)
{
//从source读取,向target写入
target
.
put
(
source
.
get
());
}
debugAll
(
target
);
}
}
source
.
compact
();
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
Selector
selector
=
Selector
.
open
();
//预设ByteBuffer,并分配空间,为了读取数据
final
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
16
);
//创建ServerSocketChannel
final
ServerSocketChannel
ssc
=
ServerSocketChannel
.
open
();
//绑定端口号
ssc
.
configureBlocking
(
false
);
//ServerSocketChannel注册到Selector中
final
SelectionKey
sscKey
=
ssc
.
register
(
selector
,
0
,
null
);
sscKey
.
interestOps
(
SelectionKey
.
OP_ACCEPT
);
log
.
info
(
"sscKey={}"
,
sscKey
);
ssc
.
bind
(
new
InetSocketAddress
(
8080
));
while
(
true
)
{
//select方法,没有事件发生,线程阻塞,有事件,线程才会恢复运行
//select在事件未处理时,它不会阻塞,会一直请求处理
selector
.
select
();
final
Iterator
<
SelectionKey
>
iterator
=
selector
.
selectedKeys
().
iterator
();
while
(
iterator
.
hasNext
())
{
final
SelectionKey
key
=
iterator
.
next
();
iterator
.
remove
();
log
.
info
(
"key={}"
,
key
);
if
(
key
.
isAcceptable
())
{
final
ServerSocketChannel
channel
=
(
ServerSocketChannel
)
key
.
channel
();
final
SocketChannel
sc
=
channel
.
accept
();
sc
.
configureBlocking
(
false
);
ByteBuffer
bf
=
ByteBuffer
.
allocate
(
16
);
//将一个ByteBuffer作为附件绑定到SelectionKey,处理消息边界
final
SelectionKey
scKey
=
sc
.
register
(
selector
,
0
,
bf
);
scKey
.
interestOps
(
SelectionKey
.
OP_READ
);
log
.
info
(
"sc={}"
,
sc
);
log
.
info
(
"scKey={}"
,
scKey
);
}
else
if
(
key
.
isReadable
())
{
try
{
final
SocketChannel
channel
=
(
SocketChannel
)
key
.
channel
();
//获取附件的ByteBuffer
final
ByteBuffer
bf
=
(
ByteBuffer
)
key
.
attachment
();
final
int
read
=
channel
.
read
(
bf
);
if
(
read
==
-
1
)
{
key
.
cancel
();
}
else
{
split
(
bf
);
//多次读取,处理消息边界
if
(
bf
.
position
()
==
bf
.
limit
())
{
//扩容
final
ByteBuffer
newByteBuffer
=
ByteBuffer
.
allocate
(
buffer
.
capacity
()
*
2
);
buffer
.
flip
();
newByteBuffer
.
put
(
buffer
);
key
.
attach
(
newByteBuffer
);
//绑定新的ByteBuffer
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
key
.
cancel
();
//取消事件处理
}
}
}
}
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_06_write/NioClient.java
0 → 100644
浏览文件 @
c9d7445a
package
com.kwan.shuyu.heima.netty_03_nio.nio_06_write
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.SocketChannel
;
/**
* NioClient 客户端
* <p>
* 发消息:sc.write(Charset.defaultCharset().encode("hi!"))
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 18:22
*/
public
class
NioClient
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
//创建SocketChannel
SocketChannel
sc
=
SocketChannel
.
open
();
//建立和服务端的连接
sc
.
connect
(
new
InetSocketAddress
(
"localhost"
,
8080
));
int
count
=
0
;
while
(
true
)
{
final
ByteBuffer
byteBuffer
=
ByteBuffer
.
allocate
(
1024
*
1024
);
count
+=
sc
.
read
(
byteBuffer
);
System
.
out
.
println
(
count
);
byteBuffer
.
clear
();
}
}
}
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_06_write/NioServer.java
0 → 100644
浏览文件 @
c9d7445a
package
com.kwan.shuyu.heima.netty_03_nio.nio_06_write
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.SelectionKey
;
import
java.nio.channels.Selector
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.channels.SocketChannel
;
import
java.nio.charset.Charset
;
import
java.util.Iterator
;
import
static
com
.
kwan
.
shuyu
.
until
.
ByteBufferUtil
.
debugAll
;
/**
* NioServer 服务端 NioServer 附件传递和处理消息边界
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 18:22
*/
@Slf4j
public
class
NioServer
{
private
static
void
split
(
ByteBuffer
source
)
{
source
.
flip
();
final
int
limit
=
source
.
limit
();
for
(
int
i
=
0
;
i
<
limit
;
i
++)
{
//找到完整的消息
if
(
source
.
get
(
i
)
==
'\n'
)
{
final
int
len
=
i
+
1
-
source
.
position
();
final
ByteBuffer
target
=
ByteBuffer
.
allocate
(
len
);
for
(
int
j
=
0
;
j
<
len
;
j
++)
{
//从source读取,向target写入
target
.
put
(
source
.
get
());
}
debugAll
(
target
);
}
}
source
.
compact
();
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
Selector
selector
=
Selector
.
open
();
//预设ByteBuffer,并分配空间,为了读取数据
final
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
16
);
//创建ServerSocketChannel
final
ServerSocketChannel
ssc
=
ServerSocketChannel
.
open
();
//绑定端口号
ssc
.
configureBlocking
(
false
);
//ServerSocketChannel注册到Selector中
final
SelectionKey
sscKey
=
ssc
.
register
(
selector
,
0
,
null
);
sscKey
.
interestOps
(
SelectionKey
.
OP_ACCEPT
);
log
.
info
(
"sscKey={}"
,
sscKey
);
ssc
.
bind
(
new
InetSocketAddress
(
8080
));
while
(
true
)
{
//select方法,没有事件发生,线程阻塞,有事件,线程才会恢复运行
//select在事件未处理时,它不会阻塞,会一直请求处理
selector
.
select
();
final
Iterator
<
SelectionKey
>
iterator
=
selector
.
selectedKeys
().
iterator
();
while
(
iterator
.
hasNext
())
{
final
SelectionKey
key
=
iterator
.
next
();
iterator
.
remove
();
log
.
info
(
"key={}"
,
key
);
if
(
key
.
isAcceptable
())
{
final
ServerSocketChannel
channel
=
(
ServerSocketChannel
)
key
.
channel
();
final
SocketChannel
sc
=
channel
.
accept
();
sc
.
configureBlocking
(
false
);
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
3000000
;
i
++)
{
sb
.
append
(
"a"
);
}
final
ByteBuffer
bf
=
Charset
.
defaultCharset
().
encode
(
sb
.
toString
());
final
int
write
=
sc
.
write
(
bf
);
System
.
out
.
println
(
write
);
while
(
bf
.
hasRemaining
())
{
sscKey
.
interestOps
(
sscKey
.
interestOps
()
+
SelectionKey
.
OP_WRITE
);
sscKey
.
attach
(
bf
);
}
}
else
if
(
key
.
isWritable
())
{
final
ByteBuffer
byteBuffer
=
(
ByteBuffer
)
key
.
attachment
();
final
SocketChannel
channel
=
(
SocketChannel
)
key
.
channel
();
final
int
write
=
channel
.
write
(
byteBuffer
);
System
.
out
.
println
(
write
);
//清理操作
if
(!
buffer
.
hasRemaining
()){
key
.
attach
(
null
);
key
.
interestOps
(
key
.
interestOps
()
-
SelectionKey
.
OP_WRITE
);
}
}
}
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录