Xsocket学习

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     @Override
34     public boolean onConnectException(INonBlockingConnection connection,
35             IOException e) throws IOException {
36         // TODO Auto-generated method stub
37         LOG.error("转发Bank连接异常..onConnectException...");
38         return true;
39     }
40 
41     @Override
42     public boolean onConnect(INonBlockingConnection connection) throws IOException,
43             BufferUnderflowException, MaxReadSizeExceededException {
44         // TODO Auto-generated method stub
45         LOG.info("Scoket前置打开连接..onConnect...");
46         return true;
47     }
48 
49     @Override
50     public boolean onDisconnect(INonBlockingConnection connection) throws IOException {
51         // TODO Auto-generated method stub
52         LOG.info("Scoket前置关闭连接..onDisconnect...");
53         connection.close();
54         return true;
55     }
56 
57 }

 

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在FreeRTOS中,`send`函数是用于在TCP/IP网络中发送数据的函数之一。它用于将数据从一个套接字发送到另一个套接字,提供了一种发送数据的基本方式。 FreeRTOS中的`send`函数通常用于网络套接字编程中,主要用于发送TCP数据。其函数原型如下: ```c int send(Socket_t xSocket, const void *pData, size_t xDataLength, BaseType_t xFlags); ``` 参数解释如下: - `xSocket`:要发送数据的套接字。 - `pData`:指向要发送数据的缓冲区的指针。 - `xDataLength`:要发送数据的长度。 - `xFlags`:发送标志,可用于控制发送行为,如是否阻塞等。 `send`函数会将指定长度的数据从缓冲区发送到套接字中,并返回实际发送的字节数。如果发送成功,返回值为发送的字节数;如果发送失败,返回值为负数。常见的错误码包括EAGAIN(资源暂时不可用)和ECONNRESET(连接被重置)等。 在FreeRTOS中,`send`函数通常是阻塞的,也就是说,如果发送缓冲区已满,则函数会一直阻塞等待直到有空间可用或超时发生。可以通过设置套接字选项来修改其阻塞行为。 需要注意的是,`send`函数只负责将数据发送到网络中,而不保证对方是否接收到数据。如果需要确保数据的可靠传输,可能需要结合使用其他机制,如使用TCP协议的确认机制、重传机制等。 总结起来,FreeRTOS中的`send`函数是用于发送TCP数据的函数,通过指定套接字、数据缓冲区、数据长度和发送标志来发送数据,并返回实际发送的字节数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值