• 计算机网络 CTCP 对客户端的封装


    完整代码如下

    类声明

    1. #ifndef CTCPMAP_H
    2. #define CTCPMAP_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. using namespace std;
    9. class client
    10. {
    11. public:
    12. client();
    13. ~client();
    14. //初始化网络
    15. bool InitnewWork();
    16. //销毁网络
    17. void DeleteWork();
    18. //发送数据
    19. bool sendDate(char*szbuf,int nLen);
    20. //接收数据
    21. void recvDate();
    22. //线程函数
    23. static DWORD WINAPI ThreadWork(LPVOID lpvoid);
    24. private:
    25. SOCKET m_client;
    26. HANDLE m_hThread;
    27. bool bFlagQuit;
    28. };
    29. #endif // CLIENT_H

    类定义

    1. #include "client.h"
    2. client::client()
    3. {
    4. m_client=0;
    5. m_hThread=NULL;
    6. bFlagQuit=true;
    7. }
    8. client::~client()
    9. {
    10. }
    11. bool client::InitnewWork()
    12. {
    13. WORD wVersionRequested;
    14. WSADATA wsaData;
    15. int err;
    16. wVersionRequested = MAKEWORD(2, 2);
    17. err = WSAStartup(wVersionRequested, &wsaData);
    18. if (err != 0) {
    19. printf("WSAStartup failed with error: %d\n", err);
    20. return false;
    21. }
    22. if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
    23. DeleteWork();
    24. return false;
    25. }
    26. else
    27. printf("The Winsock 2.2 dll was found okay\n");
    28. //创建套接字
    29. m_client=socket(AF_INET,SOCK_STREAM,0);
    30. if(m_client==INVALID_SOCKET)
    31. {
    32. DeleteWork();
    33. return false;
    34. }
    35. //connect()
    36. //绑定IP
    37. sockaddr_in addrserver;
    38. addrserver.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
    39. addrserver.sin_port=htons(8899);
    40. addrserver.sin_family=AF_INET;
    41. if(SOCKET_ERROR==connect(m_client,(sockaddr*)&addrserver,sizeof(addrserver)))
    42. {
    43. DeleteWork();
    44. return false;
    45. }
    46. //创建线程
    47. m_hThread=CreateThread(0,0,&ThreadWork,this,0,0);
    48. return true;
    49. }
    50. DWORD WINAPI client::ThreadWork(LPVOID lpvoid)
    51. {
    52. client* pthis=(client* ) lpvoid;
    53. while(pthis->bFlagQuit)
    54. {
    55. }
    56. return 0;
    57. }
    58. void client::DeleteWork()
    59. {
    60. bFlagQuit=false;
    61. if(m_hThread)
    62. {
    63. if(WAIT_TIMEOUT==WaitForSingleObject(m_hThread,100))
    64. TerminateThread(m_hThread,-1);
    65. CloseHandle(m_hThread);
    66. m_hThread=NULL;
    67. }
    68. if(m_client)
    69. {
    70. closesocket(m_client);
    71. m_client=0;
    72. }
    73. WSACleanup();
    74. }
    75. bool client::sendDate(char*szbuf,int nLen)
    76. {
    77. if(!szbuf||nLen<=0)
    78. return false;
    79. if(send(m_client,(char*)&nLen,sizeof(int),0)<=0)
    80. return false;
    81. if(send(m_client,szbuf,nLen,0)<=0)
    82. return false;
    83. return true;
    84. }
    85. void client::recvDate()
    86. {
    87. int nRecvNum;
    88. int nPackNum;
    89. char *szbuf;
    90. int noffest;
    91. while(bFlagQuit)
    92. {
    93. nRecvNum=recv(m_client,(char*)&nPackNum,sizeof(int),0);
    94. if(nRecvNum<=0)
    95. {
    96. DeleteWork();
    97. break;
    98. }
    99. if(nPackNum<=0) continue;
    100. szbuf=new char[nPackNum];
    101. noffest=0;
    102. while(nPackNum)
    103. {
    104. nRecvNum=recv(m_client,szbuf+noffest,nPackNum,0);
    105. if(nRecvNum>0)
    106. {
    107. nPackNum-=nRecvNum;
    108. noffest+=nRecvNum;
    109. }
    110. }
    111. }
    112. }

    主函数的测试

    1. #include
    2. #include
    3. #include
    4. #include"client.h"
    5. using namespace std;
    6. int main(int argc, char *argv[])
    7. {
    8. client cli;
    9. if(cli.InitnewWork())
    10. {
    11. cout<<"connect sucess"<
    12. char szbuf[1024];
    13. cin>>szbuf;
    14. cli.sendDate(szbuf,sizeof (szbuf));
    15. }
    16. }

  • 相关阅读:
    设计模式简介
    I2S总线介绍以及通信注意事项
    LeetCode_哈希表_简单_594.最长和谐子序列
    ES主要功能特性和使用场景
    VTK网格细分-vtkAdaptiveSubdivisionFilter
    金融业务系统云原生技术转型:从传统架构到云原生的跨越
    Go语言入门之数组切片
    vue 使用qrcode生成二维码并可下载保存
    postman 获取HTML 里name=token 的值
    【软考】-- 计算机组成体系结构(上)【我的1024】
  • 原文地址:https://blog.csdn.net/van9527/article/details/126463245