目录
服务器
server.h
#ifndef CHATSERVER_H
#define CHATSERVER_H
#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:
ChatServer(QWidget *parent = nullptr);
~ChatServer();
//定义一个服务器指针
QTcpServer *server;
//定义客户端指针,盛放连接的客户端
QList<QTcpSocket *> socketlist;
private slots:
void on_starbutton_clicked();
void on_newConnection_slot();//newconnect 对应的槽函数声明
void on_readyRead_slot(); //readyread 对应的槽函数声明
void send(QString mes);
private:
Ui::ChatServer *ui;
};
#endif // CHATSERVER_H
server.cpp
#include "chatserver.h"
#include "ui_chatserver.h"
ChatServer::ChatServer(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ChatServer)
{
ui->setupUi(this);
//实例化服务器
server =new QTcpServer(this);
}
ChatServer::~ChatServer()
{
delete ui;
}
//启动按钮对应的槽函数
void ChatServer::on_starbutton_clicked()
{
quint16 port = quint16(ui->portedit->text().toInt());
//将服务器设置为监听状态
/*
*/
server->listen(QHostAddress::Any,port);
//将相应的控件设置为不可用状态
ui->portedit->setEnabled(false);
ui->starbutton->setEnabled(false);
//当有新的客户端连接到该服务器后,会发出一个newconnect的信号
connect(server,&QTcpServer::newConnection,this,&ChatServer::on_newConnection_slot);
}
//newconnection对应的槽函数
void ChatServer::on_newConnection_slot()
{
//获取最新连接的客户端的套接字
QTcpSocket * socket=server->nextPendingConnection();
//将套接字放入套接字链表中
socketlist.append(socket);
//当服务器接收到客户端传过来的消息时,就会触发一个readyread信号
connect(socket,&QTcpSocket::readyRead,this,&ChatServer::on_readyRead_slot);
}
//readyRead对应槽函数的定义
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 = socketlist.at(i)->readAll();
//将消息展示到listview上
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());
}
}
客户端
clint.h
#ifndef CHATCLINT_H
#define CHATCLINT_H
#include <QWidget>
#include<QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class chatclint; }
QT_END_NAMESPACE
class chatclint : public QWidget
{
Q_OBJECT
public:
chatclint(QWidget *parent = nullptr);
~chatclint();
private slots:
void on_conntctButton_clicked();
void on_connected_slot(); //connected信号对应的槽函数声明
void on_disconnected_slot();//disconnect~
void on_readyrad_slot();//readyread 信号对应的槽函数声明
void on_sendButton_clicked();
private:
Ui::chatclint *ui;
//定义客户端套接字指针
QTcpSocket *socket;
//定义用户名
QString userName;
//定义bool类型数据 表示连接状态
bool isOk;
};
#endif // CHATCLINT_H
clint.cpp
#include "chatclint.h"
#include "ui_chatclint.h"
#include <QHostAddress>
chatclint::chatclint(QWidget *parent)
: QWidget(parent)
, ui(new Ui::chatclint)
{
ui->setupUi(this);
//实例化socket
socket = new QTcpSocket(this);
//给连接状态初始值
isOk = false;
//将connected信号连接到自定义的槽函数总处理相关逻辑
connect(socket,&QTcpSocket::connected,this,&chatclint::on_connected_slot);
//将disconnected信号连接到自定义的槽函数总处理相关逻辑
connect(socket,&QTcpSocket::disconnected,this,&chatclint::on_disconnected_slot);
//当客户端接收到服务器发的消息后 会出发一个readyread信号,在对应的信号的槽函数中处理
connect(socket,&QTcpSocket::readyRead,this,&chatclint::on_readyrad_slot);
}
chatclint::~chatclint()
{
delete ui;
}
//连接服务器对应的槽函数
void chatclint::on_conntctButton_clicked()
{
QString ip=ui->ipEdit->text();
quint16 port=quint16(ui->portEdit->text().toInt());
//判断连接状态
if(isOk == false)
{
//连接服务器
socket->connectToHost(QHostAddress(ip),port);
//一旦链接成功,就会触发一个connect的信号,在该信号对应的槽函数中处理
//将按钮文本改为断开连接
ui->conntctButton->setText("断开连接");
isOk=true;
}else{
userName=ui->userEdit->text();
QString mes =userName+"离开群聊";
socket->write(mes.toLocal8Bit()); //告诉服务器我来了
//断开服务器
socket->disconnectFromHost();
//一旦断开成功,就会触发一个disconnect的信号,在该信号对应的槽函数中处理
//将按钮文本改为连接连接
ui->conntctButton->setText("启动连接");
isOk=false;
}
}
//connect信号对应的槽函数定义
void chatclint::on_connected_slot()
{
userName=ui->userEdit->text();
QString mes =userName + "加入群聊";
socket->write(mes.toLocal8Bit()); //告诉服务器我来了
ui->userEdit->setEnabled(false);
ui->portEdit->setEnabled(false);
ui->ipEdit->setEnabled(false);
}
//disconnected信号对应的槽函数定义
void chatclint::on_disconnected_slot()
{
ui->userEdit->setEnabled(true);
ui->portEdit->setEnabled(true);
ui->ipEdit->setEnabled(true);
}
//readyread信号对应的槽函数的定义
void chatclint::on_readyrad_slot()
{
//获取套接字中的数据
QString mes=QString::fromLocal8Bit(socket->readAll());
//将数据显示在list widget中
ui->listWidget->addItem(mes);
}
//发送按钮对应的槽函数
void chatclint::on_sendButton_clicked()
{
userName=ui->userEdit->text();
QString mes=userName +":"+ui->mesEdit->text();
//将数据发送给服务器
socket->write(mes.toLocal8Bit());
//将发送数据的框中数据清空
ui->mesEdit->clear();
}
运行截图