我需要发送的结构体

struct NetDataHeader_t {
int nDataType;
int nDataSize;
}; struct NetDataBase_t {
NetDataHeader_t dataHeader;
double hValue;
double lValue;
double temperature;
};

我的运行函数

 #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "sensordata.h"
#include <cstring> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); mySocket = new QTcpSocket();
QObject::connect(mySocket, &QTcpSocket::readyRead, this, &MainWindow::MySocketReadData);
QObject::connect(mySocket, &QTcpSocket::disconnected, this, &MainWindow::MySocketDisconnected); ui->btnSend->setEnabled(false);
ui->lineEdit_IP->setText("127.0.0.1");
ui->lineEdit_Port->setText("");
} MainWindow::~MainWindow()
{
delete mySocket;
delete ui;
} void MainWindow::MySocketReadData()
{
QByteArray buffer; buffer = mySocket->readAll();
struct NetDataBase_t myData;
if(!buffer.isEmpty()) {
char *src = buffer.data();
memcpy(&myData, src, sizeof(struct NetDataBase_t));
ui->textEdit_Recv->setText(QString("hValue:%1,lValue%2,temp:%3")
.arg(QString::number(myData.hValue, 'g', ))
.arg(QString::number(myData.lValue, 'g', )).arg(myData.temperature));
}
} void MainWindow::MySocketDisconnected()
{
ui->btnSend->setEnabled(false); ui->btnConnect->setText("连接");
qDebug()<<"Disconnected!";
} void MainWindow::on_btnSend_clicked()
{
struct NetDataBase_t myData;
myData.dataHeader.nDataSize = ;
myData.dataHeader.nDataSize = sizeof(struct NetDataBase_t) - sizeof(struct NetDataHeader_t);
myData.hValue = -1.234567890;
myData.lValue = 0.123456789;
myData.temperature = 23.1; QByteArray data;
data.resize(sizeof(struct NetDataBase_t));
memcpy(data.data(), &myData, sizeof(struct NetDataBase_t)); mySocket->write(data);
mySocket->flush();
} void MainWindow::on_btnConnect_clicked()
{
if(ui->btnConnect->text() == tr("连接")) {
QString ip;
int port;
ip = ui->lineEdit_IP->text();
port = ui->lineEdit_Port->text().toInt(); mySocket->abort();
mySocket->connectToHost(ip, port);
if(!mySocket->waitForConnected()) {
qDebug()<<"Connection failed!";
return;
}
qDebug()<<"Connect successfully!"; ui->btnSend->setEnabled(true);
ui->btnConnect->setText("断开连接");
} else {
mySocket->disconnectFromHost();
ui->btnConnect->setText("连接");
ui->btnSend->setEnabled(false);
}
}

同时使用QBytearray的data函数转换成字符串形式。

然后用memcpy进行复制和转换。

--------------------------------

或许还可以这样,没试过:把结构体转换成一个QString型的就可以write了,然后就可以了。
用QString::fromascii()去转换吧,或者是QString::fromlocal8bit()去转换

QTcpSocket发送结构体的更多相关文章

  1. C# Socket 入门4 UPD 发送结构体(转)

    今天我们来学 socket  发送结构体 1. 先看要发送的结构体 using System; using System.Collections.Generic; using System.Text; ...

  2. Linux C Socket编程发送结构体、文件详解及实例

    利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件.结构体.数字等等. 本文:http://www.c ...

  3. socket发送结构体

    struct send_info {char info_from[20]; //发送者IDchar info_to[20]; //接收者IDint info_length; //发送的消息主体的长度c ...

  4. pythonUDP发送结构体,对齐到C++结构体

    给出程序先: import random import socket import struct import threading import pickle import json from str ...

  5. java socket传送一个结构体给用C++编写的服务器解析的问题

    另一端是Java写客户端程序,两者之间需要通信.c++/c接收和发送的都是结构体,而Java是直接发送的字节流或者byte 数组.解决方法:c++/c socket 在发送结构体的时候其实发送的也是字 ...

  6. enginefuncs_t 结构体中的函数

    就是常见的 g_engfuncs 中的函数.AMXX 里就是 fakemeta 的 EngFunc_** // 这些函数由引擎提供给EXTDLL使用.mp.dll hl.dll ... typedef ...

  7. C#结构体数组间的转化

    转自:http://developer.51cto.com/art/200908/143779.htm 解决C#结构体数组间的转化问题的由来:在写C#TCP通信程序时,发送数据时,如果是和VC6.0等 ...

  8. WPF向系统发送消息 并传递结构体

    场景 :需要开发一个通讯组件 流程为:界面-开启接收服务-通过发送组件发送信息到 其他客户端和服务端 接受服务接收其他客户端发送的消息 需要传递给对应组件或者界面 因此会出现类库重复引用问题.因为采用 ...

  9. C#引用c++DLL结构体数组注意事项(数据发送与接收时)

    本文转载自:http://blog.csdn.net/lhs198541/article/details/7593045 最近做的项目,需要在C# 中调用C++ 写的DLL,因为C# 默认的编码方式是 ...

随机推荐

  1. 【Linux】设置开机自启

    忘记转发的哪里的. 方法 1这种方法会利用 /etc/ 中的 rc.local 文件来在启动时执行脚本与命令.我们在文件中加上一行来执行脚本,这样每次启动系统时,都会执行该脚本. 不过我们首先需要为 ...

  2. JQUERY的$(function(){})和window.onload=function(){}的区别【转】

    在Jquery里面,我们知道入口函数有两种写法:$(function(){}) 和$(document).ready(function(){}) 作用类似于传统JavaScript中的window.o ...

  3. scrapy工作流程

    整个scrapy流程,我们可以用去超市取货的过程来比喻一下 两个采购员小王和小李开着采购车,来到一个大型商场采购公司月饼.到了商场之后,小李(spider)来到商场前台,找到服务台小花(引擎)并对她说 ...

  4. error C2664: “ATL::CStringT<BaseType,StringTraits>::Remove”: 不能将参数 1 从“const char [2]”转换为“char”

    转自VC错误:http://www.vcerror.com/?p=1395 问题描述: 代码: CString str("asdfafda"); str.Remove(" ...

  5. Sublime Text 3 快捷键总结(Mac)

    Command + Shift + L 光标同时定位多行 Command + Enter 在下一行插入新行.举个栗子:即使光标不在行尾,也能快速向下插入一行.

  6. centos7运行yum报如下提示:Run "yum repolist all" to see the repos you have

    centos7运行yum报如下提示: There are no enabled repos. Run "yum repolist all" to see the repos you ...

  7. 动态创建类/ swizzle class

    动态创建类 Class subclass = objc_allocateClassPair(baseClass, subclassName, );//生成,指定父类 //添加方法,变量...一些操作 ...

  8. Vue2.0---vuex初理解

    先来一张vuex的帅照 第一眼看到这张图片我内心是万匹草泥马飞过. 简单理解:  vuex:一个可以全局被使用的状态管理的“仓库”:state.js中定义初始状态,通过action去触发mutatio ...

  9. c# Winform dev控件之ChartControl

    1.改变颜色 字体颜色 背景颜色 XYDiagram dia = chartControl1.Diagram as XYDiagram; dia.AxisX.Label.TextColor = Col ...

  10. [fw]How to use DISM to install a hotfix from within Windows

    How to use DISM to install a hotfix from within Windows Jeff Hughes (CORE) 15 Feb 2011 10:10 AM 22 H ...