通过TCP填充数据(Padding data over TCP)

编程入门 行业动态 更新时间:2024-10-24 18:18:34
通过TCP填充数据(Padding data over TCP)

我正在开发一个客户端 - 服务器项目,需要实现一个逻辑,我需要在继续之前检查是否通过TCP套接字连接收到了最后一个数据。 为了确保我已收到所有数据,我打算在发送的最后一个数据包上填写一个标志。我有两个选项,如下所示,还有相关的概率。

一世。 使用如下的结构,并为发送的最后一个数据包填充vst_pad,并在recv端检查它是否存在。 优于选项二的优点是,在将其写入文件之前,我不必从实际数据中删除该标志。只需检查结构的第一个成员

typedef struct { /* String holding padding for last packet when socket is changed */ char vst_pad[10]; /* Pointer to data being transmitted */ char *vst_data; //unsigned char vst_data[1]; } st_packetData;

问题是我必须在每次发送调用时序列化结构。 此外,我不确定是否将在一次recv调用中通过TCP接收整个结构,因此必须添加逻辑/开销以每次检查这一点。 到目前为止,我已经实现了这一点,但后来认为基于流的TCP可能无法保证在一次调用中恢复整个结构。

II。 使用类似strncat的函数将结尾的标志添加到最后发送的数据中。

问题是我必须使用正则表达式函数或strstr函数检查每个接收调用是否存在该标志,如果必须,则必须从数据中删除它。

该应用程序将用于大型数据传输,因此希望在每次发送/接收/读/写调用时增加最小的开销。 真的很感激知道是否有更好的选择,然后上面两个或任何其他选项来检查最后一个数据包的收据。 该程序是多线程的。

编辑:我不知道我要发送的文件的总大小,但我发送固定数量的数据。 这是fgets读取,直到指定大小-1或直到遇到新行。

I am working on a client-server project and need to implement a logic where I need to check whether I have received the last data over a TCP socket connection, before I proceed. To make sure that I have received all the data , I am planning to pad a flag to the last packet sent.I had two options in mind as below and also related prob.

i. Use a struct as below and populate the vst_pad for the last packet sent and check the same on the recv side for its presence. The advantage over option two is that, I dont have to remove the flag from actual data before writing it to a file.Just check the first member of the struct

typedef struct { /* String holding padding for last packet when socket is changed */ char vst_pad[10]; /* Pointer to data being transmitted */ char *vst_data; //unsigned char vst_data[1]; } st_packetData;

The problem is I have to serialize the struct on every send call. Also I am not sure whether I will receive the entire struct over TCP in one recv call and so have to add logic/overhead to check this every time. I have implemented this so far but figured it later that stream based TCP may not guarantee to recv entire struct in one call.

ii. Use function like strncat to add that flag at the end to the last data being sent.

The prob is I have to check on every receive call either using regex functions or function like strstr for the presence of that flag and if so have to remove it from the data.

This application is going to be used for large data transfers and hence want to add minimal overhead on every send/recv/read/write call. Would really appreciate to know if there is a better option then the above two or any other option to check the receipt of last packet. The program is multithreaded.

Edit: I do not know the total size of file I am going to send, but I am sending fixed amount of data. That is fgets read until the size specified -1 or until a new line is encountered.

最满意答案

您是否事先知道数据的大小,是否要求实现消息结束标志?

因为我会简化设计,所以添加一个4字节的标头(假设你没有为每条消息发送超过4GB的数据),它包含了预期的消息大小。

因此,您解析前4个字节,计算大小,然后继续调用recv,直到获得那么多数据。

您需要处理recv调用从下一条消息中获取数据的情况,以及明显的错误处理。

你的10byte pad解决方案没有提出的另一个问题是如果实际的消息包含10个零字节会发生什么 - 假设你用零填充它? 您需要转义10字节的零,否则您可能会错误地截断消息。

使用固定大小的标头和已知的大小值将缓解此问题。

Do you know the size of the data in advance, and is it a requirement that you implement a end of message flag?

Because I would simplify the design, add a 4-byte header (assuming you're not sending more than 4gb of data per message), that contains the expected size of the message.

Thus you parse out the first 4 bytes, calculate the size, then continue calling recv until you get that much data.

You'll need to handle the case where your recv call gets data from the next message, and obviously error handling.

Another issue not raised with your 10byte pad solution is what happens if the actual message contains 10 zero bytes--assuming you're padding it with zeros? You'd need to escape the 10bytes of zeros otherwise you may mistakenly truncate the message.

Using a fixed sized header and a known size value will alleviate this problem.

更多推荐

本文发布于:2023-07-23 12:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   TCP   data   Padding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!