.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; }