Qt实现TCP网络聊天室
代码示例
服务器
头文件
#ifndef CHATSERVER_H
#define CHATSERVER_H
#include<QDebug>
#include <QWidget>
#include<QTcpServer>
#include<QTcpSocket>
#include<QList>
QT_BEGIN_NAMESPACE
namespace Ui { class ChatServer; }
QT_END_NAMESPACE
class ChatServer : public QWidget
{
Q_OBJECT
public:
explicit ChatServer(QWidget *parent = nullptr);
~ChatServer();
private slots:
void on_startBtn_clicked();
void on_newConnect_slot();
void on_readyRead_slot();
void send(QString mes);
private:
Ui::ChatServer *ui;
QTcpServer *server;
QList<QTcpSocket *> socketList;
};
#endif
源文件
#include "chatserver.h"
#include "ui_chatserver.h"
ChatServer::ChatServer(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ChatServer)
{
ui->setupUi(this);
this->setWindowIcon(QIcon(":/images/shuiyin.png"));
this->setFixedSize(700,500);
server = new QTcpServer(this);
}
ChatServer::~ChatServer()
{
delete ui;
}
void ChatServer::on_startBtn_clicked()
{
quint16 port = quint16(ui->portEdit->text().toInt());
if(server->listen(QHostAddress::Any,port))
{
qDebug()<<"服务器创建成功";
}else{
qDebug()<<"服务器创建失败";
}
ui->startBtn->setEnabled(false);
ui->portEdit->setEnabled(false);
connect(server,&QTcpServer::newConnection,this,&ChatServer::on_newConnect_slot);
}
void ChatServer::on_newConnect_slot()
{
QTcpSocket* socket = server->nextPendingConnection();
socketList.append(socket);
connect(socket,&QTcpSocket::readyRead,this,&ChatServer::on_readyRead_slot);
}
void ChatServer::on_readyRead_slot()
{
for(int i=0;i<socketList.count();i++)
{
if(socketList.at(i)->state() == false)
{
socketList.removeAt(i);
}
}
for(quint16 i=0;i<socketList.count();i++)
{
if(socketList.at(i)->bytesAvailable())
{
QString mes = QString::fromLocal8Bit(socketList.at(i)->readAll());
ui->listWidget->addItem(mes);
send(mes);
}
}
}
void ChatServer::send(QString mes)
{
for(int i=0;i<socketList.count();i++)
{
socketList.at(i)->write(mes.toLocal8Bit());
}
}
客户端
头文件
#ifndef CHATCLIENT_H
#define CHATCLIENT_H
#include <QWidget>
#include<QTcpSocket>
#include<QHostAddress>
QT_BEGIN_NAMESPACE
namespace Ui { class ChatClient; }
QT_END_NAMESPACE
class ChatClient : public QWidget
{
Q_OBJECT
public:
ChatClient(QWidget *parent = nullptr);
~ChatClient();
private slots:
void on_connectBtn_clicked();
void on_connected_slot();
void on_disconnected_slot();
void on_readyRead_slot();
void on_sendBtn_clicked();
private:
Ui::ChatClient *ui;
QTcpSocket *socket;
QString userName;
bool isOk;
};
#endif
源文件
#include "chatclient.h"
#include "ui_chatclient.h"
ChatClient::ChatClient(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ChatClient)
{
ui->setupUi(this);
this->setFixedSize(700,500);
ui->msgEdit->setPlaceholderText("请在这里输入消息");
ui->IpEdit->setPlaceholderText("请输入IP地址");
ui->userNameEdit->setPlaceholderText("请输入用户名");
ui->portEdit->setPlaceholderText("请输入端口号");
socket = new QTcpSocket(this);
isOk = false;
connect(socket,&QTcpSocket::connected,this,&ChatClient::on_connected_slot);
connect(socket,&QTcpSocket::disconnected,this,&ChatClient::on_disconnected_slot);
connect(socket,&QTcpSocket::readyRead,this,&ChatClient::on_readyRead_slot);
}
ChatClient::~ChatClient()
{
delete ui;
}
void ChatClient::on_connectBtn_clicked()
{
QString ip = ui->IpEdit->text();
quint16 port = quint16(ui->portEdit->text().toInt());
if(isOk==false)
{
socket->connectToHost(QHostAddress(ip),port);
ui->connectBtn->setText("断开连接");
isOk = true;
}else
{
userName = ui->userNameEdit->text();
QString msg = userName + "退出群聊";
socket->write(msg.toLocal8Bit());
socket->disconnectFromHost();
ui->connectBtn->setText("连接服务器");
isOk = false;
}
}
void ChatClient::on_connected_slot()
{
ui->IpEdit->setEnabled(false);
ui->userNameEdit->setEnabled(false);
ui->portEdit->setEnabled(false);
userName = ui->userNameEdit->text();
QString msg = userName + "加入群聊";
socket->write(msg.toLocal8Bit());
}
void ChatClient::on_disconnected_slot()
{
ui->IpEdit->setEnabled(true);
ui->userNameEdit->setEnabled(true);
ui->portEdit->setEnabled(true);
}
void ChatClient::on_readyRead_slot()
{
QString msg = QString::fromLocal8Bit(socket->readAll());
ui->listWidget->addItem(msg);
}
void ChatClient::on_sendBtn_clicked()
{
QString msg = userName+":"+ui->msgEdit->text();
socket->write(msg.toLocal8Bit());
ui->msgEdit->clear();
}
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b1157c7a1bdbea48059ccc9703b5643a.png)