protocol buf结合mina开发网络程序
来源:互联网 发布:制作linux u盘启动盘 编辑:程序博客网 时间:2024/05/17 07:51
一、服务端代码
public static void main(String[] args) throws NamingException, BeanException {System.setProperty("java.naming.factory.initial","org.apache.naming.java.javaURLContextFactory");// 加载machineInitialContext context = new InitialContext();File configDir = new File(getConfigDirectory(),SERVICE_CONFIGURATION_FILE);initService(configDir, context);// 启动socket服务NioSocketAcceptor acceptor = new NioSocketAcceptor();acceptor.getFilterChain().addLast("logger", new LoggingFilter());acceptor.getFilterChain().addLast("codec", new StreamWriteFilter()); acceptor.setHandler(new ServiceRouteServerHandler());acceptor.getSessionConfig().setReadBufferSize(4096);acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);try {acceptor.bind(new InetSocketAddress(PORT));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
注意红色部分,因为protocol buf良好的支持字节流,所有要使用此过滤器二、客户端代码
public static void main(String[] args) {Socket client;try {client = new Socket("127.0.0.1", 9123);OutputStream out = client.getOutputStream();RequestBeanProtos.RequestBean request = RequestBeanProtos.RequestBean.newBuilder().setAccount("811").setDeviceId("device1").setGroup("sync").setType("card").build();out.write(request.toByteArray());InputStream in = client.getInputStream();byte[] data = IOTools.readContent(in, 4096);System.out.println(new String(data));if (data != null && data.length > 0) {ServiceResponseBeanProtos.ResponseBean response = ServiceResponseBeanProtos.ResponseBean.parseFrom(data);System.out.println(response.getError());System.out.println(response.getContent());} else {System.out.println("content is empty!");}out.close();in.close();client.close();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
三、Handler的消息接收处理
@Overridepublic void messageReceived(IoSession session, Object message)throws Exception {IoBuffer buffer = (IoBuffer) message;ByteBuffer bf = buffer.buf();byte[] tempBuffer = new byte[bf.limit()];bf.get(tempBuffer);RouteEngine engine = Configuration.getConfiguration().getEngine();byte []data = engine.response(tempBuffer);int len = data.length;IoBuffer resp = IoBuffer.allocate(len);resp.put(data, 0, len);resp.flip(); session.write(resp);}
- protocol buf结合mina开发网络程序
- protocol buf的简单使用
- [MINA]使用 Apache MINA 开发高性能网络应用程序
- Apache MINA--使用 Apache MINA 开发高性能网络应用程序
- 基于MINA框架快速开发网络应用程序
- 基于MINA框架快速开发网络应用程序
- 基于MINA框架快速开发网络应用程序
- 使用 Apache MINA 2 开发网络应用
- Apache MINA 开发高性能网络应用程序
- 基于MINA 框架快速开发网络应用程序
- 使用 Apache MINA 2 开发网络应用
- 基于MINA框架快速开发网络应用程序
- 基于MINA框架快速开发网络应用程序
- 基于MINA框架快速开发网络应用程序
- 使用 Apache MINA 2 开发网络应用
- 使用 Apache MINA 2 开发网络应用
- 使用 Apache MINA 2 开发网络应用
- 使用 Apache MINA 2 开发网络应用
- V4L2应用程序框架
- ubuntu中lamp几个常用的路径
- 判断网络的状态断开与否
- 深入解析用户界面(一)
- Debian 初始化
- protocol buf结合mina开发网络程序
- ListView中item的左右滑动
- dataadapter dataset 注意事项
- 优化SQL语句
- C#字符间转换
- ADO.net操作数据库总结
- 动态创建一个组件
- QTablewidget用法汇总
- Dialog整体架构详解