Skip to content

Commit 593c9ef

Browse files
authored
Add files via upload
1 parent b7c8038 commit 593c9ef

File tree

10 files changed

+139
-0
lines changed

10 files changed

+139
-0
lines changed

NettyDome/NettyDome.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />

NettyDome/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>0</groupId>
8+
<artifactId>1</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<dependencies>
11+
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
12+
<dependency>
13+
<groupId>io.netty</groupId>
14+
<artifactId>netty-all</artifactId>
15+
<version>4.1.42.Final</version>
16+
</dependency>
17+
</dependencies>
18+
19+
20+
21+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cn.hebeuit;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.buffer.ByteBuf;
5+
import io.netty.buffer.Unpooled;
6+
import io.netty.channel.*;
7+
import io.netty.channel.nio.NioEventLoopGroup;
8+
import io.netty.channel.socket.SocketChannel;
9+
import io.netty.channel.socket.nio.NioSocketChannel;
10+
import io.netty.util.CharsetUtil;
11+
12+
public class Client {
13+
public static void main(String[] args) {
14+
EventLoopGroup eventExecutors=new NioEventLoopGroup();
15+
Bootstrap bootstrap=new Bootstrap();
16+
bootstrap.group(eventExecutors)
17+
.channel(NioSocketChannel.class)
18+
.handler(new ChannelInitializer<SocketChannel>() {
19+
protected void initChannel(SocketChannel serverChannel) throws Exception {
20+
serverChannel.pipeline().addLast(
21+
new ChannelInboundHandlerAdapter(){
22+
@Override
23+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
24+
System.out.println(ctx);
25+
ctx.writeAndFlush(Unpooled.copiedBuffer("hello", CharsetUtil.UTF_8));
26+
}
27+
28+
@Override
29+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
30+
System.out.println("read:"+ctx);
31+
ByteBuf buf=(ByteBuf)msg;
32+
System.out.println(buf.toString(CharsetUtil.UTF_8));
33+
34+
}
35+
}
36+
);
37+
}
38+
});
39+
try {
40+
41+
ChannelFuture channelFuture= bootstrap.connect("127.0.0.1",6668).sync();
42+
Thread.sleep(10000);
43+
channelFuture.channel().writeAndFlush(Unpooled.copiedBuffer("hi", CharsetUtil.UTF_8));
44+
channelFuture.channel().closeFuture().sync();
45+
} catch (InterruptedException e) {
46+
e.printStackTrace();
47+
}finally {
48+
eventExecutors.shutdownGracefully();
49+
50+
}
51+
52+
}
53+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cn.hebeuit;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.buffer.ByteBuf;
5+
import io.netty.buffer.Unpooled;
6+
import io.netty.channel.*;
7+
import io.netty.channel.nio.NioEventLoopGroup;
8+
import io.netty.channel.socket.SocketChannel;
9+
import io.netty.channel.socket.nio.NioServerSocketChannel;
10+
import io.netty.util.CharsetUtil;
11+
12+
13+
public class Server {
14+
public static void main(String[] args) {
15+
var bossGroup=new NioEventLoopGroup();
16+
EventLoopGroup workerGroup=new NioEventLoopGroup();
17+
ServerBootstrap bootstrap=new ServerBootstrap();
18+
bootstrap.group(bossGroup,workerGroup)
19+
.channel(NioServerSocketChannel.class)
20+
.option(ChannelOption.SO_BACKLOG,128)
21+
.childOption(ChannelOption.SO_KEEPALIVE,true)
22+
.childHandler(new ChannelInitializer<SocketChannel>() {
23+
@Override
24+
protected void initChannel(SocketChannel socketChannel) throws Exception {
25+
socketChannel.pipeline().addLast(
26+
new ChannelInboundHandlerAdapter(){
27+
@Override
28+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
29+
System.out.println(ctx);
30+
ByteBuf buf=(ByteBuf)msg;
31+
String s = buf.toString(CharsetUtil.UTF_8);
32+
System.out.println(s);
33+
34+
ctx.writeAndFlush(Unpooled.copiedBuffer(s,CharsetUtil.UTF_8));
35+
}
36+
37+
@Override
38+
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
39+
ctx.writeAndFlush(Unpooled.copiedBuffer("end",CharsetUtil.UTF_8));
40+
41+
}
42+
43+
@Override
44+
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
45+
super.handlerRemoved(ctx);
46+
System.out.println("out :"+ctx);
47+
48+
}
49+
}
50+
);
51+
}
52+
});
53+
try {
54+
ChannelFuture cf=bootstrap.bind("127.0.0.1",6668).sync();
55+
cf.channel().closeFuture().sync();
56+
} catch (Exception e) {
57+
e.printStackTrace();
58+
59+
}
60+
}
61+
62+
63+
}
Binary file not shown.
Binary file not shown.
2.21 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
2.24 KB
Binary file not shown.

0 commit comments

Comments
 (0)