#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, socket(new QTcpSocket(this))
{
ui->setupUi(this);
//初始化页面
ui->mag_edit->setEnabled(false);
ui->send_btn->setEnabled(false);
ui->dis_btn->setEnabled(false);
//
connect(socket,&QTcpSocket::connected,this,&Widget::connected_slot);
//如果服务器向客户端发送数据,那么客户端就会自动发射一个readyread()信号
connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_connect_btn_clicked()
{
QString ip = ui->ip_edit->text();
quint16 port = ui->port_edit->text().toUInt();
socket->connectToHost(ip,port);
}
void Widget::connected_slot()
{
QString userName = ui->user_edit->text();
//告诉服务器我来了
QString msg = userName + ":进入聊天室";
//将信息发送给服务器
socket->write(msg.toLocal8Bit());
ui->mag_edit->setEnabled(true);
ui->send_btn->setEnabled(true);
ui->dis_btn->setEnabled(true);
ui->user_edit->setEnabled(false);
ui->ip_edit->setEnabled(false);
ui->port_edit->setEnabled(false);
ui->connect_btn->setEnabled(false);
}
void Widget::on_send_btn_clicked()
{
//获取ui界面的文本
QString msg =ui->mag_edit->text();
msg = userName + ":" + msg;
//将信息发生给服务器
socket->write(msg.toLocal8Bit());
//清空
ui->mag_edit->clear();
}
void Widget:: readyRead_slot()
{
QByteArray msg = socket->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}
void Widget::disconnected_slot()
{
ui->mag_edit->setEnabled(false);
ui->send_btn->setEnabled(false);
ui->dis_btn->setEnabled(false);
ui->user_edit->setEnabled(true);
ui->ip_edit->setEnabled(true);
ui->port_edit->setEnabled(true);
ui->connect_btn->setEnabled(true);
}
void Widget::on_dis_btn_clicked()
{
//告诉服务器我要走了
QString msg = userName + ":离开聊天室";
socket->write(msg.toLocal8Bit());
socket->disconnectFromHost();
}
服务器
#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()
{
quint16 port = ui->port_edit->text().toUInt();
if(server->listen(QHostAddress::Any,port))
{
QMessageBox::information(this,"","启动服务器成功!");
}
else
{
QMessageBox::information(this,"","启动服务器失败!");
return;
}
connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}
void Widget::newConnection_slot()
{
qDebug()<<"有新的客户端连接";
QTcpSocket *s = server->nextPendingConnection();
socketList.push_back(s);
connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}
void Widget::readyRead_slot()
{
for(int i=0;i<socketList.count();i++)
{
if(socketList.at(i)->state()==0)
{
//移除无效客户端
socketList.removeAt(i);
}
}
//遍历客户端容器判断哪个客户端有数据待读
for(int i=0;i<socketList.count();i++)
{
//判断哪个客户端有数据待读
if(socketList.at(i)->bytesAvailable()!=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);
}
}
}
}