当前位置:   article > 正文

基于C++ socket通信的Json数据的传输解析_c++ json通信

c++ json通信

1.数据发送端---socket server端

Qt发送json数据,Qt提供了 QJsonObject 类和QJsonDocument 类,使用提供的两个类,可以实现将json数据转化成QString数据,再进行打包,增加协议头和长度,实现 head+len+data这种协议格式。

数据发送端是基于Qt开发的,下面是核心部分代码

  1. void TcpServer::sendUpFrame() {
  2. m_jsonObject->insert("speed", (int)m_speed);
  3. m_jsonObject->insert("rpm", (int)m_rpm);
  4. m_jsonObject->insert("waterTemp", (int)m_waterTemp);
  5. m_jsonObject->insert("fuel", (int)m_fuel);
  6. m_jsonObject->insert("gear", (int)m_gear);
  7. qDebug()<<*m_jsonObject;
  8. QByteArray byteArray;
  9. byteArray = QJsonDocument(*m_jsonObject).toJson();
  10. dataWrapper(&byteArray);
  11. qDebug()<<byteArray.size();
  12. m_tcpSocket->write(byteArray,byteArray.size());
  13. }
  14. void TcpServer::dataWrapper(QByteArray* byteArray) {
  15. char strSize[11];
  16. qsnprintf(strSize,11,"%010d",byteArray->size());
  17. byteArray->prepend(strSize);
  18. byteArray->prepend("AA");
  19. }

2.数据接受解析端---socket  client

数据接受解析是基于VS2017开发的,由于需要解析Json数据,所以下载了第三方开源的Json库,编译成一个静态的库用来解析Json数据,Json库的使用可参考 http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.html

下面是核心代码部分

  1. #define RECVDATALENTH 512
  2. #define RECV_DATA_MIN_LENGTH (10+2)
  3. #define HEAD_LENGTH 2
  4. void transceiverThread(HMIAdapter* hmiAdapter) {
  5. int send_len = 0;
  6. int recv_len = 0;
  7. bool lenFlag = false;
  8. int len = 0;
  9. char send_buf[RECVDATALENTH];
  10. char recv_buf[RECVDATALENTH];
  11. string recvData;
  12. recvData.clear();
  13. printf("initrecvDataLen: %d \n", recvData.size() );
  14. memset(recv_buf,0,sizeof(recv_buf));
  15. SOCKET s_server;
  16. SOCKADDR_IN server_addr;
  17. initialization();
  18. server_addr.sin_family = AF_INET;
  19. server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
  20. server_addr.sin_port = htons(1234);
  21. s_server = socket(AF_INET, SOCK_STREAM, 0);
  22. if (connect(s_server, (SOCKADDR *)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
  23. WSACleanup();
  24. }
  25. while (1) {
  26. printf("wait recv data \n" );
  27. memset(recv_buf,0,sizeof(recv_buf));
  28. recv_len = recv(s_server, recv_buf, RECVDATALENTH-1, 0);
  29. recv_buf[RECVDATALENTH-1] = '\0';
  30. string recvDataTemp = recv_buf;
  31. recvData.append(recvDataTemp);
  32. if(!recvData.compare("exit")) {
  33. break;
  34. }
  35. // vehicleInfoStruct* infoSturct = (vehicleInfoStruct*) recv_buf;
  36. string head;
  37. while (recvData.size() >= RECV_DATA_MIN_LENGTH)
  38. {
  39. head = recvData.substr(0, 2);
  40. if(head.compare("AA")) {
  41. bool foundAA = false;
  42. for(int i = 1; i < recvData.size(); i++) {
  43. head = recvData.substr(i, 2);
  44. if(!head.compare("AA")) {
  45. // find head AA
  46. foundAA = true;
  47. recvData.erase(0, i);
  48. break;
  49. }
  50. }
  51. if(foundAA == false) {
  52. recvData.erase();
  53. }
  54. } else {
  55. // find head AA
  56. // recvData.erase(0, 2); 注意:这个先不要移除,等找到完整的数据包,再一起移除
  57. if((recvData.size() > RECV_DATA_MIN_LENGTH) && (lenFlag == false)) {
  58. len = std::stoi(recvData.substr(HEAD_LENGTH,10));
  59. // recvData.erase(0, 10); // 注意:这个先不要移除,等找到完整的数据包,再一起移除
  60. lenFlag = true;
  61. printf("recvData.size():%d \n", recvData.size() );
  62. printf("len:%d \n", len );
  63. } else {
  64. //do action
  65. }
  66. if(recvData.size() >= (len + RECV_DATA_MIN_LENGTH)) {
  67. printf("-------------------- \n" );
  68. recvData.erase(0, RECV_DATA_MIN_LENGTH); // 注意:找到完整的数据包,一起移除
  69. hmiAdapter->onDataReceive(recvData.substr(0,len));
  70. recvData.erase(0, len);
  71. lenFlag = false;
  72. } else {
  73. break; //exit while
  74. //do action
  75. }
  76. }
  77. }
  78. // hmiAdapter->onDataReceive(infoSturct);
  79. }
  80. closesocket(s_server);
  81. WSACleanup();
  82. }

 

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

闽ICP备14008679号