前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qt项目网络聊天室设计

Qt项目网络聊天室设计

作者头像
DeROy
修改2020-08-25 23:54:50
2.2K0
修改2020-08-25 23:54:50
举报
文章被收录于专栏:编程学习基地编程学习基地

网络聊天室

Qt网络聊天室服务端

网络聊天室程序

  1. 基于TCP的可靠连接(QTcpServer、QTcpSocket)
  2. 一个服务器,多个客户端

3. 服务器接收到某个客户端的请求以及发送信息,经服务器发给其它客户端 最终实现一个共享聊天内容的聊天室!

QTcpServer

提供一个TCP基础服务类 继承自QObject,这个类用来接收到来的TCP连接,可以指定TCP端口或者用QTcpServer自己挑选一个端口,可以监听一个指定的地址或者所有的机器地址。

QTcpServer的信号:

代码语言:javascript
复制
newConnection()//有新连接连接时触发该信号

配置

pro文件添加

代码语言:javascript
复制
QT += network

获取当前设备所有ip地址

枚举设备所有ip地址

代码语言:javascript
复制
QList<QHostAddress> ipList = QNetworkInterface::allAddresses();
QStringList addressStrList;
addressStrList.clear();

for(int index = 0;index<ipList.size();index++)
{
    if(ipList.at(index).isNull())   continue;         //如果地址为空,则去掉
    QAbstractSocket::NetworkLayerProtocol protocol = ipList.at(index).protocol();
    if(protocol != QAbstractSocket::IPv4Protocol)   continue;           //只取IPV4的地址
    addressStrList.append(ipList.at(index).toString());
}
ui->comboBox_Address->addItems(addressStrList);

listen() close()

调用listen()来监听所有的连接 调用close()来关闭套接字,停止对连接的监听。

如果监听有错误,serverError()返回错误的类型。调用errorString()来把错误打印出来

代码语言:javascript
复制
bool isListening()	const

当服务端正在监听连接时候返回真,否则返回假

代码语言:javascript
复制
QString serverAddressStr = ui->comboBox_Address->currentText();     //获取服务器ip地址
quint16 port = ui->lineEdit_port->text().toInt();                   //获取服务器端口
QHostAddress serverAddress = QHostAddress(serverAddressStr);        //初始化协议族

if(mServer->isListening())
{
    //在监听状态 取消监听
    mServer->close();
}
else
{
    //不在监听状态 开始监听
    if(mServer->listen(serverAddress, port))
    {
        //监听成功
        qDebug() << "Listen Ok!!";
    }
    else
    {
        //监听失败
        QMessageBox::warning(this, "Tcp Server Listen Error", mServer->errorString());
    }
}

当监听连接时候,可以调用serverAddress()serverPort()来返回服务端的地址和端口。

newConnection()

每当一个newConnection新的客户端连接到服务端就会发射信号newConnection()

调用nextPendingConnection()来接受待处理的连接。返回一个连接的QTcpSocket(),我们可以用这个返回的套接字和客户端进行连接

代码语言:javascript
复制
private slots:
    void newConnectionSlot();           //新连接
代码语言:javascript
复制
//处理新连接客户端
connect(mServer, SIGNAL(newConnection()),this, SLOT(newConnectionSlot()));
代码语言:javascript
复制
void Widget::newConnectionSlot()
{
    mClient = mServer->nextPendingConnection();
    connect(mClient, SIGNAL(readyRead()),this, SLOT(readyReadSlot()));//接收消息
    connect(mClient, SIGNAL(disconnected()),this, SLOT(disconnectedSlot()));//断开连接
}

SINGL

readyRead()

客户端有数据发送至服务端时触发该信号

代码语言:javascript
复制
connect(mClient, SIGNAL(readyRead()),this, SLOT(readyReadSlot()));          //接收消息
isReadable

是否可读

readAll

读取客户端发送过来的全部信息

代码语言:javascript
复制
if(mClient->isReadable())
{
	QByteArray recvArray = mClient->readAll();
}
disconnected()

当客户端断开连接时触发该信号

代码语言:javascript
复制
connect(mClient, SIGNAL(disconnected()),this, SLOT(disconnectedSlot()));    //断开连接

UI设计

服务端UI设计

TcpServer项目训练

widget.h

代码语言:javascript
复制
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QNetworkInterface>
#include <QAbstractSocket>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QDateTime>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:
    void on_pushButton_listen_clicked();//开始监听
    void newConnectionSlot();           //新连接
    void disconnectedSlot();            //断开连接
    void readyReadSlot();               //接收消息的槽函数
    void on_pushButton_send_clicked();	//发送消息

private:
    void enumIpAddress();				//枚举当前设备所有端口
