English 中文(简体)
通过校验和快速验证1500字节的内存
原标题:Validate 1500 bytes of memory quickly via checksum

我在C++中写一个程序 将1500字节的数据 从A机器发送到B机器

假设如下:

char* tx_data = (char*)operator new(1500)
for (int i = 0; i < 1500; i++) {
  tx_data[i] = (char)((int) 65); // ASCII 65 = A;
}
send_tx_data();

因此,我们有字母A 1500乘以填充此缓冲( tx_data ) 。 接收主机抓取输入的数据并将其丢入缓冲( rx_data ), 并抓取收到数据的长度( rxLength );

rxLength = recvfrom(sock, rxdata, 1500, 0, NULL, NULL);
           //             ^ the buffer we are putting into

如果 rxLength = 1500 < \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ < \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \

我怎样才能有效地评估收到的1500字节数据? [通过效率,我是说快速。这将会发生千次一秒,所以代码需要相当的优化!]。我有一个关于某种核对总和的想法,因为我们知道我们应该收到什么,但我不确定这样做的好办法。有人能建议一个好的核对方法吗?或者,如果这是一个愚蠢的想法,你能解释为什么或者建议别的东西吗?

最佳回答

在对散列函数和 CRCs 进行多重吸附后, 我使用< a href=" https:// stackoverflow.com/a/7968917/560065" 标题的代码 = "意外与 std 碰撞:: hash" 。 < this answerd to another question, 来做一个简单的散列函数 。

问题回答

首先考虑你将1500字节的数据从A机发送到B机的具体程序,并检查你在这个程序中是否有任何步骤可能造成任何损失或腐败。你的程序可能没有采取任何步骤引入任何这些步骤。例如,如果你使用TCP/IP发送数据,则保证通过基本的 TCP/IP 堆叠正确(如果有的话)收到你的数据。

另一方面,如果您在引入数据丢失、腐败、秩序变化等方面采取了某些步骤,您应该考虑 CRCs, 如果您如此重视业绩的话。 您可以在此找到使用 CRCs 的完整解释和源代码示例 : < a href="http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculating-C-Code" rel=“nofollow” >http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calcult-C-Code





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?