IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> QT自制TCP服务器 -> 正文阅读

[网络协议]QT自制TCP服务器

TCP服务器

TCP服务器实现对TCP客户端的监听,实现对数据的发送与接收。
在这里插入图片描述
该TCP服务器入门级别,后面可以继续丰富。

界面

上图就是实现的界面,后面可以自己去丰富一下,上面的按钮,接收数据框,发送数据框,端口号输入框,文字显示等使用的控件为:

  1. 文字显示(除去按钮,按钮可以直接写)
    在这里插入图片描述

  2. 端口号输入框
    和后面发送框一样的控件
    在这里插入图片描述

  3. 按钮
    在这里插入图片描述

  4. 发送框
    在这里插入图片描述

  5. 接收框

在这里插入图片描述
注意:此控件设置为只读模式,因为不需要我们输入。

逻辑代码

.pro文件

QT       += core gui network//这个地方需要加上这个就可以了

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QtNetwork>//需要的头文件
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    QTcpServer *tcpServer;
    QTcpSocket *tcpSocket;
private slots:
    void newConnection_Slot();
    void readyRead_Slot();
    void disconnected_Slot();
    void on_openbt_clicked();
    void on_closeBt_clicked();
    void on_sendBT_clicked();
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

此处添加头文件,声明槽函数。

.cpp文件

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpServer = new QTcpServer(this);
    tcpSocket = new QTcpSocket(this);
//    tcpServer->listen(QHostAddress::LocalHost, ui->duankouEdit_2->text().toUInt());
//    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
}
void Widget::newConnection_Slot()
{
    static int i = 0;
    tcpSocket = tcpServer->nextPendingConnection();//创建socket连接
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead_Slot()));//创建读数据凹槽
    connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected_Slot()));//创建服务端失联凹槽
    i++;
}
void Widget::readyRead_Slot()
{
    QString buf;
    buf = tcpSocket->readAll();
    ui->receiveEdit->appendPlainText(buf);
}
void Widget::disconnected_Slot()
{
}
Widget::~Widget()
{
    delete ui;
}
void Widget::on_openbt_clicked()
{
    tcpServer->listen(QHostAddress::Any, ui->duankouEdit_2->text().toUInt());
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
    QString localhostName = QHostInfo::localHostName();
    QHostInfo info = QHostInfo::fromName(localhostName);
    ui->receiveEdit->appendPlainText(localhostName);
    ui->receiveEdit->appendPlainText(info.addresses().back().toString());//first:ipv6地址,back:ipv4地址
    foreach(QHostAddress address,info.addresses())//寻找ipv4地址
    {
         if(address.protocol() == QAbstractSocket::IPv4Protocol)
            ui->receiveEdit->appendPlainText(address.toString());
    }
    quint16 port = tcpServer->serverPort();
    QString st = QString::number(port);
    ui->receiveEdit->appendPlainText(st);
}
void Widget::on_closeBt_clicked()
{
    tcpServer->close();
    tcpSocket->close();
    disconnect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
}
void Widget::on_sendBT_clicked()
{
     tcpSocket->write(ui->sendEdit->text().toLocal8Bit().data());
}

打开服务器获取主机IP地址的两种方法:

	QHostInfo info = QHostInfo::fromName(localhostName);
    ui->receiveEdit->appendPlainText(localhostName);
    ui->receiveEdit->appendPlainText(info.addresses().back().toString());
    //first:ipv6地址,back:ipv4地址
    foreach(QHostAddress address,info.addresses())//寻找ipv4地址
    {
         if(address.protocol() == QAbstractSocket::IPv4Protocol)
            ui->receiveEdit->appendPlainText(address.toString());
    }
    

项目展示

tcp服务器:
在这里插入图片描述
tcp客户端:在这里插入图片描述
打开服务器的时候,服务器端会自动输出当前服务器的IP地址和端口号。
然后将IP地址和端口号输入到客户端里面,打开客户端连接就可以了。
实现了服务器与客户端的通信。

源码资源下载

https://download.csdn.net/download/qq_30255657/85675636

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-06-25 18:24:32  更:2022-06-25 18:25:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/27 1:44:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码