private:
    Ui::Widget *ui;

    QTcpServer *mServer;				//服务器
    QTcpSocket *mClient = nullptr;		//客户端
    QList<QTcpSocket *>mClientList;		//客户端链表
};

#endif // WIDGET_H

widget.cpp

代码语言:javascript
复制
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    mServer = new QTcpServer;
    enumIpAddress();

    //处理新连接客户端
    connect(mServer, SIGNAL(newConnection()),this, SLOT(newConnectionSlot()));
}

Widget::~Widget()
{
    delete ui;
}
void Widget::enumIpAddress()
{
    QList<QHostAddress> ipList = QNetworkInterface::allAddresses();
    QStringList addressStrList;
    addressStrList.clear();

    for(int index = 0;index<ipList.size();index++)
    {
        if(ipList.at(index).isNull())   continue;         //如果地址为空,则去掉
        QAbstractSocket::NetworkLayerProtocol protocol = ipList.at(index).protocol();
        if(protocol != QAbstractSocket::IPv4Protocol)   continue;           //只取IPV4的地址
        addressStrList.append(ipList.at(index).toString());
    }
    ui->comboBox_Address->addItems(addressStrList);
}

void Widget::on_pushButton_listen_clicked()
{
    QString serverAddressStr = ui->comboBox_Address->currentText();     //获取服务器ip地址
    quint16 port = ui->lineEdit_port->text().toInt();                   //获取服务器端口
    QHostAddress serverAddress = QHostAddress(serverAddressStr);        //初始化协议族

    if(mServer->isListening())
    {
        //在监听状态 取消监听
        mServer->close();
        ui->pushButton_listen->setText("监听");
    }
    else
    {
        //不在监听状态 开始监听
        if(mServer->listen(serverAddress, port))
        {
            //监听成功
            qDebug() << "Listen Ok!!";
            ui->pushButton_listen->setText("停止监听");
        }
        else
        {
            //监听失败
            QMessageBox::warning(this, "Tcp Server Listen Error", mServer->errorString());
        }
    }
}

void Widget::newConnectionSlot()
{
    QString clientInfo;

    mClient = mServer->nextPendingConnection();
    mClientList.append(mClient);
    //窥视Client 信息
    clientInfo = mClient->peerAddress().toString() + ":"+  QString::number(mClient->peerPort());
    ui->listWidget_client->addItem(clientInfo);

    connect(mClient, SIGNAL(readyRead()),this, SLOT(readyReadSlot()));          //接收消息

    connect(mClient, SIGNAL(disconnected()),this, SLOT(disconnectedSlot()));    //断开连接
}

void Widget::readyReadSlot()
{
    QByteArray recvArray;
    QTcpSocket* current = nullptr;
    if(!mClientList.isEmpty())
    {
        //接收客户端数据
        for(int index = 0;index < mClientList.count();index ++)
        {
            current = mClientList.at(index);

            if(current->isReadable())
            {
                recvArray = current->readAll();
                if(recvArray.isEmpty()) continue;
                QString str = QString(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss ddd")) +
                        ":Recv\n" + str.fromLocal8Bit(recvArray.data());    //本地GBK转Unicode 解决乱码
                ui->textBrowser_recv->append(str);
                break;
            }
        }
        //转发给其他客户端
        for(int index = 0;index < mClientList.count();index ++)
        {
            QTcpSocket* temp = mClientList.at(index);
            if(current == temp) continue;
            if(temp->isWritable())
            {
                temp->write(recvArray);
            }
        }
    }
}

void Widget::disconnectedSlot()
{
    QMessageBox::information(this, "client close Signal", "client over");
}

void Widget::on_pushButton_send_clicked()
{
    QString sendString = ui->plainTextEdit_send->toPlainText();
    QByteArray sendArr = sendString.toLocal8Bit();

    //群发给所有客户端连接
    if(!mClientList.isEmpty())
    {
        for(int index = 0;index < mClientList.count();index ++)
        {
            QTcpSocket* temp = mClientList.at(index);
            if(temp->isWritable())
            {
                temp->write(sendArr);
            }
        }
    }
    QString str = QString(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss ddd"))
            + ":Send\n" + sendString;
    ui->textBrowser_recv->append(str);          //本地GBK转Unicode 解决乱码
}
本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-15,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 编程学习基地 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Qt网络聊天室服务端
    • 网络聊天室程序
      • QTcpServer
        • QTcpServer的信号:
          • 配置
            • 获取当前设备所有ip地址
              • listen() close()
                • newConnection()
                  • SINGL
                    • UI设计
                    • TcpServer项目训练
                      • widget.h
                        • widget.cpp
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                        http://www.vxiaotou.com