QT自制TCP服务器

TCP服务器

TCP服务器实现对TCP客户端的监听,实现对数据的发送与接收。
QT自制TCP服务器_第1张图片
该TCP服务器入门级别,后面可以继续丰富。

界面

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

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

  2. 端口号输入框
    和后面发送框一样的控件
    QT自制TCP服务器_第2张图片

  3. 按钮
    QT自制TCP服务器_第3张图片

  4. 发送框
    QT自制TCP服务器_第4张图片

  5. 接收框

QT自制TCP服务器_第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 
#include 
#include 
#include 
#include //需要的头文件
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服务器:
QT自制TCP服务器_第6张图片
tcp客户端:QT自制TCP服务器_第7张图片
打开服务器的时候,服务器端会自动输出当前服务器的IP地址和端口号。
然后将IP地址和端口号输入到客户端里面,打开客户端连接就可以了。
实现了服务器与客户端的通信。

源码资源下载

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

你可能感兴趣的:(QT,qt,tcp/ip,服务器)