通过网络发送结构化数据 [英] Sending structured data over a network

查看:168
本文介绍了通过网络发送结构化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网络编程的乞丐,所以,对不起,如果我的问题可能会出现一些明显。



我试图从Qt应用程序发送一些数据到一个Python服务器,它将处理它们并发回一些回答。



允许我在 QTcpSocket class are:

  // ... 
write(const QByteArray&)
write(const char *)
// ...



我的应用程序将管理: ,发送和接收一些复杂的数据,如 struct 和文件。



我有很多关于这种情况的问题:


  1. 上述方法是否足以发送复杂数据?

  2. 如何处理(用Python)?

  3. 你认为我应该使用其他protocole如HTTP(使用 QNetworkAccessManager class)?


解决方案

尝试回答您的问题:

$ b上面提到的方法是否足以发送复杂数据,以及如何?


好吧,是的,发送原始字节数组是最低级别的格式。但是,您需要一个某事,可以让您从复杂数据到字节数组,从字节数组到复杂数据。



这个过程以不同的方式调用,编码,序列化,编组....但是一般来说,它只是意味着创建一个系统来将复杂的结构编码为一个字节或字符序列



有很多您可以从中选择: ASN.1 JSON XML Google协议缓冲区 MIME ....



你甚至可以设计自己的TLV:(Tag-Length-Value),其中Tag是类型的标识符,值可以是基本类型[并且您必须为每个类型定义一个表示,您认为基本类型]或再次一个或多个TLV),长度



选择什么取决于编码的位置(语言/平台)和解码的位置(语言/平台)以及您对速度,带宽使用,传输,是否应检查邮件等要求。



如果您处理异构体系结构,您可能需要请考虑字面顺序



最后,你应该区分格式(即如何将复杂结构表示为行中的字节序列)和用于编码的库(或用于解码的库)。有时它们会被链接,有时候对于相同的格式,你可以选择要使用的库。


如何处理数据类型在服务器端(用Python)?


所以,这里你有一个要求...如果你要去外部提供的格式,你必须确保它有一个python库能够解码。



如果你要去一个自制的解决方案,你应该定义的事情之一是你的复杂C ++结构的表达式作为Python结构。



另一种可能性是在C ++中做一切,而对于python服务器端使用一个系统来创建python扩展(例如 boost-python swig ....)


你认为我应该使用其他协议像HTTP(使用QNetworkAccessManager类)?


这取决于你尝试做。
有很多HTTP库可供广泛使用,您可以在不同的语言和不同的体系结构中使用。



您仍然需要解决决定格式化的问题信息(虽然HTTP有一些定义的做法)。



此外,HTTP显然偏向客户端与服务器的通信,而操作总是由客户端发起。



当服务器是需要发起通信的服务器或需要发送自发信息的服务器时,事情变得复杂(或广泛支持)。


I'm a begginer in network programming, so, sorry if my questions may appear a little obvious.

I'm trying to send some data from Qt application to a Python server which will process them and send back some answer.

the methods that allows me to send data in the QTcpSocket class are:

// ...
write(const QByteArray &)
write(const char *)
// ...

my application will manage: authentification, sending and receiving some complexe data like struct, and files.

I've many questions about this situation:

  1. Are the methods mentioned above sufficient to send complexe data, and how ?
  2. how to deal with the data types in the server side (with Python) ?
  3. do you think I should use an other protocole like HTTP (with the QNetworkAccessManager class) ?

解决方案

Trying to answer your questions:

Are the methods mentioned above sufficient to send complexe data, and how ?

Well, yes, sending a raw byte array is the lowest level format. However, you need a something that can get you uniquely from your complex data to the byte array and from the byte array back to your complex data.

This process is called in different ways, encoding, serializing, marshalling.... But in general it just means creating a system for encoding complex structures into a sequence of bytes or characters

There are many you can choose from: ASN.1, JSON, XML, Google's protocol buffers or MIME....

You can even design your own (e.g. a simple schema is using TLV: (Tag-Length-Value), where Tag is an identifier of the Type and Value can be a basic type [and you have to define a representation for each type that you consider basic] or again one or more TLV), Length indicates how many bytes/characters are used to encode the Value.

What to choose depends a lot of where you encode (language/platform) and where you decode (language/platform) and your requirements for speed, bandwidth usage, transport, whether messages should be inspected... etc.

If you're dealing with heterogenous architectures you might need to think about endianness.

Finally, you should distinguish between the format (i.e. how the complex structure is expressed as a sequence of bytes in the line) and the library used for encoding (or the library used for decoding). Sometimes they will be linked, sometimes for the same format you will have a choice of libraries to use.

how to deal with the data types in the server side (with Python) ?

So, here you have a requirement... if you're going for a externally provided format, you must make sure it has a python library able to decode it.

if you're going for a home-grown solution, one of things you should define is the expression of your complex C++ structures as Python structures.

An additional possibility is to do everything in C++, and for the python server side use one of the systems for creating python extensions in C++ (e.g. boost-python or swig....)

do you think I should use an other protocol like HTTP (with the QNetworkAccessManager class) ?

It depends on what you try to do. There are many HTTP libraries widely available that you can use on different languages and different architectures.

You still need to solve the problem of deciding the formatting of your information (although HTTP have some defined practices).

In addition HTTP is clearly biased towards the communication of a client with a server, with the action always initiated by the client.

Things get complex (or less widely supported) when is the server the one that needs to initiate the communication or the one that needs to send spontaneous information.

这篇关于通过网络发送结构化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