基于TCP的QT服务器与QT客户端的搭建

1、客户端的搭建

client.pro

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#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

widget.h

#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();
public slots:
    void read_data();

private slots:
    void on_connect_btn_clicked();

    void on_send_btn_clicked();

private:
    Ui::Widget *ui;
    QTcpSocket *client;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QTcpSocket>
#include  <QHostAddress>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("client");
    this->setWindowIcon(QIcon("./client.png"));
    //实例化
    this->client=new QTcpSocket(this);
    //信号与槽函数的关联

    connect(this->client,&QTcpSocket::connected,this,[=]()
    {
           QMessageBox::information(0,"客户端","连接成功");
    });
    connect(this->client,&QTcpSocket::readyRead,this,&Widget::read_data);


}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_connect_btn_clicked()
{
    //获取ip和port
    QString ip=this->ui->ip_edit->text();
    int port=this->ui->port_edit->text().toUInt();
    //连接
    this->client->connectToHost(QHostAddress(ip),port);

};

widget.ui

2、服务器端的搭建

 server.pro

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#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

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
public slots:
    void read_data();
private slots:
    void on_send_btn_clicked();

private:
    Ui::Widget *ui;
    QTcpServer *server;
    QTcpSocket *client;
};
#endif // WIDGET_H

widget.cpp

 注意:ip号查看自己的电脑配置

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置窗口标题
    this->setWindowTitle("server");
    //实例化服务器套接字
    this->server = new QTcpServer(this);
    //创建监听队列并绑定ip和port
    this->server->listen(QHostAddress("192.168.2.107"), 9999);
    //等待客户端连接==》本质就是等信号产生     信号与槽函数关联
    
    connect(this->server, &QTcpServer::newConnection, this,
            [=]()
    {
            //连接客户端
           this->client = this->server->nextPendingConnection();
           //收消息的信号与槽函数的关联
           connect(this->client, &QTcpSocket::readyRead, this, &Widget::read_data);
    });
}

Widget::~Widget()
{
    delete ui;
}

//读数据
void Widget::read_data()
{
     //从当前的这个客户端读缓存区中读数据
     QByteArray data = this->client->readAll();
     //把读取到的数据追加写入到 接收文本框中
     this->ui->recv_edit->append(data);
}

//发数据
void Widget::on_send_btn_clicked()
{
    //获取到发送文本框中的内容
    QString data = this->ui->send_edit->text();
    //写给客户端
    this->client->write(data.toUtf8());
    //清空发送文本框中的内容
    this->ui->send_edit->clear();
}

widget.ui

 3、实现效果

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值