• C/C++ Qt 命令行版网络通信


    实现简单的结构体传输: 两端传输结构体。

    服务端

    #include <QCoreApplication>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <iostream>
    
    struct MyStruct
    {
        char uname[7];
        qint32 id;
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        MyStruct ptr;
        QTcpServer server;
    
        server.listen(QHostAddress::Any,9000);
        server.waitForNewConnection(100000);
    
        QTcpSocket *socket;
    
        socket = server.nextPendingConnection();
        while(socket->state() && QAbstractSocket::ConnectedState)
        {
            socket->waitForReadyRead(1000);
            socket->read((char*)&ptr,sizeof(MyStruct));
            std::cout << ptr.uname << std::endl;
    
            socket->write((char *)"sz",sizeof("sz"));
        }
    
        socket->close();
        server.close();
        return a.exec();
    }
    

    客户端

    #include <QCoreApplication>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <iostream>
    
    struct MyStruct
    {
        char uname[7];
        qint32 id;
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QTcpSocket socket;
    
        MyStruct ptr = {"abc",1001};
    
        socket.connectToHost(QHostAddress::LocalHost,9000);
    
        while(socket.state() && QAbstractSocket::ConnectedState)
        {
            socket.waitForConnected();
            socket.write((char *)&ptr,sizeof(MyStruct));
    
            socket.waitForBytesWritten();
            socket.waitForReadyRead(10000);
    
            char sz_ref[1024]={0};
    
            QByteArray qb = socket.readAll();
    
            QString str;
            str.prepend(qb);
            std::cout << str.toStdString() << std::endl;
        }
    
        socket.close();
        return a.exec();
    }
    

    实现简单传递字符串:

    client

    #include <QCoreApplication>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QTcpSocket socket;
        socket.connectToHost(QHostAddress::LocalHost,9000);
    
        if(socket.state() && QAbstractSocket::ConnectedState)
        {
            socket.waitForReadyRead(10000);
    
            QByteArray ref = socket.readAll();
    
            QString ref_string;
    
            ref_string.prepend(ref);
    
            std::cout << ref_string.toStdString() << std::endl;
        }
    
        socket.close();
        return a.exec();
    }
    

    server

    #include <QCoreApplication>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <iostream>
    
    struct MyStruct
    {
        char uname[7];
        qint32 id;
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        MyStruct ptr;
        QTcpServer server;
    
        server.listen(QHostAddress::Any,9000);
        server.waitForNewConnection(100000);
    
        QTcpSocket *socket;
    
        socket = server.nextPendingConnection();
        if(socket->state() && QAbstractSocket::ConnectedState)
        {
            QString str = "abcde";
            QByteArray bytes = str.toUtf8();
            socket->write(bytes.data(),bytes.length());
        }
    
        socket->close();
        server.close();
        return a.exec();
    }
    

    简单通信框架

    server

    #include <QCoreApplication>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QTcpServer server;
    
        server.listen(QHostAddress::Any,9000);
        server.waitForNewConnection(100000);
    
        QTcpSocket *socket;
    
        socket = server.nextPendingConnection();
        if(socket->state() && QAbstractSocket::ConnectedState)
        {
            // 获取CPU数据
            QString str = "GetCPU";
            QByteArray bytes = str.toUtf8();
            socket->write(bytes.data(),bytes.length());
    
            // 接收返回结果
            socket->waitForReadyRead(10000);
            QByteArray ref = socket->readAll();
            QString ref_buffer(ref);
            std::cout << "返回结果: " << ref_buffer.toStdString() << std::endl;
        }
    
        socket->close();
        server.close();
        return a.exec();
    }
    

    client

    #include <QCoreApplication>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <iostream>
    #include <QString>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QTcpSocket socket;
    
        while(1)
        {
            // 循环链接服务器
            socket.connectToHost(QHostAddress::LocalHost,9000);
    
            // 循环接收数据包,并处理请求
            while(socket.state() && QAbstractSocket::ConnectedState)
            {
                socket.waitForReadyRead(10000);
    
                // 将字节序转为字符串
                QByteArray ref = socket.readAll();
                QString ref_string(ref);
    
                if( (QString::compare(ref_string,"GetCPU")) == 0)
                {
                    std::cout << "获取CPU数据" << std::endl;
    
                    QString str = "94%";
                    QByteArray bytes = str.toUtf8();
                    socket.write(bytes.data(),bytes.length());
                }
                else if( QString::compare(ref_string,"GetMemory") == 0)
                {
                    std::cout << "ref memory" << std::endl;
                }
                else if( QString::compare(ref_string,"GetLoadAvg") == 0)
                {
                    std::cout << "ref load avg" << std::endl;
                }
            }
        }
    
        socket.close();
        return a.exec();
    }
    
    文章出处:https://www.cnblogs.com/LyShark/p/15407428.html
    版权声明:本博客文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章 [均为原创] 作品,转载请 [添加出处] ,您添加出处是我创作的动力!

    如果您恶意转载本人文章并被本人发现,则您的整站文章,将会变为我的原创作品,请相互尊重 !
    转载规范 点击阅读 如果您转载本人文章,则视为您默认同意此规范约定。
  • 相关阅读:
    如何选出一支优秀的债券基金?
    来吧,今天说说股票型基金
    来吧,帮你认识基金
    债券基金有A、有B、还有C,到底买哪种?
    基金到底分几类
    来吧,一个指标让你选到优秀的“固收+"基金
    来吧,带你读懂债券基金
    Windows 彻底删除文件
    IntelliJ IDEA 工程、模块常用操作
    IntelliJ IDEA 创建项目(开发第一个程序)
  • 原文地址:https://www.cnblogs.com/LyShark/p/15407428.html
Copyright © 2020-2023  润新知