qt事件和连接TCP协议

QT网络聊天室服务器实现

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    ,server(new QTcpServer(this))//给服务器指针实例化一个空间
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_start_btn_clicked()
{
    //    获取UI界面上的端口号
    quint16 port =  ui->port_edit->text().toUInt();//将字符串转成整型
    //    函数原型 参数1:监听的主机 参数2监听的端口号
    //    bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
    //    让服务器设置监听
    if( server->listen(QHostAddress::Any, port) ){
        QMessageBox::information(this,"","启动服务器成功!");
    }else{
        QMessageBox::information(this,"","启动服务器失败!");
        return;
    }
    //    服务器已经设置好监听,如果有客户端发来连接,服务器端会自动发射newconnection信号
    connect(server, &QTcpServer::newConnection, this, &Widget::newConnection_slot);//newConnection连接到newConnection_slot槽函数
}

//newConnection槽函数声明
void Widget::newConnection_slot()
{
    // 函数原型:virtual QTcpSocket *nextPendingConnection();
    // 使用nextpadding获取最新连接客户端套接字
    QTcpSocket *s = server->nextPendingConnection();
    //    将客户端放入容器中
    socketList.push_back(s);
    //    如果有客户端向服务器发来数据,客户端就会自动发送一个readyread信号,就可以将该信号连接到自定义槽函数中读取数据
    connect(s, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);//readyRead信号连接到readyRead_slot槽函数
}

void Widget::readyRead_slot()
{
    //遍历客户端容器,移除无效客户端
    for( int i=0; i<socketList.count(); i++){ //count是容器的元素个数
        //判断连接状态 SocketState.UnconnectedState 
//        enum SocketState {
//            UnconnectedState,0
//            HostLookupState,1
//            ConnectingState,2
//            ConnectedState,3
//            BoundState,4
//            ListeningState,5
//            ClosingState
//        };
        if( socketList.at(i)->state() == 0 ){
            // 删除该元素
            socketList.removeAt(i);
        }
    }
    // 寻找哪个客户端有数据待读
    for(int i=0;i<socketList.count(); i++){

        if( socketList.at(i)->bytesAvailable()!= 0 ){ //当前socketList的字节数不为0,代表有数据
            // 读取数据
            QByteArray msg = socketList.at(i)->readAll();
            // 将读取到的数据,放入到UI界面上
            ui->listWidget->addItem(QString::fromLocal8Bit(msg));
            //  将数据广播给所有客户端
            for(int j=0;j<socketList.count(); j++){
                socketList.at(j)->write(msg);
            }
        }
    }
}

qt事件和连接TCP协议

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值