import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class GroupChatServer {
// 服务的端口号
private int port;
public GroupChatServer(int port){
this.port = port;
}
public void run() throws Exception{
// 创建BossGroup和WorkGroup
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workGroup = new NioEventLoopGroup();
// 创建 ServerBootstrap 服务启动对象
ServerBootstrap bootstrap = new ServerBootstrap();
try{
// 给bootstrap设置相关的参数
bootstrap.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,128)
.childOption(ChannelOption.SO_KEEPALIVE,true)
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
// 获取pipeline对象
ChannelPipeline pipeline = sc.pipeline();
// 设置对应的handler
pipeline.addLast(“docoder”,new StringDecoder());
pipeline.addLast(“encoder”,new StringEncoder());
pipeline.addLast(new GroupChatServerHandler());
}
});
System.out.println(“服务端启动了…”);
// 绑定端口
ChannelFuture future = bootstrap.bind(port).sync();
future.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
GroupChatServer server = new GroupChatServer(8888);
server.run();
}
}
[](()3. 客户端代码
[](()3.1 客户端处理器
获取服务器转发的消息
package com.dpb.netty.goupchat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class GroupChatClientHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
System.out.println(s.trim());
}
}
[](()3.2 客户端代码连接服务器,发送消息
package com.dpb.netty.goupchat;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.util.Scanner;
public class GroupChatClient {
public String host;
public int port ;
public GroupChatClient(String host,int port){
this.host = host;
this.port = port;
}
public void run() throws Exception{
EventLoopGroup clientGroup = new NioEventLoopGroup(1);
Bootstrap bootstrap = new Bootstrap();
try{
bootstrap.group(clientGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(“decoder”,new StringDecoder());
sc.pipelin 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 e().addLast(“encoder”,new StringEncoder());
sc.pipeline().addLast(new GroupChatClientHandler());
}
});
ChannelFuture future = bootstrap.connect(host, port).sync();
Channel channel = future.channel();
// 发送消息
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String msg = scanner.nextLine();
channel.writeAndFlush(msg + “n”);
}
}finally {
clientGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
GroupChatClient client = new GroupChatClient(“localhost”,8888);
client.run();
}
}



