main类
package com.server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class main { public static void main(String[] args) throws InterruptedException, NumberFormatException, IOException { Server test = new Server(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入端口号:"); int port = Integer.parseInt(bufferedReader.readLine()); test.server(port); } }
主程序
package com.server; import java.util.*; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.bytes.ByteArrayDecoder; import io.netty.handler.codec.bytes.ByteArrayEncoder; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleStateHandler; import javafx.application.Platform; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import java.nio.charset.Charset; public class Server { public static ChannelFuture f; public static EventLoopGroup g; Map<Channel, ArrayList> map = new HashMap<Channel, ArrayList>(); //保存透传映射 Map<Channel, EventLoopGroup> groups = new HashMap<Channel, EventLoopGroup>(); //保存透传线程池,关闭连接时用 Map<ChannelHandlerContext,Integer> devices = new HashMap<ChannelHandlerContext,Integer>(); // 保存在线设备端口号,在收到注册信息和心跳信息时用 public static int i = 0; //连接计数器 GUIProperty GUIProperty = new GUIProperty(); //GUI类,用于向图形界面传递信息 public Server(){ } public void server(int count) throws InterruptedException{ EventLoopGroup group = new NioEventLoopGroup(50); g = group; ServerBootstrap bootstrap = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class); //设置管道 bootstrap.childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { //ch.pipeline().addLast(new StringDecoder()); //ch.pipeline().addLast(new StringEncoder()); ch.pipeline().addLast(new ByteArrayDecoder()); ch.pipeline().addLast(new ByteArrayEncoder()); ch.pipeline().addLast(new IdleStateHandler(60, 60, 60)); //设置心跳超时时间,秒 ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String tansMsg = new String((byte[])msg,"UTF-8"); //byte转化为字符串 if (tansMsg.startsWith("reg")){ //当收到为注册信息 //从注册信息中提取端口号 System.out.println(ctx.channel().remoteAddress()+" "+tansMsg); Platform.runLater(new Runnable() { @Override public void run() { GUIProperty.setMsg(ctx.channel().remoteAddress()+" "+tansMsg); } }); int port = Integer.parseInt("2"+tansMsg.toString().substring(tansMsg.toString().length()-5, tansMsg.toString().length()-1));
使用netty开发透传服务器
最新推荐文章于 2024-05-01 01:29:41 发布