TCPIP 数组传输

别等时光非礼了梦想. 提交于 2019-12-04 06:56:08

最近一直在弄TCPIP进行数据传输,刚刚入门,网上搜了很多例子,今天终于完成了一个小demo

客户端发数组:主要就是数据类型的转化从float 转化成QByteArray

//1.建立客户端连接套接字connected 2.建立连接客户端不断监听服务器的信息readyread

//3.读取服务器信息readAll 4.设置连接按钮tcpsocket->connectToHost;
//5.设置发送信息的按钮write()
//6.关闭套接字disconnectFromHost()和close()
#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpsocket = new QTcpSocket(this);
    connect(tcpsocket,&QTcpSocket::connected,
            [=]()
    {
        QString str = "successfully connect!";
     ui->textBrowser->append(str);

    });
    connect(tcpsocket,&QTcpSocket::readyRead,
            [=]()
    {
       QByteArray array = tcpsocket->readAll();
       QByteArray arrayServer;
       arrayServer.append(array);
       double dataServer[10];
       memcpy(&dataServer,arrayServer.data(),sizeof(dataServer));
       for(int i = 0;i<10;i++)
       ui->textBrowser->append(QString::number(dataServer[i]));
    });
}

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

void Widget::on_connectBtn_clicked()
{
    QString addr = ui->addrLed->text();
    qint16 port = ui->portLed->text().toInt();
    tcpsocket->connectToHost(QHostAddress(addr),port);
}

void Widget::on_closeBtn_clicked()
{
    tcpsocket->disconnectFromHost();
    tcpsocket->close();
}

void Widget::on_sendBtn_clicked()
{//QString  str1 = "hello world!";
    float data[20] = {1.1f,1.2f,1.3f,1.4f,1.5f,1.6f,1.7f,1.8f,1.9f,2.0f,2.1f,2.2f,2.3f,2.4f,2.5f,2.6f,2.7f,2.8f,2.9f,3.0f};
    QByteArray array1;
  array1.append((char*)&data,sizeof(data));
   // QByteArray array = str1.toLocal8Bit();
  qDebug()<<array1;
    tcpsocket->write(array1);
}服务器接受://服务器的步骤和函数
//1.建立套接字 2.监听listen(QHostAddress,888) 3.建立连接newConnection
//4.netPendingconnection,peerAdress,peerPort 5.建立连接不断监听readyread
//6.QByteArray array = tcpsocket->readAll();

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

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    tcpserver = new QTcpServer(this);
    //监听,建立连接套接字
    tcpserver->listen(QHostAddress::Any,8888);
    connect(tcpserver,&QTcpServer::newConnection,//设置新连接
            [=]()
    {
        tcpsocket = tcpserver->nextPendingConnection();
        QString ip = tcpsocket->peerAddress().toString();
        qint16 port = tcpsocket->peerPort();
        QString  temp = QString("[%1:%2]sucessfully connect!").arg(ip).arg(port);
        ui->textBrowser->setText(temp);
        connect(tcpsocket,&QTcpSocket::readyRead,
                [=]()
        {//解析浮点数据
            QByteArray array = tcpsocket->readAll();//QByteArray转浮点数据
            QByteArray arraybuffer;
            arraybuffer.append(array);
            float data[20];
            memcpy(&data,arraybuffer.data(),sizeof(data));
           // ui->textBrowser->append(data[10]);
            for(int i = 0;i<20;i++)
           { qDebug()<<data[i];
            ui->textBrowser->append(QString::number(data[i]));}


            //写入客户端数据
            double dataServer[10] = {11.1,11.2,11.3,11.4,11.5,11.6,11.7,11.8,11.9,12.0};
            QByteArray arrayServer;
            arrayServer.append((char*)&dataServer,sizeof(dataServer));
            tcpsocket->write(arrayServer);
            qDebug()<<"server:"<<arrayServer;
        });

    }
    );

}

Widget::~Widget()
{
    delete ui;
}
无论怎么样,今天终于实现了,开心,同时也希望各路大佬多多指教

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!