首页 > 其他分享 >qt之网络协议

qt之网络协议

时间:2022-08-28 19:56:20浏览次数:54  
标签:Widget tcpSocket clicked qt 网络协议 ui include socket

tcp:

// -------------- widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer> //服务器
#include <QTcpSocket> // 套接字 发送的消息

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    QTcpServer* _tcpServer = NULL;
    QTcpSocket* _tcpSocket = NULL;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

// -------------- widget.cpp:

#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowTitle("服务端:8888");

    //_tcpSocket = new QTcpSocket(this);
    _tcpServer = new QTcpServer(this);
    _tcpServer->listen(QHostAddress::Any,8888);             // 服务第的ip和端口

    connect(_tcpServer, &QTcpServer::newConnection, [=](){  // 有人连接时触发
        _tcpSocket = _tcpServer->nextPendingConnection();   // 获取对方套接字,包含对方的基础信息
        QString ip = _tcpSocket->peerAddress().toString();  // 通过套接字获取对方的iP
        qint16 port = _tcpSocket->peerPort();               // 通过套接字获取对方的端口

        QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
        ui->textEdit->setText(temp);

        connect(_tcpSocket,&QTcpSocket::readyRead,[=](){    // 接收消息,对方连接我成功后方可接收
            QByteArray array = _tcpSocket->readAll();
            ui->textEdit->append(array);
        });

    });


}

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

void Widget::on_pushButton_clicked()                //给客户端发数据
{
    if(_tcpSocket == NULL) return;

    QString str = ui->textEdit_2->toPlainText();

    _tcpSocket->write(str.toUtf8().data());
}

void Widget::on_pushButton_2_clicked()              // 和客户端断开连接
{
    if(_tcpSocket == NULL) return;

    _tcpSocket->disconnectFromHost();
    _tcpSocket->close();

    _tcpSocket = NULL;
}


// -------------- socket.h:

#ifndef SOCKET_H
#define SOCKET_H

#include <QWidget>
#include <QTcpSocket>

namespace Ui {
class socket;
}

class socket : public QWidget
{
    Q_OBJECT

public:
    explicit socket(QWidget *parent = 0);
    ~socket();

    QTcpSocket* _tcpSocket = NULL;

private slots:
    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_clicked();

private:
    Ui::socket *ui;
};

#endif // SOCKET_H

// -------------- socket.cpp:

#include "socket.h"
#include "ui_socket.h"
#include <QHostAddress>

socket::socket(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::socket)
{
    ui->setupUi(this);

    _tcpSocket = new QTcpSocket(this);                      // 分配空间,指定父对象

    connect(_tcpSocket,&QTcpSocket::connected,[=](){        // 和服务器连接时触发
        ui->textEdit->setText("成功和服务器连接");

        connect(_tcpSocket,&QTcpSocket::readyRead,[=](){    // 接收消息,我连接对方成功后方可接收
            QByteArray array = _tcpSocket->readAll();
            ui->textEdit->append(array);
        });
    });
}

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

void socket::on_pushButton_clicked()                         // 连接服务器
{
    QString ip = ui->lineEdit->text();
    qint16 port = ui->lineEdit_2->text().toInt();

    _tcpSocket->connectToHost(QHostAddress(ip),port);
}

void socket::on_pushButton_2_clicked()                       // 发送数据
{
    if(_tcpSocket == NULL) return;

    QString str = ui->textEdit_2->toPlainText();

    _tcpSocket->write(str.toUtf8().data());
}

void socket::on_pushButton_3_clicked()                         // 断开连接
{
    if(_tcpSocket == NULL) return;

    _tcpSocket->disconnectFromHost();
    _tcpSocket->close();

    _tcpSocket = NULL;
}
基础示例

 

标签:Widget,tcpSocket,clicked,qt,网络协议,ui,include,socket
From: https://www.cnblogs.com/fxw1/p/16633479.html

相关文章

  • 【Qt6.2.4】qml 实现登录注册及显示详情demo
    参考https://www.bilibili.com/video/BV1dS4y1u7vN?spm_id_from=333.999.0.0(很棒的教程)环境环境版本windows10QT6.2.4QtCreator8.0.1(Community......
  • qt 线程和数据库
    qt使用数据库:1.使用的编译器是MinGM或MSVxx系统:一般MinGM在include环节没有问题,MSV的话有可能需要自己添加一下2.安装qt的时候没有勾选上qsqldatabase,那么需要自己下......
  • ubuntu22.04+qt6.2安装步骤
    chmod+xqt-unified-linux-x64-4.4.1-online.runsudoaptinstallvimsudoaptinstallnet-toolssudoaptinstallopenssh-serversudoapt-getinstallbuild-essen......
  • 关于qtableview开发过程中的一些记录
    使用QTableWidget刷新数据后,经常会自动展示为table首行。为了显示刷新数据前所在的位置,解决办法如下:     先记住滚动条位置,刷新数据后,再重置滚动条位置。伪代码如......
  • QT使用HTTP下载来实现程序下载自动安装退出,同时读取JSON更新信息。
    最近在用QT开发一套免费的HelpDesk系统,参考了网上的方法,实现了程序自动下载更新和程序自动退出再安装新程序,为了感谢网页的无私分享,自己也特地分享给大家,希望可以帮助到大......
  • Qt QWidget绘制圆角注意事项
    1、产生黑边painter.setPen(Qt::NoPen);//不设置画笔即可 2、背景不透明this->setAttribute(Qt::WA_TranslucentBackground,true); 3、大致代码this->setA......
  • cmake引入opencv和qt
    cmake_minimum_required(VERSION3.14)project(CMake_demoLANGUAGESCXX)set(CMAKE_INCLUDE_CURRENT_DIRON)set(CMAKE_AUTOUICON)set(CMAKE_AUTOMOCON)set(CMA......
  • qtav shader处理
    链接shader,标准openglshader处理过程boolVideoShader::build(QOpenGLShaderProgram*shaderProgram){    if(shaderProgram->isLinked()){        qWar......
  • Qt 'QApplication'file not found 解决办法
    本人初次接触Qt,第一个程序就折腾了大半天。hello.cpp#include<QApplication>#include<QLabel>intmain(intargc,char*argv[]){QApplicationa(argc,arg......
  • PyCharm+PyQt5+QtDesigner配置(三)
    换电脑这么多天了,一直没用python写界面应用,昨天用到,需要重新配置环境,于是开始在百度搜索,看到一篇博客写的挺条理的,嗯是我的菜,于是默默看完,最后发现这位大哥真的是转载的我......