如何通过 QTcpSocket 发送整数?
我是 Qt 新手,所以如果这是一个完全愚蠢的问题......
我正在尝试使用 QTcpSocket。
如果我这样做:
...
QTcpSocket * socket
socket = new QTcpSocket(this);
socket.write(1);
...
它会抱怨 write() 不适用于整数(不是 const char *)。
如果我这样做:
...
QTcpSocket * socket
socket = new QTcpSocket(this);
socket.write("1");
...
另一方将其视为整数 49(ASCII 表示 1)。
在类似但不同的问题上,是否可以通过 QTcpSocket 发送结构或联合?
===================================================
编辑:
服务器已经接受整数,并且期待一个整数 - 我对此无法控制。
I'm new to Qt, so if this is a completely stupid question...
I'm trying to use QTcpSocket.
If I do:
...
QTcpSocket * socket
socket = new QTcpSocket(this);
socket.write(1);
...
It complains about write() not working with integers (not a const char *).
If I do:
...
QTcpSocket * socket
socket = new QTcpSocket(this);
socket.write("1");
...
the other side sees it as the integer 49 (ASCII for 1).
On a similar but different issue, is it possible to send structs or unions over QTcpSocket?
==================================================
EDIT:
The server already accepts integers, and is expecting an integer - I have no control over that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您遇到的问题实际上与 Qt 无关,任何其他 Socket 或 Streaming 接口都会出现同样的问题。
服务器的提供商需要向您提供协议描述。该描述通常包含所使用的端口(TCP、UDP、数字)、其他 TCP 参数以及传输数据的编码。有时,该协议(或其实现)称为(协议)堆栈。
编码不仅包含字节顺序,还包含复杂结构如何传输的描述。
后一种信息通常以称为“ASN.1”(抽象语法表示法)的方式进行编码。
如果您的服务器非常简单,只接受一个接一个的整数,没有任何元信息,并且位于同一平台上,那么您可以执行以下操作:
将整数的地址作为数据缓冲区开始并传输为您的整数有多少个字节。
但请注意,如果您将数据从 Intel 架构传输到 16 位架构或 motorola PPC,则会失败。
The problem you have is not really related to Qt, the same issue would arise with any other Socket or Streaming interface.
The provider of the Server needs to give you the protocol description. This description usually contains the ports used (TCP, UDP, numbers), other TCP parameters and the coding of the transmitted data. Sometimes, this protocol (or its implementation) is called a (protocol-) stack.
The coding not only contains the byte ordering, but also a description of how complex structures are transmitted.
The latter information is often coded in something that is called "ASN.1" - Abstract Syntax Notation.
In case your server is really simple and just accepts Integers one after the other without any meta-information and is on the same platform, than you could do something like this:
You take the address of your integer as a data buffer start and transmit as many bytes as your integer has.
But note well, this will fail if you transmit data from an Intel architecture to a 16-bit architecture or a motorola PPC.
我建议将 QDataStream 与套接字一起使用。这将保护您免受小端/大端转换问题的影响。
所以如下:
I suggest using QDataStream with sockets. This will protect you from little endian/big endian conversion problem.
So something as below :
当您编写字符串表示形式时,您还必须解释另一侧的字符串。例如,QString::toInt()。
当您想将整数写为整数时,您将获得更高的吞吐量,因为传输所需的字节数更少。但是,您应该阅读有关网络字节顺序的主题。
原则上,可以将结构等复制到缓冲区中,也可以通过网络复制。然而,当您在不同架构之间甚至仅在不同版本的软件之间传输数据时,事情会再次变得复杂。所以你不应该发送原始数据,而应该使用序列化!看这个问题:
使用 Qt 进行序列化
它提供了有关如何从对象生成流以及从流生成对象的答案。然后您可以使用这些流通过网络进行传输。那么您就不必再处理整数本身了!
When you write in a string representation, you also have to interpret the string on the other side. There is, for example, QString::toInt().
When you want to write the integer as integer, you will have more throughput, as it takes less bytes to transmit. However you should read about the topic of network byte order.
In principal it is possible to copy structs, etc. into a buffer and also over the network. However things get complicated again when you transmit data between different architectures or even only different builds of your software. So you shouldn't send the raw data, but use serialization! See this question:
Serialization with Qt
It provides answers on how to generate streams out of objects and objects out of streams. These streams is what you then use to transmit over the network. Then you don't have to deal with the integers themselves anymore!
不同的重载,您正在寻找:
和
Different overloads, you are looking for:
and