test_tcpclient.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <iostream>
  2. #include <string>
  3. #include "tcpclient.h"
  4. #include "mswin_special/sys/DumpFile.h"
  5. using namespace std;
  6. using namespace uv;
  7. std::string serverip;
  8. int call_time = 0;
  9. bool is_exist = false;
  10. void CloseCB(int clientid, void* userdata)
  11. {
  12. fprintf(stdout, "cliend %d close\n", clientid);
  13. TCPClient* client = (TCPClient*)userdata;
  14. client->Close();
  15. }
  16. void ReadCB(const NetPacket& packet, const unsigned char* buf, void* userdata)
  17. {
  18. fprintf(stdout,"call time %d\n",++call_time);
  19. if (call_time > 5000) {
  20. return;
  21. }
  22. char senddata[256] = {0};
  23. TCPClient* client = (TCPClient*)userdata;
  24. sprintf(senddata, "****recv server data(%p,%d)", client, packet.datalen);
  25. fprintf(stdout, "%s\n", senddata);
  26. NetPacket tmppack = packet;
  27. tmppack.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
  28. std::string retstr = PacketData(tmppack, (const unsigned char*)senddata);
  29. if (client->Send(&retstr[0], retstr.length()) <= 0) {
  30. fprintf(stdout, "(%p)send error.%s\n", client, client->GetLastErrMsg());
  31. }
  32. }
  33. int main(int argc, char** argv)
  34. {
  35. if (argc != 3) {
  36. fprintf(stdout, "usage: %s server_ip_address clientcount\neg.%s 192.168.1.1 50\n", argv[0], argv[0]);
  37. return 0;
  38. }
  39. DeclareDumpFile();
  40. serverip = argv[1];
  41. const int clientsize = std::stoi(argv[2]);
  42. TCPClient** pClients = new TCPClient*[clientsize];
  43. TCPClient::StartLog("log/");
  44. int i = 0;
  45. char senddata[256];
  46. for (int i = 0; i < clientsize; ++i) {
  47. pClients[i] = new TCPClient(0x01, 0x02);
  48. pClients[i]->SetRecvCB(ReadCB, pClients[i]);
  49. pClients[i]->SetClosedCB(CloseCB, pClients[i]);
  50. if (!pClients[i]->Connect(serverip.c_str(), 12345)) {
  51. fprintf(stdout, "connect error:%s\n", pClients[i]->GetLastErrMsg());
  52. } else {
  53. fprintf(stdout, "client(%p) connect succeed.\n", pClients[i]);
  54. }
  55. memset(senddata, 0, sizeof(senddata));
  56. sprintf(senddata, "client(%p) call %d", pClients[i], ++call_time);
  57. NetPacket packet;
  58. packet.header = 0x01;
  59. packet.tail = 0x02;
  60. packet.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
  61. std::string str = PacketData(packet, (const unsigned char*)senddata);
  62. if (pClients[i]->Send(&str[0], str.length()) <= 0) {
  63. fprintf(stdout, "(%p)send error.%s\n", pClients[i], pClients[i]->GetLastErrMsg());
  64. } else {
  65. //fprintf(stdout,"发送的数据为:\n");
  66. // for (int i=0; i<str.length(); ++i) {
  67. // fprintf(stdout,"%02X",(unsigned char)str[i]);
  68. // }
  69. // fprintf(stdout,"\n");
  70. fprintf(stdout, "send succeed:%s\n", senddata);
  71. }
  72. }
  73. while (!is_exist) {
  74. Sleep(10);
  75. }
  76. return 0;
  77. }