• socket qt基础版本


    .h
    #pragma once
    
    #include <QtWidgets/QMainWindow>
    #include "ui_QtGuiApplication1.h"
    #include <QtNetwork/QtNetwork>
    #include <iostream>
    class QtGuiApplication1 : public QMainWindow
    {
        Q_OBJECT
    
    public:
        QtGuiApplication1(QWidget *parent = Q_NULLPTR);
    
        QTcpServer* server;
        QTcpSocket* client_connection;
    public slots:
    void read_new_connection();
    void read_data();
    private:
        Ui::QtGuiApplication1Class ui;
    
    
    };
    .cpp
    #include "QtGuiApplication1.h"
    //QtServer
    QtGuiApplication1::QtGuiApplication1(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
        server = new QTcpServer;
        server->listen(QHostAddress::Any, 6665);
        connect(server, SIGNAL(newConnection()), this, SLOT(read_new_connection()));
    }
    
    void QtGuiApplication1::read_new_connection()
    {
        client_connection = new QTcpSocket;
        
        client_connection = server->nextPendingConnection();
        //qDebug() << client_connection->socketDescriptor;
        std::cout << "connect success!" << std::endl;
        connect(client_connection, SIGNAL(readyRead()), this, SLOT(read_data()));
    }
    
    void QtGuiApplication1::read_data()
    {
        QString str = client_connection->read(100);
        std::cout << str.toStdString() << std::endl;
    //    std::cout << client_connection->peerName().toStdString() << std::endl;
        
    }
  • 相关阅读:
    java 标准异常
    java 重新抛出异常
    java 异常链
    java 轨迹栈
    mysql死锁-非主键索引更新引起的死锁
    数据库事务
    JMS学习笔记(一)
    log4j中将SocketAppender将日志内容发送到远程服务器
    Kubernetes之kubectl常用命令
    java代理与动态代理的学习
  • 原文地址:https://www.cnblogs.com/xuhongfei0021/p/12585648.html
Copyright © 2020-2023  润新知