I have this problem:
I have a data structure called DACT, that I convert into a string line and send to another computer. I have a file full of DACTs, and I need to send them all to another computer.
When I send only 100, it works fine, but when I send more, like 500, a bug happens:
It appears that some lines are sent once (checked), but received twice.
I say 'appears' because, after I receive the DACTs, I check if I already have any of the DACTs received, and discard it. For testing, I clear all the DACTs from the receiver and turn the sender on (without any repeated DACT). It happens that the receiver finds some 'repeated DACTs' and discards them. In my last test, I sent 500 and it discarded 18.
Here is the receiver code, omitting error verifications to make it short:
while (true)
{
recsize = recv(connectFd, (void *) buffer, DACT_LENGHT, 0);
// Iterates ensure the whole dact is received
dact_line.append(buffer);
while (toReceive != recsize)
{
toReceive = toReceive - recsize;
recsize = recv(connectFd, (void *) buffer, toReceive, 0);
dact_line.append(buffer);
}
toReceive = DACT_LENGHT;
DACT dact;
str2dact(&dact, dact_line);
receivedDacts.push_back(dact);
dact_line.clear();
}
Here is the code that verifies if the DACT has been repeated. Quite simple.
itrecdact = receivedDacts.begin();
for (; itrecdact != receivedDacts.end(); itrecdact++)
{
repeated = false;
itdact = dacts->begin();
for (; itdact != dacts->end(); itdact++)
{
if (!strcmp(itdact->getIdDact(), itrecdact->getIdDact()))
{
repeated = true;
break;
}
}
if (!repeated)
{
dact2str(&*itrecdact, buffer);
line_dact = buffer;
grava_dact(no, &line_dact);
}
}
And here is the sender code. Also simple.
for (itSend = listaEnvio.begin(); itSend != listaEnvio.end(); itSend++)
{
dact2str(&*itSend, buffer);
buffer_length = sizeof buffer;
bytes_sent = send(sock, buffer, buffer_length, 0);
cout << "Bytes enviados: " << bytes_sent << endl;
}
I've been weeks looking for the error.
Thank you.