项目需要使用netty做中转服务器,同时支持两种不同协议的客户端,经过几天查询资料终于找到合适的方案了,同时感谢Netty权威指南及论坛问答,开始贴代码

客户端1==》socket

 public class Bluetooth implements Runnable {
//蓝牙 private int port;
@Override
public void run() {
System.out.println("--------进入蓝牙---------");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
System.out.println("chhhh"+ch.id());
// 注册handler
/*ch.pipeline().addLast("http-codec", new HttpServerCodec());
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());*/
ch.pipeline().addLast(new SimpleServerHandler());
}
});
// b.childHandler(new ChannelFilter());
System.out.println("平台监听开启....");
Channel ch = b.bind(5500).sync().channel();
ch.closeFuture().sync(); } catch (Exception e) {
e.printStackTrace();
}finally{
//优雅的退出程序
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}

客户端2==》http

public class Myweb implements Runnable {
//Myweb private int port;
@Override
public void run() {
System.out.println("--------进入web---------");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel e) throws Exception {
System.out.println("chhhh"+e.id());
e.pipeline().addLast("http-codec", new HttpServerCodec());
e.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
e.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
e.pipeline().addLast(new SimpleServerHandler());
}
});
// b.childHandler(new ChannelFilter());
System.out.println("平台监听开启....");
Channel ch = b.bind(8888).sync().channel();
ch.closeFuture().sync(); } catch (Exception e) {
e.printStackTrace();
}finally{
//优雅的退出程序
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}

main==>开启两个监听线程

public class main {
private int port; public main(int port) {
this.port = port;
} public void run() throws Exception { } public static void main(String[] args) throws Exception {
// new main(5500).run();
Bluetooth bluetooth = new Bluetooth();
Myweb myweb = new Myweb(); Thread th1 = new Thread(bluetooth);
Thread th2 = new Thread(myweb);
th1.start();
th2.start(); }
}
Handler代码就不贴了,网上很多,主要是通过多线程分别使用不同编解码器,
对不同客户端的协议进行解析。同时将Chnnel通道保存在Map集合中,两个线程可共享这个Chnnel。

参考:https://blog.csdn.net/lmianhuatang/article/details/79675790

最新文章

  1. KMP算法实现
  2. About SQLite
  3. SQLite 函数大全
  4. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)
  5. App开发的过程
  6. C++ Qt 框架静态编译 操作记录
  7. Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
  8. Effective C++ 笔记二 构造/析构/赋值运算
  9. linux下的webserver BOA及CGIC库的使用指南(转帖)
  10. 【转】在写一个iOS应用之前必须做的7件事(附相关资源)
  11. 将网站固定到开始菜单,自定义图标、颜色和Windows推送通知
  12. 【APP测试初体验】android测试命令----压力测试
  13. 用vue官方提供的模板vue-cli搭建一个helloWorld案例
  14. table给tbody设置滚动条
  15. HDU 2176 取(m堆)石子游戏 (尼姆博奕)
  16. sencha touch datepicker/datepickerfield(时间选择控件)扩展(废弃 仅参考)
  17. IntelliJ IDEA部署tomcat时Edit Configuration无artifact选项
  18. PLSQL_性能优化索引Index介绍(概念)
  19. FromBottomToTop团队项目总结
  20. Weak References

热门文章

  1. iOS与导航相关的都在这
  2. Universally Unique Identifier amazonservices API order 亚马逊订单接口的分析 NextToken
  3. &lt;2014 04 16&gt; 上班实习第一天
  4. Web 编程中路径问题
  5. java 集合类复习(未完结)
  6. java容器的线程安全性
  7. 设置EditText明文切换
  8. node.js---sails项目开发(6)--- 实现分页功能
  9. CentOS 6.5 QtCreator启动时 dbus-1的错误解决方法
  10. python学习之路-第五天-python的数据结构