本次搭建需要使用关于TCP的库,所以需要在配置文件中加入
QT += network
一、服务器的搭建
1、ui界面的构建
2、widget.h
主要是头文件的包络和函数的声明
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_startBtn_clicked();
void on_newConnection_slot();
void on_readyRead_slot();
void send(QString mes);
private:
Ui::Widget *ui;
//定义一个服务器指针
QTcpServer *server;
//定义一个客户端指针,存放连接的客户端
QList<QTcpSocket *> socketList;
};
#endif // WIDGET_H
3、widget.cpp