Compare commits

..

No commits in common. "f0ff7a82f1fff7b9217f0c5c85c153bc00682e0b" and "96b9935b08a88e1cb05af6c5c383cbacf39d90f2" have entirely different histories.

4 changed files with 1 additions and 94 deletions

View File

@ -10,7 +10,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -106,15 +106,6 @@
<skipDockerBuild>false</skipDockerBuild>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,36 +0,0 @@
package cn.bunny.service.netty.demo1;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import java.net.InetSocketAddress;
public class StartClient01 {
/*
客户端处理内容
*/
public static void main(String[] args) throws InterruptedException {
// 启动类
new Bootstrap()
// 添加 事件循环
.group(new NioEventLoopGroup())
// 选择客户端 channel 实现
.channel(NioServerSocketChannel.class)
// 添加处理器
.handler(new ChannelInitializer<NioSocketChannel>() {
// 连接后被调用
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringDecoder());
}
})
.connect(new InetSocketAddress("localhost", 8080))
.sync()
.channel()
.writeAndFlush("你好啊啊啊");
}
}

View File

@ -1,48 +0,0 @@
package cn.bunny.service.netty.demo1;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import static java.lang.StringTemplate.STR;
public class StartServer01 {
/*
服务器端处理内容
*/
public static void main(String[] args) {
// 启动器
new ServerBootstrap()
// 添加组件 NioEventLoopGroup WorkerEventLoop
.group(new NioEventLoopGroup())
// 选择 ServerNio 实现还支持某种Linux的实现
.channel(NioServerSocketChannel.class)
// 处理字节进行分工需要处理哪些逻辑决定了 child可以执行哪些操作
.childHandler(
// 代码客户端读写通道 添加别的 handler
new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
// 添加解码将传输过来的 ByteBuffer 转为字符串
nioSocketChannel.pipeline().addLast(new StringDecoder());
// 自定处理器
nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
// 发生读事件
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 打印上一步转化好的字符串
System.out.println(STR."消息为\{msg}");
super.channelRead(ctx, msg);
}
});
}
})
// 绑定端口
.bind(8080);
}
}