#define ceshiip "127.0.0.1" #define RECEIVE 5550 //接收输入信息端口 #define SEND 5560 //接收端口 #include "UDP.h" #include "json/json.h" #include using namespace std; CUDP m_UDP; int q,w; char a; double b; HANDLE hThread; HANDLE hThread1; struct SUMMING { unsigned char sum1; unsigned int num1; int num2; }sumning; struct json_text { char name; double number; SUMMING truct; }on_text; void send_test(json_text* json1) { unsigned char c; Json::Value root; Json::Value truct; root["name"] = Json::Value(&(json1->name)); root["number"] = Json::Value(json1->number); truct["sum1"] = Json::Value(reinterpret_cast(&(json1->truct.sum1))); truct["num1"] = Json::Value(json1->truct.num1); truct["num2"] = Json::Value(json1->truct.num2); root["truct"] = Json::Value(truct); Json::FastWriter syle_writer; std::string s; s = syle_writer.write(root).c_str(); char buf[5000]; memset(buf, 0, 5000); memcpy(buf, s.c_str(), strlen(s.c_str())); m_UDP.init(); m_UDP.UDPSend(buf, 5000, 5560, ceshiip); /*root["struct"]["sum1"] = Json::Value(json1->truct.sum1); root["struct"]["num1"] = Json::Value(json1->truct.num1); root["struct"]["num2"] = Json::Value(json1->truct.num2);*/ } void* receive_test() { CUDP UDP_ForceData; UDP_ForceData.init(); char buf[500]; memset(buf, 0, 500); while (1) { CoInitialize(NULL); UDP_ForceData.UDPRecv(buf, 500, 5550, ceshiip); Json::Value va; Json::Reader re; re.parse(buf, va); a = *(va["name2"].asCString()); b = va["number2"].asDouble(); if (b != 0) w = 1; /*on_text.truct.num1 = va["truct"]["num1"].asInt(); on_text.truct.num2 = va["truct"]["num2"].asInt(); on_text.truct.sum1 = *(va["truct"]["sum1"].asCString());*/ CoUninitialize(); Sleep(30); } return 0; } void* receive_test_2() { CUDP UDP_ForceData; UDP_ForceData.init(); char buf[500]; memset(buf, 0, 500); while (1) { CoInitialize(NULL); UDP_ForceData.UDPRecv(buf, 500, 5540, ceshiip); Json::Value va; Json::Reader re; re.parse(buf, va); on_text.name = *(va["name"].asCString()); on_text.number = va["number"].asDouble(); on_text.truct.num1 = va["truct"]["num1"].asInt64(); on_text.truct.num2 = va["truct"]["num2"].asInt(); on_text.truct.sum1 = *(va["truct"]["sum1"].asCString()); if (on_text.number != 3) q = 1; CoUninitialize(); Sleep(30); } return 0; } int main() { unsigned a = -15; int c = 1; on_text.name = 'a'; on_text.number = 3; on_text.truct.num1 = -15; on_text.truct.num2 = 2; on_text.truct.sum1 = 'b'; cout << on_text.truct.sum1 << endl; a = 'a'; while (c < 10) { send_test(&on_text); c++; } if (NULL != (hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)receive_test, NULL, 0, NULL))) { cout << "线程1创建成功!" << endl; } if (NULL != (hThread1 = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)receive_test_2, NULL, 0, NULL))) { cout << "线程2创建成功!" << endl; } while (true) { if (q == 1 ) { cout << "接收到新数据1" << on_text.name << " " << on_text.number << endl; cout << "struct" << on_text.truct.num1 << " " << on_text.truct.num2 << on_text.truct.sum1 << endl; q = 0; } if (w == 1) { cout<<"接收到新数据2"<< a << " " << b << endl; w = 0; } } return 0; }