TCP服务器
头文件
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
#include <QMessageBox>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_staetbtn_clicked();
void newConnection_slot();
void readyRead_slot();
private:
Ui::Widget *ui;
QTcpServer *ser;
QList<QTcpSocket *> cliList;
};
#endif // WIDGET_H
源文件
cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ser=new QTcpServer(this);
}
Widget::~Widget()
{
delete ui;
}
//启动按钮的槽函数
void Widget::on_staetbtn_clicked()
{
if(ui->staetbtn->text()=="启动")
{
//获取ui界面上的端口号
quint16 port=ui->portedit->text().toInt();
//启动服务器
if(ser->listen(QHostAddress::Any,port)==true)
{
QMessageBox::information(this,"成功","服务器启动成功");
}else
{
QMessageBox::information(this,"失败","服务器启动失败");
}
connect(ser,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
//按钮文本更改
ui->staetbtn->setText("关闭");
}else
{
//关闭服务器
ser->close();
//更改按钮文本内容
ui->staetbtn->setText("启动");
}
}
//处理newConnection信号的槽函数
void Widget::newConnection_slot()
{
//获取最新连接的客户端套接字,并将
QTcpSocket* socket=ser->nextPendingConnection();
cliList.append(socket);
connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}
void Widget::readyRead_slot()
{
for(int i=0;i<cliList.length();i++)
{
if(cliList[i]->state()==QTcpSocket::UnconnectedState)
{
cliList.removeAt(i);
}
}
for(int i=0;i<cliList.length();i++)
{
if(cliList[i]->bytesAvailable()!=0)
{
QByteArray msg= cliList[i]->readAll();
//将该消息展示到ui界面
ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
//将该消息转发给所有客户端
for(int j=0;j<cliList.length();j++)
{
if(i!=j) //不转发自己
{
cliList[j]->write(msg);
}
}
}
}
}
客户端
头文件
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_connectbtn_clicked();
void connected_slot();
void on_sendbtn_clicked();
void readyRead_slot();
void disconnectes_slot();
private:
Ui::Widget *ui;
QTcpSocket *cli;
QString user;
};
#endif // WIDGET_H
源文件
cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
cli=new QTcpSocket(this);
//将客户端的connect信号连接到自定义的槽函数
connect(cli,&QTcpSocket::connected,this,&Widget::connected_slot);
//客户端收到服务器消息时会自动发射一个readyRead信号
connect(cli,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
//断开信号连接对应的槽函数
connect(cli,&QTcpSocket::disconnected,this,&Widget::disconnectes_slot);
}
Widget::~Widget()
{
delete ui;
}
//连接服务器按钮对应得到槽函数
void Widget::on_connectbtn_clicked()
{
if(ui->connectbtn->text()=="连接服务器")
{
user=ui->userEdit->text();
QString ip=ui->IPEdit->text();
quint16 port=ui->portEdit->text().toUInt();
cli->connectToHost(ip,port);
ui->connectbtn->setText("断开服务器");
}else
{
QString msg=user+"已下线";
cli->write(msg.toLocal8Bit());
cli->disconnectFromHost();
ui->connectbtn->setText("连接服务器");
}
}
//connected信号的槽函数的实现
void Widget::connected_slot()
{
QMessageBox::information(this,"连接","连接成功");
}
//信息发送按钮对应的槽函数
void Widget::on_sendbtn_clicked()
{
//将ui界面上的文本信息获取下来
QString msg=user+":"+ ui->lineEdit->text();
//将消息发送给服务器
cli->write(msg.toLocal8Bit());
//清空6行编辑器的内容
ui->lineEdit->clear();
//将消息放入ui界面中
QListWidgetItem *item=new QListWidgetItem(msg);
item->setTextAlignment(Qt::AlignRight);
ui->msgwidget->addItem(item);
}
//处理readyRead信号对应的槽函数
void Widget::readyRead_slot()
{
//将客户端套接字中的消息读取出来
QByteArray msg=cli->readAll();
//将消息展示到ui界面
ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
}
//disconnected信号对应的槽函数
void Widget::disconnectes_slot()
{
QMessageBox::information(this,"断开","断开连接");
}
数据库
头文件
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QMessageBox>
#include <QSqlQuery>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_addbtn_clicked();
void on_searchbtn_clicked();
void on_deletebtn_clicked();
void on_updataButton_clicked();
private:
Ui::Widget *ui;
QSqlDatabase db;
};
#endif // WIDGET_H
源文件
cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
if(!db.contains("mydb.db"))
{
db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mydb.db");
}
if(!db.open())
{
QMessageBox::information(this,"失败","打开失败");
return;
}
QString sql="create table if not exists Stu(numb int,name char,sex char,score double);";
QSqlQuery query;
if(!query.exec(sql))
{
QMessageBox::information(this,"提示","创建失败");
return;
}
}
Widget::~Widget()
{
delete ui;
}
//添加槽函数
void Widget::on_addbtn_clicked()
{
int ui_numb=ui->numEdit->text().toUInt();
QString ui_name=ui->nameedit->text();
QString ui_sex=ui->sexEdit->text();
double ui_score=ui->scoreedit->text().toUInt();
if(ui_sex==NULL||ui_numb==0||ui_name==NULL||ui_score==0)
{
QMessageBox::information(this,"提示","请将信息填写完整");
return;
}
QString sql=QString("insert into Stu values(%1,'%2','%3',%4)").arg(ui_numb).arg(ui_name).arg(ui_sex).arg(ui_score);
QSqlQuery query;
if(!query.exec(sql))
{
QMessageBox::information(this,"提示","添加失败");
return;
}else
{
QMessageBox::information(this,"提示","添加成功");
//清空所有行编辑框
ui->numEdit->clear();
ui->nameedit->clear();
ui->sexEdit->clear();
ui->scoreedit->clear();
return;
}
}
//查找槽函数
void Widget::on_searchbtn_clicked()
{
ui->tableWidget->clearContents();
QString sql;
if(ui->nameedit->text()==NULL)
{
sql="select *from Stu";
}else
{
sql=QString("select * from Stu where name='%1'").arg(ui->nameedit->text());
}
QSqlQuery query;
if(!query.exec(sql))
{
QMessageBox::information(this,"提示","查找失败");
return;
}
//清空所有行编辑框
ui->numEdit->clear();
ui->nameedit->clear();
ui->sexEdit->clear();
ui->scoreedit->clear();
int i=0;
while(query.next())
{
for(int j=0;j<query.record().count();j++)
{
QTableWidgetItem *item=new QTableWidgetItem(query.record().value(j).toString());
item->setTextAlignment(Qt::AlignCenter);
ui->tableWidget->setItem(i,j,item);
}
i++;
}
}
//删除槽函数
void Widget::on_deletebtn_clicked()
{
QString sql;
if(ui->nameedit->text()==NULL)
{
sql="delete from Stu";
}else
{
sql=QString("delete from Stu where name='%1'").arg(ui->nameedit->text());
}
QSqlQuery query;
if(!query.exec(sql))
{
QMessageBox::information(this,"提示","删除失败");
return;
}else
{
QMessageBox::information(this,"提示","删除成功");
//清空所有行编辑框
ui->numEdit->clear();
ui->nameedit->clear();
ui->sexEdit->clear();
ui->scoreedit->clear();
}
}
//修改槽函数
void Widget::on_updataButton_clicked()
{
QString sql;
sql=QString("update Stu set score=%1 where name='%2'").arg(ui->scoreedit->text()).arg(ui->nameedit->text());
QSqlQuery query;
if(!query.exec(sql))
{
QMessageBox::information(this,"提示","修改失败");
return;
}else
{
QMessageBox::information(this,"提示","修改成功");
//清空所有行编辑框
ui->numEdit->clear();
ui->nameedit->clear();
ui->sexEdit->clear();
ui->scoreedit->clear();
}
}