TCPClient.h 915 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef __TCP_CLIENT_HEADER__
  2. #define __TCP_CLIENT_HEADER__
  3. #include "Global.h"
  4. // winsocket库;
  5. #include <Ws2tcpip.h>
  6. #pragma comment(lib,"Ws2_32.lib")
  7. #pragma once
  8. #ifdef IRCONTROL_EXPORTS
  9. #define IRCONTROL_API __declspec(dllexport)
  10. #else
  11. #define IRCONTROL_API __declspec(dllimport)
  12. #endif
  13. class IRCONTROL_API CTCPClient
  14. {
  15. public:
  16. CTCPClient();
  17. ~CTCPClient();
  18. // 初始化套接字库;
  19. bool InitSocket();
  20. // 连接服务器;
  21. bool Connect(std::string ip, int port);
  22. // 连接服务器;
  23. bool SelectConnect(std::string ip, int port, int time_out = 3);
  24. // 断开连接;
  25. void DisConnect();
  26. // 发送数据;
  27. bool Send(std::string s_data, std::string &r_data);
  28. // 重连;
  29. void ReConnect(DWORD dwError = WSAENETRESET);
  30. protected:
  31. bool checkEOM(std::string& data);
  32. private:
  33. // 客户端套接字;
  34. SOCKET m_socket;
  35. // 客户端地址;
  36. sockaddr_in m_sin;
  37. // ip;
  38. std::string m_ip;
  39. // port;
  40. int m_port;
  41. };
  42. #endif //__TCP_CLIENT_HEADER__