netty 实现群聊
2021-03-12 20:37
标签:客户端程序 自己 stack keepalive remote nal local start foreach 服务端 Server 服务器端 Channel 初始化器 GroupChatServerChannelInitializer 服务器端 自定义的 handler---GroupChatServerHandler 客户端 Client GroupChatClientChannelInitializer GroupChatClientHandler 启动服务端程序 和 3 个 客户端程序 客户端发送消息 --- netty 实现群聊 标签:客户端程序 自己 stack keepalive remote nal local start foreach 原文地址:https://www.cnblogs.com/xy-ouyang/p/12824998.htmlpackage com.oy.groupchat;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class Server {
private int port;
public Server(int port) {
this.port = port;
}
public static void main(String[] args) throws Exception {
new Server(8000).run();
}
public void run() throws Exception {
EventLoopGroup boss = new NioEventLoopGroup(1);
EventLoopGroup work = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap
.channel(NioServerSocketChannel.class)
.group(boss, work)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new GroupChatServerChannelInitializer());
// 绑定端口,启动服务
ChannelFuture future = bootstrap.bind(port).sync();
System.out.println("server started and listen " + port);
// 监听关闭
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
work.shutdownGracefully();
}
}
}
package com.oy.groupchat;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class GroupChatServerChannelInitializer extends ChannelInitializer
package com.oy.groupchat;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GroupChatServerHandler extends SimpleChannelInboundHandler
package com.oy.groupchat;
import com.oy.helloworld.NettyClient;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.Scanner;
public class Client {
private static final String HOST = "127.0.0.1";
private static final int PORT = 8000;
public static void main(String[] args) {
new Client().run(HOST, PORT);
}
public void run(String host, int port) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap client = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer
package com.oy.groupchat;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class GroupChatClientChannelInitializer extends ChannelInitializer
package com.oy.groupchat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class GroupChatClientHandler extends SimpleChannelInboundHandler