当前位置:   article > 正文

C++实现Socket传输json(UDP)_c++发送json格式数据

c++发送json格式数据

服务端发送给客户端,服务端有一串字符串,按照分隔符将其分割,并存入json,发给客户端,然后输出。

Json.hpp 下载地址https://github.com/nlohmann/json/tree/develop/include/nlohmann

一.服务端

main.cpp

  1. #include <stdio.h>
  2. #include<iostream>
  3. #include <winsock2.h>
  4. #include "json.hpp"
  5. #include <string>
  6. using json = nlohmann::json;
  7. //using namespace std;
  8. #pragma comment(lib, "ws2_32.lib")
  9. std::vector<std::string> split(const std::string &s, const std::string &seperator) {
  10. std::vector<std::string> result;
  11. typedef std::string::size_type string_size;
  12. string_size i = 0;
  13. while (i != s.size()) {
  14. //找到字符串中首个不等于分隔符的字母;
  15. int flag = 0;
  16. while (i != s.size() && flag == 0) {
  17. flag = 1;
  18. for (string_size x = 0; x < seperator.size(); ++x)
  19. if (s[i] == seperator[x]) {
  20. ++i;
  21. flag = 0;
  22. break;
  23. }
  24. }
  25. //找到又一个分隔符,将两个分隔符之间的字符串取出;
  26. flag = 0;
  27. string_size j = i;
  28. while (j != s.size() && flag == 0) {
  29. for (string_size x = 0; x < seperator.size(); ++x)
  30. if (s[j] == seperator[x]) {
  31. flag = 1;
  32. break;
  33. }
  34. if (flag == 0)
  35. ++j;
  36. }
  37. if (i != j) {
  38. result.push_back(s.substr(i, j - i));
  39. i = j;
  40. }
  41. }
  42. return result;
  43. }
  44. std::string jsonCreateString()
  45. {
  46. std::string s = "0.3,0.5,0.6,0.9,20,hanzu,w#0.3,0.3,0.6,0.9,21,han,m#0.3,0.5,0.1,0.9,100,zangzu,w";
  47. json j, data;
  48. int m, i, t = 1;
  49. std::vector<std::string> v = split(s, ",#");
  50. for (std::vector<std::string>::size_type i = 0; i != v.size(); ++i)
  51. {
  52. if (i % 7 == 0) data["x"] = v[i];
  53. else if (i % 7 == 1) data["y"] = v[i];
  54. else if (i % 7 == 2) data["w"] = v[i];
  55. else if (i % 7 == 3) data["h"] = v[i];
  56. else if (i % 7 == 4) data["age"] = v[i];
  57. else if (i % 7 == 5) data["race"] = v[i];
  58. else if (i % 7 == 6)
  59. {
  60. data["gender"] = v[i];
  61. j["p" + std::to_string(t++)] = data;
  62. }
  63. }
  64. j["ipAddress"] = "";
  65. j["ipPort"] = "";
  66. j["frame"] = "";
  67. //std::cout << j << std::endl;
  68. //system("pause");
  69. return j.dump();
  70. }
  71. int main(int argc, char* argv[])
  72. {
  73. WSADATA wsaData;
  74. WORD sockVersion = MAKEWORD(2, 2);
  75. if (WSAStartup(sockVersion, &wsaData) != 0)
  76. {
  77. return 0;
  78. }
  79. SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  80. if (serSocket == INVALID_SOCKET)
  81. {
  82. printf("socket error !");
  83. return 0;
  84. }
  85. sockaddr_in serAddr;
  86. serAddr.sin_family = AF_INET;
  87. serAddr.sin_port = htons(8888);
  88. serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
  89. if (bind(serSocket, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
  90. {
  91. printf("bind error !");
  92. closesocket(serSocket);
  93. return 0;
  94. }
  95. sockaddr_in remoteAddr;
  96. int nAddrLen = sizeof(remoteAddr);
  97. while (true)
  98. {
  99. char recvData[255];
  100. int ret = recvfrom(serSocket, recvData, 255, 0, (sockaddr *)&remoteAddr, &nAddrLen);
  101. if (ret > 0)
  102. {
  103. recvData[ret] = 0x00;
  104. printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
  105. printf(recvData);
  106. }
  107. std::string data = jsonCreateString();
  108. const char* sendData = data.c_str();
  109. //std::cout << sendData << std::endl;
  110. //const char* sendData = "发送\n";
  111. sendto(serSocket, sendData, strlen(sendData), 0, (sockaddr *)&remoteAddr, nAddrLen);
  112. //if (i++ > 1000) break;
  113. }
  114. closesocket(serSocket);
  115. WSACleanup();
  116. return 0;
  117. }

二.客户端

main.cpp

  1. #include <stdio.h>
  2. #include <winsock2.h>
  3. #include "json.hpp"
  4. #include <iostream>
  5. #pragma comment(lib, "ws2_32.lib")
  6. #define DATA_SIZE 300000
  7. using json = nlohmann::json;
  8. int main(int argc, char* argv[])
  9. {
  10. WORD socketVersion = MAKEWORD(2, 2);
  11. WSADATA wsaData;
  12. if (WSAStartup(socketVersion, &wsaData) != 0)
  13. {
  14. return 0;
  15. }
  16. SOCKET sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  17. sockaddr_in sin;
  18. sin.sin_family = AF_INET;
  19. sin.sin_port = htons(8888);
  20. sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
  21. int len = sizeof(sin);
  22. while (true)
  23. {
  24. const char* sendData = "";
  25. sendto(sclient, sendData, strlen(sendData), 0, (sockaddr *)&sin, len);
  26. int ret = 0;
  27. char recvData[DATA_SIZE];
  28. ret = recvfrom(sclient, recvData, DATA_SIZE, 0, (sockaddr *)&sin, &len);
  29. if (ret <= 0) break;
  30. if (ret > 0)
  31. {
  32. recvData[ret] = 0x00;
  33. //printf(recvData);
  34. json o = json::parse(recvData);
  35. for (json::iterator it = o.begin(); it != o.end(); ++it)
  36. std::cout << it.key() << " : " << it.value() << "\n";
  37. }
  38. }
  39. closesocket(sclient);
  40. WSACleanup();
  41. system("pause");
  42. return 0;
  43. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/522967
推荐阅读
相关标签
  

闽ICP备14008679号