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协议
