简体   繁体   English

TCP套接字数据以XML数据包发送

[英]tcp socket data send in xml packet

I'm trying to send some data over tcp socket. 我正在尝试通过tcp套接字发送一些数据。 Now the thing is instead of sending simple char* data I need to buffer some data and make one xml packet structure out there and therefore send that xml data over the network. 现在,事情不再是发送简单的char *数据,我需要缓冲一些数据并在那里制作一个xml数据包结构,然后通过网络发送该xml数据。 For example I have one ini file to read all the data into corresponding variables. 例如,我有一个ini文件,可将所有数据读入相应的变量。 say: 说:

[Device data ini]

DeviceID = 0042
Manufacturer = Company Name
VendorID = 00-291-647
Timestamp = 2014-08-13 12:40:11

Now I need to make the xml out of above data like below: 现在,我需要根据上述数据制作xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<DataBlock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <DeviceID>0042</Device>
    <Manufacturer>Company Name</Manufacturer>
    <VendorID>00-291-647</VendorID>
    <TimeStamp>2014-08-13 12:40:11</TimeStamp>
</DataBlock>

And send these whole data as char* to server. 并将这些全部数据作为char *发送到服务器。 What would be the best approach to build the xml data packet from simple char* data structure. 从简单的char *数据结构构建xml数据包的最佳方法是什么。 Code will be written in c++ and the xml will be parsed at the server side with c# code. 代码将用c ++编写,而xml将在服务器端使用c#代码进行解析。 Any suggestions or recommendation would be off great help. 任何建议或建议将无济于事。

Use any C++ tokenizer to split input/source data in order to process each line separately (use \\r\\n as separators). 使用任何C ++标记器拆分输入/源数据,以便分别处理每行(使用\\r\\n作为分隔符)。 For example you can use boost::split to do that. 例如,您可以使用boost::split来做到这一点。 After that for each line use the same technique to extract the key and the value (use = as separator character). 之后,对于每一行,使用相同的技术提取键和值(使用=作为分隔符)。 You can put parsed keys and values to a std::map because you'll need them on next step. 您可以将解析的键和值放入std::map因为下一步需要它们。 The last step - simply format your target xml string using parsed data. 最后一步-使用解析的数据简单格式化目标xml字符串。 To do that you need to iterate through map elements and format the result string. 为此,您需要遍历map元素并格式化结果字符串。 You can also use XML template string with placeholders for each attribute and replace them with parsed values. 您还可以将XML模板字符串与每个属性的占位符一起使用,并将其替换为解析后的值。

I would suggest that it would not be good approach to first convert the data into XML, until and unless its very important or there is no other choice. 我建议先将数据转换为XML并不是一个好方法,除非并且除非它非常重要或没有其他选择。 The reason for this is that when you are formatting the values in XML, you are increasing the number of bytes to be send over the network. 这样做的原因是,当您以XML格式格式化值时,会增加要通过网络发送的字节数。 Instead of this, a better approach would be send the values to the other end and convert it to XML before handing over to the C#. 取而代之的是,更好的方法是将值发送到另一端并将其转换为XML,然后再移交给C#。 You can use structure approach for sending and receiving. 您可以使用结构方法进行发送和接收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM