QT--TCP网络编程(客户端/服务器)
1.主要流程
1.客户端
-
创建QTcpSocket对象
-
连接到服务器 --connectToHost()
-
-
读取数据 ---readAll()
-
断开连接
2.服务器
-
创建QTcpServer 对象
-
绑定本地信息---listen(QHostAddress::Any,端口)
-
等待客户端连接---newConnection()
-
读取数据
-
发送数据
-
断开连接
2.头文件
-
客户端:#include < QTcpSocket >
-
服务器:#include < QTcpServer >
-
必须要在pro文件加上-----QT += core gui network
3.代码
1.客户端
-
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtNetwork/QTcpSocket> #include <QKeyEvent> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT private: QTcpSocket *client; QString *message; void keyPressEvent(QKeyEvent *event); public: explicit Widget(QWidget *parent = nullptr); ~Widget(); private: Ui::Widget *ui; }; #endif // WIDGET_H
-
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QTextEdit> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); client = new QTcpSocket(this); ui->Button_send->setEnabled(false); /* 连接服务器 */ connect(ui->Button_connection,&QPushButton::released,this, [=]() { client->connectToHost(ui->lineEdit_ip->text(),ui->lineEdit_d->text().toInt()); }); /* 连接成功 */ connect(client,&QTcpSocket::connected,this, [&]() { ui->Button_send->setEnabled(true); ui->label_prompt->setText("已连接"); ui->textEdit_receive->append("连接成功……"); }); /* 发送到服务器 */ connect(ui->Button_send,&QPushButton::released,this, [&]() { QString data = ui->textEdit_edit->toPlainText(); if(data.length() > 0) //如果输入框不为空才可以发送 { client->write(data.toUtf8()); QString data1 = "已发送:"+data; ui->textEdit_receive->append(data1); } else { ui->textEdit_receive->append("输入信息"); } }); /* 接收信息 */ connect(client,&QTcpSocket::readyRead,this, [&]() { QString msg = "收到:" + client->readAll(); ui->textEdit_receive->append(msg); }); /* 断开连接 */ connect(ui->Button_disconnect,&QPushButton::released,this, [=]() { client->disconnectFromHost(); ui->Button_send->setEnabled(false); }); /* 断开连接 */ connect(client,&QTcpSocket::disconnected,this, [&]() { ui->label_prompt->setText("未连接……"); ui->textEdit_receive->append("断开连接……"); }); } /* 键盘事件:捕获按下回车键发送---这一部分可选择不要 */ void Widget::keyPressEvent(QKeyEvent *event) { int key = event->key(); if(key == Qt::Key_Enter) { QString data = ui->textEdit_edit->toPlainText(); if(data.length() > 0) { client->write(data.toUtf8()); QString data1 = "已发送:"+data; ui->textEdit_receive->append(data1); } else { ui->textEdit_receive->append("输入信息"); } } } Widget::~Widget() { delete ui; }
2.服务器
-
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtNetwork/QTcpServer> #include <QtNetwork/QTcpSocket> #include <QKeyEvent> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: QTcpServer *server; QTcpSocket *socket; void new_client(); void send_data(); void read_data(); void disconnect(); void keyPressEvent(QKeyEvent *event); //重写键盘事件虚函数,如果不要键盘发 //送可选择删掉 public: explicit Widget(QWidget *parent = nullptr); ~Widget(); private: Ui::Widget *ui; }; #endif // WIDGET_H
-
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); ui->Button_send->setEnabled(false); server = new QTcpServer(this); /* 绑定绑定信息 */ server->listen(QHostAddress::Any,ui->lineEdit_d->text().toInt()); /* 等待客户端连接 */ connect(server,&QTcpServer::newConnection,this,&Widget::new_client); /* 发送信息 */ connect(ui->Button_send,&QPushButton::released,this,&Widget::send_data); } /* 等待连接 */ void Widget::new_client() { qDebug() << "新客户连接"; ui->label_prompt->setText("已连接"); ui->textEdit_log->append("连接成功"); ui->Button_send->setEnabled(true); socket = server->nextPendingConnection(); connect(socket,&QTcpSocket::readyRead,this,&Widget::read_data); connect(ui->Button_disconnect,&QPushButton::released,socket,&QTcpSocket::disconnectFromHost); connect(socket,&QTcpSocket::disconnected,this, [&]() { ui->textEdit_log->append("断开连接"); }); } /* 发送信息 */ void Widget::send_data() { QString data = ui->textEdit_edit->toPlainText(); if(data.length() > 0) //如果输入栏不为空才可以发送 { socket->write(data.toUtf8()); QString data1 = "已发送:" + data; ui->textEdit_log->append(data1); } else { ui->textEdit_log->append("输入信息"); } } /* 接收信息 */ void Widget::read_data() { // QTcpSocket *obj = (QTcpSocket*)sender(); QString msg = "收到:" + socket->readAll(); ui->textEdit_log->append(msg); } /* 断开连接 */ void Widget::disconnect() { ui->textEdit_log->append("断开连接"); ui->Button_send->setEnabled(false); } /* 回车键发送 ----- 可选择不要 */ void Widget::keyPressEvent(QKeyEvent *event) { int key = event->key(); if(key == Qt::Key_Enter) { QString data = ui->textEdit_edit->toPlainText(); if(data.length() > 0) //如果输入栏不为空才可以发送 { qDebug() << data.length(); socket->write(data.toUtf8()); QString data1 = "已发送:" + data; ui->textEdit_log->append(data1); } else { ui->textEdit_log->append("输入信息"); } } } Widget::~Widget() { delete ui; }