From 0a6a20fd75c001e420c60438ae2e90359af92c06 Mon Sep 17 00:00:00 2001 From: wangcheng <1846935001@qq.com> Date: Tue, 11 Jul 2023 00:43:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Include/DealJson.hpp | 31 +++++++++++++ src/Client.cc | 101 +++++++++++++++++++++++++++++++++++++++++++ src/DealJson.cc | 83 +++++++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 Include/DealJson.hpp create mode 100644 src/Client.cc create mode 100644 src/DealJson.cc diff --git a/Include/DealJson.hpp b/Include/DealJson.hpp new file mode 100644 index 0000000..613b632 --- /dev/null +++ b/Include/DealJson.hpp @@ -0,0 +1,31 @@ +#ifndef __DealJson_HPP__ +#define __DealJson_HPP__ + +#include "../include/nlohmann/json.hpp" +#include +#include +using std::map; +using std::string; +using json = nlohmann::json; + + +class DealJson +{ +public: + DealJson(int id, string word); + ~DealJson(); + + string packet(); // 打包,普通字符串--->json字符串 + map & unpack(); // 解包,json字符串--->json对象 + int decodeKey(); //id为100,解码关键字 + int decodePage(); //id为200,解码网页 + +private: + int _id; + string _word; + map _result; + json _jsonObj; +}; + + +#endif diff --git a/src/Client.cc b/src/Client.cc new file mode 100644 index 0000000..222f9ab --- /dev/null +++ b/src/Client.cc @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "../include/nlohmann/json.hpp" +#include "DealJson.hpp" + + +using std::cout; +using std::cin; +using std::endl; +using std::string; +using std::strncpy; +using std::isdigit; +using std::stoi; + + +#define SERV_IP "127.0.0.1" +#define SERV_PORT 8888 + +int main() +{ + int cfd; + struct sockaddr_in serv_addr; + char buf[BUFSIZ]; + + int nByte; + cfd = socket(AF_INET, SOCK_STREAM, 0); + if(-1 == cfd) + { + perror("socket error"); + exit(-1); + } + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(SERV_PORT); + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + /* inet_pton(cfd, SERV_IP, &serv_addr.sin_addr.s_addr); */ + connect(cfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); + + + while(1) + { + string _queryWord; + fgets(buf, sizeof(buf), stdin);//客户端输入查询词 + string qjson(buf);//buf转为string + _queryWord = qjson; + DealJson query(1, qjson); + qjson = query.packet();//生成查询词json串 + strncpy(buf, qjson.c_str(), sizeof(buf)-1); + buf[sizeof(buf)-1] = '\0';//string传给buf + write(cfd, buf, strlen(buf));//查询json串传给服务器 + + + nByte = read(cfd, buf, sizeof(buf));//获取服务器传回的关键字集合json串 + string kjson(buf);//buf转为string + DealJson key(1, kjson); + map keys = key.unpack();//获取解包后关键字集合map + for(auto & elem : keys){ //输出关键字集合 + cout< pages = page.unpack();//获取解包后网页集合map + for(auto & elem : keys){ //输出网页集合 + cout< + +using std::istringstream; + +DealJson::DealJson(int id, string word) +: _id(id) +, _word(word) +{} + +DealJson::~DealJson() {} + + +// 返回json 格式的字符串 +string DealJson::packet(){ + _jsonObj["id"] = _id; + _jsonObj["word"] = _word; // 此时word代表关键词字符串 或 网页嵌套json包字符串 + _jsonObj["length"] = _word.length(); //待查询内容的长度 + + return _jsonObj.dump(); +} + +map & DealJson::unpack(){ + //解包 + _jsonObj = json::parse(_word); + _id = _jsonObj["id"]; + + if(_id == 100){ + decodeKey(); + return _result; + }else{ + decodePage(); + return _result; + } + +} + +int DealJson::decodeKey(){ + _word = _jsonObj["word"]; + if(_word.length() != _jsonObj["length"]){ + _result[-1] = "关键词集合传输错误"; + return 1; + } + + if(_word == " "){ //关键词未命中 返回空格 + _result[0] = "未匹配到相关关键词"; + return 1; + } + + istringstream iss(_word); //传回的关键词集合以空格分隔 + string key; + int num = 1; + while(iss >> key){ + _result[num++] = key; + } + return 1; +} + +int DealJson::decodePage(){ + _word = _jsonObj["word"]; + if(_word.length() != _jsonObj["length"]){ + _result[-1] = "网页集合传输错误"; + return 1; + } + + if(_word == " "){ //关键词未命中 返回空格 + _result[0] = "未匹配到相关网页"; + return 1; + } + + json page = json::parse(_word); //_word中是网页查询json的字符串 + int num = 1; + for(auto & elem : page){ + string title = elem["title"]; + _result[num++] = title; + string url = elem["url"]; + _result[num++] = url; + string summary = elem["summary"]; + _result[num++] = summary; + } + return 1; +} + -- Gitee