赞
踩
Server界面代码:
系统管理文件:
- QT += core gui network
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- CONFIG += c++11
-
- # The following define makes your compiler emit warnings if you use
- # any Qt feature that has been marked deprecated (the exact warnings
- # depend on your compiler). Please consult the documentation of the
- # deprecated API in order to know how to port your code away from it.
- DEFINES += QT_DEPRECATED_WARNINGS
-
- # You can also make your code fail to compile if it uses deprecated APIs.
- # In order to do so, uncomment the following line.
- # You can also select to disable deprecated APIs only up to a certain version of Qt.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
-
- SOURCES += \
- main.cpp \
- widget.cpp
-
- HEADERS += \
- widget.h
-
- FORMS += \
- widget.ui
-
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
头文件:
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
- #include <QTcpServer> //服务器头文件
- #include <QTcpSocket> //客户端头文件
- #include <QList> //链表头文件用来存放客户端容器
- #include <QDebug>
- #include <QMessageBox> //消息对话框
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
- private slots:
- void on_pushButton_clicked();
- void newConnection_slot();
- void readyRead_slot(); //自定义处理readyRead信号的槽函数
-
- private:
- Ui::Widget *ui;
-
- //定义服务器指针
- QTcpServer *server;
-
- //定义客户端指针链表容器
- QList<QTcpSocket *> clientList;
-
- };
- #endif // WIDGET_H
主函数:
- #include "widget.h"
-
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
主要功能函数:
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //给服务器指针实例化对象
- server = new QTcpServer(this); //服务器创建完成
-
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- //启动服务器按钮对应的槽函数
- void Widget::on_pushButton_clicked()
- {
- //获取UI界面的端口号
- quint16 port = ui->lineEdit->text().toUInt();
- //将服务器设置为被动监听状态
- //bool QTcpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)
- //参数1:要监听的主机地址,如果是any,表示监听所有主机地址,也可以给特定的主机地址进行监听
- //参数2:通过指定的端口号进行访问服务器,如果是0,表示由服务器自动分配。如果非0,则表示指定端口号
- //返回值:成功返回真,失败返回假
- if(!server->listen(QHostAddress::Any,port))
- {
- QMessageBox::critical(this, "失败", "服务器启动失败");
- }else{
- QMessageBox::information(this, "成功", "服务器启动成功");
- }
-
- //执行到这表明服务器启动成功,并对客户端连接进行监听,如果有客户端向服务器发来连接请求,那么该服务器就会自动发射一个newConnection信号
- //我们可以将信号连接到对应的槽函数中处理相关逻辑
- connect(server, &QTcpServer::newConnection, this, &Widget::newConnection_slot);
- }
-
- void Widget::newConnection_slot()
- {
- qDebug() <<"有客户端申请连接";
-
- //获取最新连接的客户端套接字
- //[virtual] QTcpSocket *QTcpServer::nextPendingConnection()
- QTcpSocket *s = server->nextPendingConnection();
-
- //将获取的套接字存放到客户端容器中
- clientList.push_back(s);
-
- //此时,客户端就和服务器建立起来联系了
- //如果客户端有数据向服务器发送过来,那么该套接字就会自动发送一个readyread信号
- //我们可以将该信号连接到自定义的槽函数中处理相关逻辑
- connect(s, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);
- }
-
- //关于readyRead信号对应槽函数的实现
- void Widget::readyRead_slot()
- {
- //删除客户端链表中的无效客户端套接字
- for(int i=0; i<clientList.count(); i++)
- {
- //判断套接字的状态
- //函数原型 SocketState state() const;
- //功能:返回客户端状态
- //返回值:客户端状态,如果是0,表示无连接
- if(clientList[i]->state() == 0)
- {
- clientList.removeAt(i); //将下标为i的客户端移除
- }
- }
-
- //遍历所有客户端,查看是哪个客户端发来数据
- for(int i=0; i<clientList.count(); i++)
- {
- //函数原型:qint64 bytesAvailable() const override;
- //功能:返回当前客户端套接字中的可读数据字节个数
- //返回值:当前客户端待读的字节数,如果该数据0,表示无待读数据
- if(clientList[i]->bytesAvailable() != 0)
- {
- //读取当前客户端的相关数据
- //函数原型:QByteArray readAll();
- //功能:读取当前套接字中的所有数据,并返回一个字节数组
- //返回值:数据的字节数组
- QByteArray msg = clientList[i]->readAll();
-
- //将数据战术到ui界面上
- ui->listWidget->addItem(QString::fromLocal8Bit(msg));
-
- //将接收到的该消息,发送给所有客户端
- for(int j=0; j<clientList.count(); j++)
- {
- clientList[j]->write(msg);
- }
- }
- }
-
- }
所用组件:
Client界面代码:
系统管理文件:
- QT += core gui network
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- CONFIG += c++11
-
- # The following define makes your compiler emit warnings if you use
- # any Qt feature that has been marked deprecated (the exact warnings
- # depend on your compiler). Please consult the documentation of the
- # deprecated API in order to know how to port your code away from it.
- DEFINES += QT_DEPRECATED_WARNINGS
-
- # You can also make your code fail to compile if it uses deprecated APIs.
- # In order to do so, uncomment the following line.
- # You can also select to disable deprecated APIs only up to a certain version of Qt.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
-
- SOURCES += \
- main.cpp \
- widget.cpp
-
- HEADERS += \
- widget.h
-
- FORMS += \
- widget.ui
-
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
头文件:
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
- #include <QTcpSocket>
- #include <QMessageBox>
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
- private slots:
- void on_pushButton_2_clicked();
- void connected_slot(); //自定义处理信号的槽函数
- void readyRead_slot(); //自定义处理readyRead信号的槽函数
- void disconnected_slot(); //自定义处理disconnected信号的槽函数
-
- void on_pushButton_clicked();
-
- void on_pushButton_3_clicked();
-
- private:
- Ui::Widget *ui;
-
- //定义一个客户端指针
- QTcpSocket *socket;
-
- //用户名
- QString userName;
- };
- #endif // WIDGET_H
主函数:
- #include "widget.h"
-
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
主要功能函数:
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //给客户端指针实例化空间
- socket = new QTcpSocket(this);
-
- //如果连接服务器成功,该客户端就会发射一个connected信号
- //我们可以将该信号连接到自定义的槽函数中处理相关逻辑
- //由于该连接只需要连接一次。所有在构造函数中即可
- connect(socket, &QTcpSocket::connected, this, &Widget::connected_slot);
-
- //客户端与服务器连接成功后,如果服务器向客户端发来数据,那么该客户端就会自动发射一个readyRead信号
- //我们可以将该信号连接到自定义槽函数中处理相关逻辑
- connect(socket, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);
-
- //当客户端与服务器断开连接后,该客户端就会自动发射1一个disconnected信号
- //我们可以将该信号与自定义的槽函数连接
- //由于只需要连接一下,所有该连接写到构造函数即可
- connect(socket, &QTcpSocket::disconnected, this, &Widget::disconnected_slot);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- //连接服务器按钮对应的槽函数
- void Widget::on_pushButton_2_clicked()
- {
- //获取UI界面的信息
- userName = ui->lineEdit_2->text(); //获取用户名
- QString hostName = ui->lineEdit_3->text(); //获取主机地址
- quint16 port = ui->lineEdit_4->text().toUInt(); //获取端口号
-
-
- //调用函数连接到主机
- //函数原型:[virtual] void QAbstractSocket::connectToHost(const QString &hostName, quint16 port)
- //参数1:服务器的主机地址
- //参数2:端口号
- //返回值:无
- socket->connectToHost(hostName,port);
-
-
- }
-
- //关于处理connected信号的槽函数
- void Widget::connected_slot()
- {
- QMessageBox::information(this,"成功","连接服务器成功");
-
- //顺便向服务器发送一条消息,说:xxx进入聊天室
- QString msg = userName + "进入聊天室";
-
- socket->write(msg.toLocal8Bit());
- }
-
- //关于readyRead信号对应槽函数的实现
- void Widget::readyRead_slot()
- {
- //读取该客户端中的数据
- //返回值:QBytearray
- QByteArray msg = socket->readAll();
-
- //将数据展示在UI界面
- ui->listWidget->addItem(QString::fromLocal8Bit(msg));
-
- }
-
-
- void Widget::on_pushButton_clicked()
- {
- //获取UI界面中的编辑的文本内容
- QString m = ui->lineEdit->text();
-
- //整合要发送的信息
- QString msg = userName + ": " + m;
-
- //将消息发送给服务器
- socket->write(msg.toLocal8Bit());
-
- //将消息编辑框中的内容清空
- ui->lineEdit->clear();
- }
-
-
- //断开服务器按钮对应的槽函数
- void Widget::on_pushButton_3_clicked()
- {
- //准备要发送的信息
- QString msg = userName + ": 离开聊天室";
- socket->write(msg.toLocal8Bit());
-
- //调用成员函数disconnectFromHost
- //函数原型:virtual void disconnectFromHost();
- //功能:断开服务器与客户端的连接
- //参数:无
- //返回值:无
- socket->disconnectFromHost();
- }
-
- //disconn信号对应槽函数的实现
- void Widget::disconnected_slot()
- {
- QMessageBox::information(this, "退出", "断开成功");
- }
所用组件:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。