Commit 44fe55af authored by leyboy's avatar leyboy

1.ZnksServer完成,接受客户端解码未完成

parent e77ed45b
This diff is collapsed.
package com.adc.da.znks.server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* znks server
*
* @author ley
* **/
**/
public class ZnksServer {
private static final Logger logger = LoggerFactory.getLogger(ZnksServer.class);
/**
* init znks server
**/
public static void initZnksServer(int port) {
//服务类
ServerBootstrap bootstrap = new ServerBootstrap();
//boss和worker
//EventLoopGroup用来管理和调度netty中的各种线程
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
try {
//设置线程池
bootstrap.group(boss, worker);
//设置socket工厂、
bootstrap.channel(NioServerSocketChannel.class);
//设置管道工厂
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
}
});
//设置参数,TCP参数
bootstrap.option(ChannelOption.SO_BACKLOG, 2048);//serverSocketchannel的设置,链接缓冲池的大小
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);//socketchannel的设置,维持链接的活跃,清除死链接
bootstrap.childOption(ChannelOption.TCP_NODELAY, true);//socketchannel的设置,关闭延迟发送
//绑定端口
ChannelFuture future = bootstrap.bind(port);
logger.info("57所netty监听服务启动成功...");
//等待服务端关闭
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
}
package com.adc.da.znks.server.handler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
/**
* request decoder
**/
public class RequestDecoder extends ByteToMessageDecoder {
/**
*
* **/
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
}
}
package com.adc.da.znks.server.handler;
import com.adc.da.znks.server.utils.ClientUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* znks server handler
*
* @author ley
**/
public class ZnksServerHandler extends ChannelInboundHandlerAdapter {
private static final Logger logger = LoggerFactory.getLogger(ZnksServerHandler.class);
/**
* 存有连接进来的客户端
**/
public static final Map<String, ChannelHandlerContext> clientMap =
new ConcurrentHashMap<>(64);
/**
* 客户端数量
**/
private final AtomicInteger clientNumber = new AtomicInteger(0);
/**
* 接受客户端数据
**/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
}
/**
* 新客户端接入
**/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String key = ClientUtils.getClientIpAndPort(ctx);
clientMap.put(key, ctx);
clientNumber.incrementAndGet();
logger.info("客户端: {}, 连接成功", key);
logger.info("连接服务端的客户端个数: {}", clientNumber.get());
}
/**
* 客户端断开
**/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
}
/**
* 处理异常
**/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
}
}
package com.adc.da.znks.server.utils;
import io.netty.channel.ChannelHandlerContext;
import java.net.InetSocketAddress;
/**
* client utility class
*
* @author ley
**/
public class ClientUtils {
/**
* get client ip and port
**/
public static String getClientIpAndPort(ChannelHandlerContext context) {
InetSocketAddress clientSocket = (InetSocketAddress) context.channel().remoteAddress();
String clientIp = clientSocket.getAddress().getHostAddress();
int clientPort = clientSocket.getPort();
return clientIp + ":" + clientPort;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment