c++ QTcpServer

cpp:

cpp 复制代码
   server = new QTcpServer(this);
    connect(server, &QTcpServer::newConnection, this, &PythonConsole::newConnection);
    if (!server->listen(QHostAddress::Any, 8000)) {
        qWarning("Unable to start the server: %s", qPrintable(server->errorString()));
    }

调试:

cpp 复制代码
    connect(server, &QTcpServer::newConnection, [this]() {
        QTcpSocket *socket = server->nextPendingConnection();
        qDebug() << "New connection from:" << socket->peerAddress().toString();
    });
cpp 复制代码
void PythonConsole::newConnection()
{
    clientConnection = server->nextPendingConnection();
    connect(clientConnection, &QTcpSocket::readyRead, this, &PythonConsole::readClient);
}

void PythonConsole::readClient()
{
    QByteArray data = clientConnection->readAll();
    QString command = QString::fromUtf8(data);
    runSource(command);
    clientConnection->close();
}

h:

cpp 复制代码
    QTcpServer *server;
    QTcpSocket *clientConnection;
相关推荐
hjxu20162 小时前
【OpenClaw 龙虾养成笔记一】在远程服务器,使用Docker安装OpenClaw
服务器·笔记·docker
sinat_255487812 小时前
读者、作家 Java集合学习笔记
java·笔记·学习
observe1013 小时前
输入输出1
笔记
sanshanjianke6 小时前
AI辅助网文创作理论研究笔记(五):测试环境的搭建和一些问题的讨论
人工智能·笔记·ai写作
云边散步7 小时前
godot2D游戏教程系列二(18)
笔记·学习·游戏
C羊驼7 小时前
C语言:随机数
c语言·开发语言·经验分享·笔记·算法
風清掦7 小时前
【江科大STM32学习笔记-09】USART串口协议 - 9.1 STM32 USART串口外设
笔记·stm32·单片机·嵌入式硬件·学习
xian_wwq8 小时前
【学习笔记】看参识模型
笔记·学习
猹叉叉(学习版)8 小时前
【系统分析师_知识点整理】 3.数据库系统
数据库·笔记·软考·系统分析师
李子琪。8 小时前
攀山的人
经验分享·笔记·百度·新浪微博