客户端: #include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//实例化一个客户端对象
Client = new QTcpSocket(this);
//当收到连接成功的信号后调用自定义槽函数
connect(Client,&QTcpSocket::connected,this,&Widget::Connect_slot);
connect(Client,&QTcpSocket::readyRead,this,&Widget::Readready_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_Connect_pushButton_clicked()
{
if(ui->Connect_pushButton->text() == "连接")
{
Username = ui->Name_edit->text();
QString Ip = ui->Ip_edit->text();
quint16 port = ui->Port_edit->text().toUInt();
Client->connectToHost(Ip,port);
//程序执行至此,客户端已向服务器端发送亲求,如果连接成功,那么该套接字会自动发送一个connect的信号
}
else
{
QMessageBox Box_1 (QMessageBox::Question,
"提醒",
"你确定要断开连接?",
QMessageBox::Ok|QMessageBox::No,
this
);
Box_1.setButtonText(QMessageBox::Ok,"确定");
Box_1.setButtonText(QMessageBox::No,"取消");
int btn = Box_1.exec();
if(btn == QMessageBox::Ok)
{
Client->close();
ui->Ip_edit->clear();
ui->Name_edit->clear();
ui->Port_edit->clear();
ui->listWidget->clear();
ui->Message_edit->clear();
// ui->Ip_edit->setReadOnly(false);
ui->Ip_edit->setEnabled(true);
// ui->Name_edit->setReadOnly(false);
ui->Name_edit->setEnabled(true);
// ui->Port_edit->setReadOnly(false);
ui->Port_edit->setEnabled(true);
ui->Connect_pushButton->setText("连接");
QString Leave_Data = Username + "断开连接";
Client->write(Leave_Data.toLocal8Bit());
Client->close();
}
}
}
void Widget::Connect_slot()
{
ui->Connect_pushButton->setText("断开");
// ui->Ip_edit->setReadOnly(true);
ui->Ip_edit->setEnabled(false);
// ui->Name_edit->setReadOnly(true);
ui->Name_edit->setEnabled(false);
// ui->Port_edit->setReadOnly(true);
ui->Port_edit->setEnabled(false);
QMessageBox::information(this,"提示","连接成功");
QString msg = Username + "已上线";
Client->write(msg.toLocal8Bit());
}
void Widget::Readready_slot()
{
QByteArray msg = Client->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}
void Widget::on_Send_pushButton_clicked()
{
QString Data = ui->Message_edit->text();
Client->write(Data.toLocal8Bit());
ui->Message_edit->clear();
}
服务器端
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
server = new QTcpServer(this);
connect(server,&QTcpServer::newConnection,this,&Widget::Connection_prompt_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_Start_button_clicked()
{
if(ui->Start_button->text() == "启动")
{
//启动服务器逻辑
//获取ui界面上的端口号
quint16 port = ui->port_edit->text().toUInt();
if(server->listen(QHostAddress::Any,port))
{
QMessageBox::information(this,"提示","启动成功");
}
else
{
QMessageBox::information(this,"提示","启动失败");
return;
}
ui->Start_button->setText("关闭");
}
else
{
server->close();
ui->port_edit->clear();
ui->listWidget->clear();
ui->Start_button->setText("启动");
}
}
void Widget::Connection_prompt_slot()
{
QMessageBox::information(this,"提示","有新客户连接");
QTcpSocket *s = server->nextPendingConnection();
//将该套接字存放到客户端链表中
clientList.push_back(s);
//执行到此客户端和服务器段已建立多个连接
//当某个客户端向服务器段发送消息后,其套接字会自动发射一个readyRead信号
//将该该信号连接到自定义槽函数执行相关逻辑
connect(s,&QTcpSocket::readyRead,this,&Widget::read_information_slot);
}
void Widget::read_information_slot()
{
//遍历所有客户端,查看哪一个客户端有消息待读
for(auto val:clientList)
{
if(val->bytesAvailable())
{
//将套接字消息读出来
QByteArray msg = val->readAll();
//将该消息展现到ui界面上
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
//将消息发送给所有的客户端
for(auto key:clientList)
{
key->write(msg);
}
}
}
}