I am writing a program in C++ that is sending 1500 bytes of data from machine A to machine B.
Assume the following:
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();
So we have the letter 'A' 1500 times to fill this buffer (tx_data
). The receiving host grabs incoming data and dumps it into a buffer (rx_data
), and grabs the length of the received data (rxLength
);
rxLength = recvfrom(sock, rxdata, 1500, 0, NULL, NULL);
// ^ the buffer we are putting into
If rxLength == 1500
, we have received 1500 bytes of data, but we need to be sure it's the same 1500 bytes we sent (and not 1500 bytes of data from else where, flying round the network)! Typically when comparing some small amount of user input for example, strncmp()
might be used. I'm not suggestion that strncmp()
is appropriate here, what I am saying though, is that I don't think it's a good idea to have on the receiving end, a buffer that might be called expected_data
for example, that contains 1500 x 'A' and I loop through and compare the two, like strncmp()
.
How can I evaluate the 1500 bytes of data received in an efficient manner? [By efficient I mean quickly. This will be happening thousands of times a second, so the code needs to be fairly optimal!]. I had an idea about a checksum of some sort, as we know what we should be receiving, but I'm not sure of a good way to do this. Can anyone suggest a good checksum method? Alternatively, if that is a silly idea, could you explain why and perhaps recommend something else?