qt double去掉多余的0_Qt网络编程之简易聊天室,界面很nice

4991a3f5fb7caa33512684999881f84d.png

网络聊天室程序

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

54c6d144e1ceea4b79ae3b838d9824c7.png

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

效果展示:

aa272bb7c97a4a9c5023f4c8d2be0cac.gif

来源:

https://mp.weixin.qq.com/s?__biz=MzU5NjI2NzM5OA==&mid=2247485671&idx=1&sn=6e2a45dfb676b6ae1333ea552b979d07&chksm=fe64100ec9139918ad382cb12742ebc71074ff216bbbd4f5b9789b3ed8de5f95b7da1a3500fb&token=1091309436&lang=zh_CN#rd​mp.weixin.qq.com

github地址:https://github.com/ADeRoy/Qt_Demo

基于Qt的网络编程服务端

QTcpServer

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

QTcpServer的信号:

newConnection()     //有新连接连接时触发该信号

配置

pro文件添加

QT += network

枚举设备所有ip地址

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()来把错误打印出来

bool isListening()  const

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

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(),我们可以用这个返回的套接字和客户端进行连接

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

SINGL

readyRead()

客户端有数据发送至服务端时触发该信号
connect(mClient, SIGNAL(readyRead()),this, SLOT(readyReadSlot()));          //接收消息

isReadable

是否可读

readAll

读取客户端发送过来的全部信息
if(mClient->isReadable())
{
    QByteArray recvArray = mClient->readAll();
}

disconnected()

当客户端断开连接时触发该信号
connect(mClient, SIGNAL(disconnected()),this, SLOT(disconnectedSlot()));    //断开连接

TcpServer项目训练

widget.h

#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

#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")) +
                        ":Recvn" + 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);
            }
        }
    }
//    //一对一接收,只接受最近的一次消息
//    if(mClient!=nullptr)
//    {
//        if(mClient->isReadable())
//        {
//            QByteArray recvArray = mClient->readAll();
//            QString str = QString(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss ddd")) +
//                    ":Recvn" + str.fromLocal8Bit(recvArray.data());    //本地GBK转Unicode 解决乱码
//            ui->textBrowser_recv->append(str);
//        }
//    }
}

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"))
            + ":Sendn" + sendString;
    ui->textBrowser_recv->append(str);          //本地GBK转Unicode 解决乱码

//    //一对一发送,只会发送给最近的一个客户端连接
//    if(mClient!=nullptr)
//    {
//        if(mClient->isWritable())
//        {
//            QString sendString = ui->plainTextEdit_send->toPlainText();
//            QByteArray sendArr = sendString.toLocal8Bit();
//            mClient->write(sendArr);

//            QString str = QString(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss ddd"))
//                    + ":Sendn" + sendString;
//            ui->textBrowser_recv->append(str);          //本地GBK转Unicode 解决乱码
//        }
//    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值