tcp客户端向tcp服务器发送json文件,服务器转存为json文件

客户端:

cpp 复制代码
void socket::send_msg(QString file_name)
{
    qDebug() <<"socket::send_msg(QString file_name):" << QThread::currentThread();
    //读取json文件
    QFile file(file_name); // fileName文件的路径
    if (file.open(QIODevice::ReadOnly)) {
        QByteArray data = file.readAll();
        file.close();
        qDebug() << "[out]: "<<data;
        tcpClient->write(data);
    }
}

就是根据文件路径--->QFile--->QByteArray--->传递给服务器

服务器:

cpp 复制代码
void widget::onSocketReadyRead()
{//读取缓冲区行文本
    qDebug() << "read:" << tcpSocket;
    QByteArray ba=tcpSocket->readAll();
    write_to_json_file(ba);
}
void widget::write_to_json_file(QByteArray& ba)
{
    QJsonDocument document = QJsonDocument::fromJson(ba);
    QString str = QApplication::applicationDirPath() + "/2.json";
    qDebug() << "filepath:" << str;
    QFile file(str);
    if (file.open(QIODevice::WriteOnly)) {
        file.write(document.toJson());
        file.close();
    }
}

从客户端接收数据--->QByteArray--->QJsonDocument--->QFile--->xxx.json文件

相关推荐
蜗牛一步步往上爬6 分钟前
第一章 初识Linux shell
linux·运维·服务器
无足鸟ICT24 分钟前
【RHCA+】系统调优配置文件
linux·服务器
FRing0339 分钟前
vue2干货总结10(json-server、vant-ui、postcss插件、axios二次封装、图形验证码、toast轻提示)
开发语言·前端·vue.js·ui·json·vue2·postcss
饿了吃洗衣凝珠1 小时前
DNS域名解析服务器
运维·服务器
布莱克6052 小时前
Swift 字符串详解:从基础用法到性能与最佳实践
服务器·开发语言·ssh·字符串·swift
用户298698530142 小时前
使用 Python 将 JSON 转换为 Word 文档的实践指南
python·json·api
时间的拾荒人2 小时前
Shell 脚本核心语法详解(下篇):流程控制、函数、重定向与文件包含
linux·运维·服务器·bash·脚本编程
饿了吃洗衣凝珠3 小时前
SELinux
服务器·开发语言·php
进击的荆棘4 小时前
Linux网络——网络基础概念
linux·运维·服务器·网络
.YM.Z4 小时前
从操作系统进入Linux进程概念及进程状态
linux·运维·服务器