TcpipSCF.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #define TCPIPSCF_API
  3. #define TCPIPSCF_C_API extern "C"
  4. #include "SCF.h"
  5. #include "ResDataObject.h"
  6. #include <sys/socket.h> // Linux socket API
  7. #include <netdb.h> // 网络数据库操作
  8. #include <unistd.h> // 系统调用
  9. #include <fcntl.h> // 文件控制
  10. #include <netinet/in.h> // 网络地址
  11. #include <arpa/inet.h> // IP地址转换
  12. #include <algorithm> // 转换函数
  13. #include <cctype> // 字符处理
  14. // 声明导出函数
  15. TCPIPSCF_C_API SCF* GetSCF();
  16. TCPIPSCF_C_API void ReleseSCF(SCF* p);
  17. // TCP/IP实现的SCF类
  18. class TCPIPSCF_API TcpipSCF : public SCF {
  19. int hCom; // 在Linux中不使用,保留兼容性
  20. // 内部连接方法
  21. int ConnectTcpip(const char* ip, unsigned short port, unsigned int timeout);
  22. public:
  23. TcpipSCF();
  24. virtual ~TcpipSCF();
  25. // 连接方法
  26. int Connect(ResDataObject& Connectprameters, PacketParser callback = NULL,
  27. SCF_TRANSFERTYPE TransferType = SCF_NORMAL_TRANSFER,
  28. unsigned int CommunicateTimeout = 75000);
  29. // 断开连接
  30. void Disconnect();
  31. // 检查连接状态
  32. bool isConnected();
  33. // 获取连接类型
  34. const char* GetConnectionType();
  35. // 发送数据包
  36. int SendPacket(const char* pPacket, unsigned int length, unsigned int timeout);
  37. int SendPacket(SCFPacket* pPacket, unsigned int timeout);
  38. protected:
  39. string* m_pIp; // IP地址
  40. unsigned short m_Port; // 端口号
  41. bool Connected; // 连接状态
  42. int Socketfd; // 套接字描述符
  43. sockaddr_in sa; // 地址结构
  44. int Error; // 错误代码
  45. // 读取数据
  46. int ReadData(char* pPacket, unsigned int length, unsigned int timeout);
  47. // 主机名解析
  48. hostent* Gethostbyname(const char* name1);
  49. // 服务名解析
  50. servent* Getservbyname(const char* name, const char* prot);
  51. };