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:
Some lines are sent once (checked), but received twice. Plus, after a line is read twice, the next 10-20 lines are read mistakenly - always 143 bytes are sent, but it receives only the last 120-100 (sometimes less) bytes.
Here is the relevant client (sender) code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
for (sendIterator = sendList.begin(); sendIterator != sendList.end(); sendIterator++)
{
dact2str(&*sendIterator, buffer);
buffer_length = sizeof buffer;
bytes_sent = send(sock, buffer, buffer_length, 0);
cout << "Bytes sent: " << bytes_sent << endl;
if (bytes_sent < 0)
{
Die(strerror(errno));
}
}
|
And this is the relevant receiver code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
while (true)
{
recsize = recv(connectFd, (void *) buffer, 143, 0);
if (recsize < 0)
{
fprintf(stderr, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
else if (recsize == 0)
{
break;
}
wline_dact = buffer;
if (wline_dact.size() < 142)
{
cout
<< "Invalid Dact. Expected size: 142. Received size: "
<< wline_dact.size() << endl;
continue;
}
wline_dact = wline_dact.substr(0, 142);
DACT dact;
str2dact(&dact, wline_dact);
receivedDacts.push_back(dact);
}
|
Later, in the receiver code, I check if any dact was received more than once. That's how I know what is happening.
Thank you for your attention.