• 大小: 13KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: 其他
  • 标签: qt  struct  

资源简介

QT TCP SOCKET SEND RECIEVE STRUCT SERVER CLIENT 看见这几个关键字了,应该知道是啥东西。

资源截图

代码片段和文件信息

#include “dialogclient.h“
#include “ui_dialogclient.h“
#include 
#include 
#include 
#include 

DialogClient::DialogClient(QWidget *parent) :
    QDialog(parent)
    ui(new Ui::DialogClient)
{
    ui->setupUi(this);

    //端口验证
    QIntValidator *vali = new QIntValidator();
    vali->setRange(0 65535);
    ui->lineEditPort->setValidator(vali);
    //
    ui->lineEditPort->setText(“2000“);
    ui->lineEditIP->setText(“127.0.0.1“);

    this->setLayout(ui->mainLayout);

    //创建客户端
    m_pTCPClient = new QTcpSocket();
    //关联信号
    connect(m_pTCPClient SIGNAL(connected()) this SLOT(OnSuccConnect()));
    connect(m_pTCPClient SIGNAL(readyRead()) this SLOT(ReadStructDataAndShow()));
}

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

void DialogClient::on_pushButtonConnect_clicked()
{
    int nPort = ui->lineEditPort->text().toInt();
    QHostAddress address;
    bool bres = address.setAddress(ui->lineEditIP->text());
    //如果IP出错
    if( ! bres )
    {
        QMessageBox::warning(this tr(“IP address error“) tr(“IP address is not correct!“));
        return;
    }
    //开始连接
    m_pTCPClient->connectToHost(address nPort);

}

void DialogClient::OnSuccConnect()
{
    QMessageBox::information(this tr(“Connected“) tr(“Connect successed.“));
}

void DialogClient::ReadStructDataAndShow()
{
    QString strMsg;
    QByteArray dataArray = m_pTCPClient->readAll();
    if(dataArray.length() != sizeof(NetData))
    {
        //长度不对
        strMsg = tr(“Receive error struct length is %1 B recv length is %2 B“)
                .arg(sizeof(NetData)).arg(dataArray.length());

        qDebug()<        //
        return;
    }
    //接收长度正确
    const NetData* pData = (NetData*)(dataArray.data());
    //显示
    QString strShow = tr(“NetData: \r\n“);
    strShow += tr(“Length = %1\r\n“).arg(pData->length);
    strShow += tr(“n1 = %1\r\n“).arg(pData->n1);
    strShow += tr(“d1 = %1\r\n“).arg(pData->d1);
    strShow += tr(“d2 = %1\r\n“).arg(pData->d2);
    strShow += tr(“name = %1\r\n“).arg(pData->name);
    //显示
    ui->labelShow->setText(strShow);

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-01-07 00:03  TestClient\
     文件         397  2015-01-06 21:39  TestClient\TestClient.pro
     文件       18267  2015-01-07 00:03  TestClient\TestClient.pro.user
     文件        2295  2015-01-06 23:57  TestClient\dialogclient.cpp
     文件         774  2015-01-06 23:55  TestClient\dialogclient.h
     文件        2515  2015-01-06 23:58  TestClient\dialogclient.ui
     文件         187  2015-01-06 21:38  TestClient\main.cpp
     目录           0  2015-01-07 00:04  TestSrv\
     文件         427  2015-01-06 22:21  TestSrv\TestSrv.pro
     文件       18249  2015-01-07 00:03  TestSrv\TestSrv.pro.user
     文件         583  2015-01-06 23:47  TestSrv\clientjobs.cpp
     文件         538  2015-01-06 23:47  TestSrv\clientjobs.h
     文件        3680  2015-01-06 23:53  TestSrv\dialogsrv.cpp
     文件        1049  2015-01-06 23:51  TestSrv\dialogsrv.h
     文件        2418  2015-01-06 23:45  TestSrv\dialogsrv.ui
     文件         181  2015-01-06 21:38  TestSrv\main.cpp

评论

共有 条评论