博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Xsocket学习
阅读量:6876 次
发布时间:2019-06-26

本文共 2654 字,大约阅读时间需要 8 分钟。

1.xsocket是一个轻量级的基于NIO的服务器框架,用于开发高性能、可扩展、多线程的服务器。该框架封装了线程处理,异步读写等方面的操作。

  定义一个借口,继承IDataHandler,IConnectExceptionHandler, IConnectHandler, IDisconnectHandler:

public interface SocketDataHander extends IDataHandler,    IConnectExceptionHandler, IConnectHandler, IDisconnectHandler{}
View Code

  写出这个接口的实现方法:

 

1 public class SocketUnionPay implements SocketDataHander{ 2  3      4     @Override 5     public boolean onData(INonBlockingConnection connection) throws IOException, 6             BufferUnderflowException, ClosedChannelException, 7             MaxReadSizeExceededException { 8         // TODO Auto-generated method stub 9         start = System.currentTimeMillis();//获取当前时间10         byte[] data = connection.readBytesByLength(connection.available());  //监控接收到的数据11         12         //接收数据,转换为输出流,并且输出13         receiveBufByte = new Iso8583_Send_OutputStream(null);14         receiveBufByte.write(data, 0, data.length);15         System.out.println("##############################长度:"+data.length);16         17         if(receiveBufByte.length() > 9){18             LOG.info("接收数据:" + new String(receiveBufByte.getBufbyte()) + "  长度:" + receiveBufByte.length());19             20             byte[] sendata = unionPay_processingCentre.sendTreatmentTrade(receiveBufByte.getBufbyte());  //处理发送并返回21             byte[] rendata = unionPay_processingCentre.respTreatmentTrade(sendata);//数据返回处理22             LOG.info("返回数据:" + new String(rendata));23             connection.write(rendata);24         }25         26         end = System.currentTimeMillis();27         if(LOG.isTraceEnabled()){28             LOG.trace("########################前置处理耗时:"+(end-start)+"#######################");29         }30         return true;31     }32 33     @Override34     public boolean onConnectException(INonBlockingConnection connection,35             IOException e) throws IOException {36         // TODO Auto-generated method stub37         LOG.error("转发Bank连接异常..onConnectException...");38         return true;39     }40 41     @Override42     public boolean onConnect(INonBlockingConnection connection) throws IOException,43             BufferUnderflowException, MaxReadSizeExceededException {44         // TODO Auto-generated method stub45         LOG.info("Scoket前置打开连接..onConnect...");46         return true;47     }48 49     @Override50     public boolean onDisconnect(INonBlockingConnection connection) throws IOException {51         // TODO Auto-generated method stub52         LOG.info("Scoket前置关闭连接..onDisconnect...");53         connection.close();54         return true;55     }56 57 }

 

转载于:https://www.cnblogs.com/feitianshaoxai/p/5807319.html

你可能感兴趣的文章
linux 用户权限和组权限
查看>>
RPM的使用
查看>>
我的友情链接
查看>>
lvs
查看>>
原型图设计软件
查看>>
setTimeout和setIntelval的区别
查看>>
[C#]通过方法获得游戏人数和玩家姓名
查看>>
How to rotate a bitmap
查看>>
spring常见注解
查看>>
我的友情链接
查看>>
Linux 磁盘分区
查看>>
基于虚拟用户的邮件服务器在企业网中的应用
查看>>
android开发中shape的绘制
查看>>
在XP系统下安装ubuntu12.04
查看>>
数据库同步过程中一致性和完整性的保证
查看>>
VC++中图像处理类CBitmap的用法
查看>>
c# 建筑者模式的具体实现
查看>>
Javascript创建对象的7种模式
查看>>
linux 系统端口转发
查看>>
dis ospf peer 命令注解
查看>>