服务器端
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//实例化一个服务器对象
server =new QTcpServer(this);
}
Widget::~Widget()
{
delete ui;
}
//打开服务器按钮对应的槽函数
void Widget::on_tcpbtn_clicked()
{
//获取ui界面上的端口号
quint16 port = ui->pe->text().toUInt();
//将服务器设置成监听状态
//函数原型:bool listen()
//将服务器设置为监听状态
//参数1:要监听的主机地址,如果是any,则表明监听任意主机地址
//参数2:接入的端口号,如果为0,表明,端口号由系统自动分配
if(server->listen(QHostAddress::Any,port))
{
QMessageBox::information(this,"成功","打开服务器成功");
//此时表明服务器已经打开,并处于监听状态
//如果有客户端发来连接请求,那么该服务器就会自动触发一个newconnection信号
}
connect(server,&QTcpServer::newConnection,this,&Widget::newconnection_slot);
}
void Widget::newconnection_slot()
{
//获取最新连接的客户端套接字
QTcpSocket *s =server->nextPendingConnection();
//将该套接字放入到套接字文件中
clientVector.push_back(s);
//此时一个服务器和多个客户端已经建立连接,如果有客户端向服务器发来数据
//那么该客户端就会自动发射一个readyRead的信号
//我们可以将该信号连接到自定的槽函数中,处理相关逻辑
connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}
//处理readyRead信号对应的槽函数
void Widget::readyRead_slot()
{
//删除无效的客户端
for(int i=0;i<clientVector.size();i++)
{
//clientVector[i]:表示的是任意一个客户端
if(clientVector[i]->state()==0)
{
//删除
clientVector.removeAt(i); //将当前客户端移除该容器
}
}
//判断是哪一个客户端发来的数据
for(int i=0;i<clientVector.size();i++)
{
//判断当前的套接字中是否有数据待读
//函数原型 bytesAvailable
//功能:返回当前客户端的能被读取的字节个数
//参数:无
//返回值:可被读取的字节个数
if(clientVector[i]->bytesAvailable()!=0)
{
QByteArray msg= clientVector[i]->readAll();
//将读取的数据 放到ui界面上
ui->lw->addItem(QString::fromLocal8Bit(msg));
//将获取的该套接字中的数据广播给所有客户端
for(int j=0;j<clientVector.size();j++)
{
clientVector[j]->write(msg);
}
}
}
客户端:
widget.cpp
#include "second.h"
#include "ui_second.h"
second::second(QWidget *parent) :
QWidget(parent),
ui(new Ui::second)
{
ui->setupUi(this);
this->setWindowTitle("聊天室界面");//设置标题
this->setWindowIcon(QIcon("C:\\Users\\86150\\Desktop\\icon\\1\\denglu_1.png"));//设置图标
//创建logo
socket=new QTcpSocket(this);
//连接服务器
connect(socket,&QTcpSocket::connected,this,&second::connected_slot);
connect(socket,&QTcpSocket::readyRead,this,&second::readyRead_slot);
}
void second::jump_slot()
{
this->show();
}
second::~second()
{
delete ui;
}
void second::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawPixmap(rect(),QPixmap("C:\\Users\\86150\\Desktop\\icon\\background"));
}
void second::on_pushButton_clicked()
{
bool ok;
QFont f=QFontDialog::getFont(
&ok,
QFont("宋体",10,2,false),
this);
if(ok)
{
ui->textEdit->setCurrentFont(f);
}else {
QMessageBox::information(this,"错误","没有识别到你要的字体");
}
}
void second::on_pushButton_2_clicked()
{
QColor c=QColorDialog::getColor(
Qt::black,
this,
"字体颜色"
);
if(c.isValid())
{
ui->textEdit->setTextColor(c);
}else {
QMessageBox::information(this,"错误","设置颜色失败");
}
}
void second::on_pushButton_3_clicked()
{
QString path=QFileDialog::getOpenFileName(
this,
"打开文件",
"./",
"所有文件(*.*);;文本文件(*.txt);;");
QFile file(path);
if(!file.exists())
{
QMessageBox::information(this,"错误信息","不存在");
return;
}
if(!file.open(QIODevice::ReadWrite))
{
QMessageBox::information(this,"错误信息","读写错误");
return;
}
QByteArray msg=file.readAll();
file.close();
ui->textEdit->setText(QString::fromLocal8Bit(msg));
}
void second::on_pushButton_4_clicked()
{
QString path=QFileDialog::getSaveFileName(
this,
"保存文件",
"./",
"所有文件(*.*);;文本文件(*.txt);;");
QFile file(path);
if(!file.open(QIODevice::ReadWrite))
{
QMessageBox::information(this,"错误信息","不存在");
return;
}
QString msg=ui->textEdit->toPlainText();
file.write(msg.toLocal8Bit());
file.close();
}
void second::on_pushButton_5_clicked()//发送按钮对应的槽函数
{
QString msg =userName+" :"+ui->textEdit->toPlainText();
socket->write(msg.toLocal8Bit());
ui->textEdit->clear();
}
void second::on_connectbtn_clicked()
{
QString ip =ui->ipedit->text();
quint16 port=ui->portedit->text().toUInt();
userName=ui->nameedit->text();
if(ui->connectbtn->text()=="连接服务器")
{
socket->connectToHost(ip,port);
ui->connectbtn->setText("断开服务器");
}else{
QString msg = userName + ":离开聊天室";
socket->write(msg.toLocal8Bit());
//完成断开服务器的逻辑
socket->disconnectFromHost();
ui->ipedit->setEnabled(true);
ui->portedit->setEnabled(true);
ui->nameedit->setEnabled(true);
ui->connectbtn->setText("连接服务器");
}
}
void second::connected_slot()
{
QString msg=userName+":进入聊天室";
socket->write(msg.toLocal8Bit());
QMessageBox::information(this,"","连接成功");
ui->ipedit->setEnabled(false);
ui->portedit->setEnabled(false);
ui->nameedit->setEnabled(false);
}
void second::readyRead_slot()
{
if(socket->bytesAvailable()!=0)
{
QString msg=QString::fromLocal8Bit(socket->readAll());
ui->listWidget->addItem(msg);
}
}