使用netty开发透传服务器

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));
  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值