基于netty 开发soket服务端代码,需引用netty的jar包
package com.comtop.lcam.fwms.managementkey.socket;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import java.nio.charset.StandardCharsets;
/**
* 解析包的设置
*
* <pre>
* [
* 调用关系:
* 实现接口及父类:
* 子类:
* 内部类列表:
* ]
* </pre>
*
* @author 李锋
* @since 1.0
* @version 2018-11-20 李锋
*/
public class EslcsSocketChannel extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
ch.pipeline().addLast(new EslcsSocketHandler());
ch.pipeline().addLast(new ByteArrayEncoder());
}
}
package com.comtop.lcam.fwms.managementkey.socket;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.nio.charset.StandardCharsets;
import javax.annotation.Resource;
import com.comtop.lcam.fwms.managementkey.facade.internal.EslcsLockLogFacade;
import com.comtop.sproc.core.base.util.SprocApplicationContextUtil;
/**
* 服务端处理通道
*/
@ChannelHandler.Sharable
public class EslcsSocketHandler extends ChannelInboundHandlerAdapter {
/** 开锁日志管理服务 */
@Resource
private final EslcsLockLogFacade eslcsLockLogFacade = SprocApplicationContextUtil.getContext().getBean(EslcsLockLogFacade.class);
/***
* 这个方法会在发生异常时触发
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
System.out.println("=======================Netty发生异常=======================");
cause.printStackTrace();
ctx.close();
}
/**
* 连接断开处理事件
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) {
System.out.println("=======================Netty断开连接=======================");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("=======================有新客户端连接=======================");
}
/**
* 功能:读取服务器发送过来的信息 String data = "aa0b80 00000001 00 313233343536 04 1f7d1bc0 e6 55"
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 第一种:接收字符串时的处理
ByteBuf buf = (ByteBuf) msg;
String rev = getMessage(buf);
System.out.println("客户端收到服务器数据:" + rev);
System.out.println("服务器返回客户端数据:" + rev);
ctx.channel().writeAndFlush("aa0x31");
System.out.println("----------------------------");
/*
* EslcsLockLogDTO eslcsLockLogDTO = new EslcsLockLogDTO(); eslcsLockLogDTO.setUnlockingUserName("cesces");
* eslcsLockLogFacade.create(eslcsLockLogDTO);
*/
}
private String getMessage(ByteBuf buf) {
byte[] con = new byte[buf.readableBytes()];
buf.readBytes(con);
return new String(con, StandardCharsets.UTF_8);
}
}
package com.comtop.lcam.fwms.managementkey.socket;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* soket监听服务
*
* @author 李锋
* @version 2018-11-20 李锋
* @since 1.0
*/
public class EslcsSocketService implements Runnable {
/** 异常输出 */
private static final Logger LOGGER = LoggerFactory.getLogger(EslcsSocketService.class);
/**
* soket监听10120,"218.17.29.6"
*/
public static void soketListener() {
int port = 8525;
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new EslcsSocketChannel());
try {
ChannelFuture channelFuture = bootstrap.bind(port).sync();
channelFuture.channel().closeFuture().sync();
LOGGER.error("=======================Netty在" + port + "端口启动=======================");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
/**
* 后续集成到spring,暂时main方法启动
*
* @param args
*/
public static void main(String[] args) {
EslcsSocketService.soketListener();
}
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
EslcsSocketService.soketListener();
System.out.println("-------------开启线程监听----------------");
}
}