C++实现Socket传输json(UDP)

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

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

一.服务端

C++实现Socket传输json(UDP)_第1张图片

main.cpp

#include 
#include
#include 
#include "json.hpp"
#include 

using json = nlohmann::json;
//using namespace std;

#pragma comment(lib, "ws2_32.lib") 

std::vector split(const std::string &s, const std::string &seperator) {
	std::vector result;
	typedef std::string::size_type string_size;
	string_size i = 0;

	while (i != s.size()) {
		//找到字符串中首个不等于分隔符的字母;
		int flag = 0;
		while (i != s.size() && flag == 0) {
			flag = 1;
			for (string_size x = 0; x < seperator.size(); ++x)
				if (s[i] == seperator[x]) {
					++i;
					flag = 0;
					break;
				}
		}

		//找到又一个分隔符,将两个分隔符之间的字符串取出;
		flag = 0;
		string_size j = i;
		while (j != s.size() && flag == 0) {
			for (string_size x = 0; x < seperator.size(); ++x)
				if (s[j] == seperator[x]) {
					flag = 1;
					break;
				}
			if (flag == 0)
				++j;
		}
		if (i != j) {
			result.push_back(s.substr(i, j - i));
			i = j;
		}
	}
	return result;
}


std::string jsonCreateString()
{
	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";
	json j, data;
	int m, i, t = 1;

	std::vector v = split(s, ",#");
	for (std::vector::size_type i = 0; i != v.size(); ++i)
	{
		if (i % 7 == 0) data["x"] = v[i];
		else if (i % 7 == 1) data["y"] = v[i];
		else if (i % 7 == 2) data["w"] = v[i];
		else if (i % 7 == 3) data["h"] = v[i];
		else if (i % 7 == 4) data["age"] = v[i];
		else if (i % 7 == 5) data["race"] = v[i];
		else if (i % 7 == 6)
		{
			data["gender"] = v[i];
			j["p" + std::to_string(t++)] = data;
		}
	}
	j["ipAddress"] = "";
	j["ipPort"] = "";
	j["frame"] = "";

	//std::cout << j << std::endl;
	//system("pause");

	return j.dump();
} 


int main(int argc, char* argv[])
{
	WSADATA wsaData;
	WORD sockVersion = MAKEWORD(2, 2);
	if (WSAStartup(sockVersion, &wsaData) != 0)
	{
		return 0;
	}

	SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (serSocket == INVALID_SOCKET)
	{
		printf("socket error !");
		return 0;
	}

	sockaddr_in serAddr;
	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(8888);
	serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
	if (bind(serSocket, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
	{
		printf("bind error !");
		closesocket(serSocket);
		return 0;
	}

	sockaddr_in remoteAddr;
	int nAddrLen = sizeof(remoteAddr);
	while (true)
	{
		char recvData[255];
		int ret = recvfrom(serSocket, recvData, 255, 0, (sockaddr *)&remoteAddr, &nAddrLen);
		if (ret > 0)
		{
			recvData[ret] = 0x00;
			printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
			printf(recvData);
		}

		std::string data = jsonCreateString();
		
		const char* sendData = data.c_str();
		//std::cout << sendData << std::endl;
		//const char* sendData = "发送\n";
		sendto(serSocket, sendData, strlen(sendData), 0, (sockaddr *)&remoteAddr, nAddrLen);
		//if (i++ > 1000) break;
	
	}
	closesocket(serSocket);
	WSACleanup();
	return 0;
}

二.客户端

C++实现Socket传输json(UDP)_第2张图片

main.cpp

#include 
#include 
#include "json.hpp"
#include 

#pragma comment(lib, "ws2_32.lib")

#define DATA_SIZE 300000
using json = nlohmann::json;

int main(int argc, char* argv[])
{
	WORD socketVersion = MAKEWORD(2, 2);
	WSADATA wsaData;
	if (WSAStartup(socketVersion, &wsaData) != 0)
	{
		return 0;
	}
	SOCKET sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(8888);
	sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	int len = sizeof(sin);
	while (true)
	{
		const char* sendData = "";
		sendto(sclient, sendData, strlen(sendData), 0, (sockaddr *)&sin, len);

		int ret = 0;
		char recvData[DATA_SIZE];
		ret = recvfrom(sclient, recvData, DATA_SIZE, 0, (sockaddr *)&sin, &len);
		if (ret <= 0) break;
		if (ret > 0)
		{
			recvData[ret] = 0x00;
			//printf(recvData);
			json o = json::parse(recvData);
			for (json::iterator it = o.begin(); it != o.end(); ++it) 
				std::cout << it.key() << " : " << it.value() << "\n";
		}
	}

	closesocket(sclient);
	WSACleanup();
	system("pause");
	return 0;
}

 

你可能感兴趣的:(C++)