netty框架+绑定多个端口+接受canbus信号 矿山定位系统最终版本

背景

1、hibernate框架操作数据库

2、netty框架接受数据(非阻塞)

3、绑定多个端口


存储在数据库里面的数据如下

 

 关键代码在这里

package cn.com.service;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class ImServer implements Runnable{
	private int port;
	public ImServer(int i) {
		// TODO Auto-generated constructor stub
		this.port=i;
	}
	public void run() {
		EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();     
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup)
        		.channel(NioServerSocketChannel.class)
        		.childHandler(new ChannelInitializer<SocketChannel>() { 
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {	
                    	ch.pipeline().addLast(new ServerStringHandler());
                    }
                })
        		.option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true);
        
        try {
			 ChannelFuture f = bootstrap.bind(port).sync();
			 f.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
	}
	public static void mystart() {
		new Thread(new ImServer(6013)).start();
		new Thread(new ImServer(6019)).start();
		new Thread(new ImServer(6061)).start();
		new Thread(new ImServer(6011)).start();
		new Thread(new ImServer(6071)).start();
		new Thread(new ImServer(6021)).start();
		new Thread(new ImServer(6023)).start();
		new Thread(new ImServer(6014)).start();
		new Thread(new ImServer(6015)).start();
		new Thread(new ImServer(6088)).start();
		new Thread(new ImServer(6016)).start();
		new Thread(new ImServer(6024)).start();
		new Thread(new ImServer(6017)).start();
		new Thread(new ImServer(6022)).start();
		new Thread(new ImServer(6020)).start();
		new Thread(new ImServer(6018)).start();
		new Thread(new ImServer(6040)).start();
		new Thread(new ImServer(6010)).start();
	}

}

完整的代码在这里https://download.csdn.net/download/qq_37591637/11266521

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37591637

请给我持续更新的动力~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值