Question

The issue I'm having right now is regarding:

int k = send( hAccepted, p, size, 0 );

'p' is a buffer containing some file, an mp3, text file or what have you. The issue is that if the file in question is say '5,000,000' bytes, send() will return 5,000,000 every time it is called, even if the receiving end did not receive all 5,000,000 bytes. This is making it quite difficult to debug on the server side, as the client is fully aware it did not receive the whole file, but the server is quite convinced that it sent the whole thing.

Was it helpful?

Solution

send() returns the number of bytes transferred to the socket send buffer. If it returns 50,000, then 50,000 bytes were transferred. If you didn't receive them all, the problem is at the receiver, or in the network.

You would have to post some code before any further analysis is possible.

Probably you're expecting to receive all those bytes in a single recv() call. It isn't obliged to do that by its specification. You have to loop:

char buffer[8192];
int length = ... // number of bytes expected;
int total = 0;
int count;

while (total < length && (count = recv(socket, buffer, min(sizeof buffer, length-total), 0)) > 0)
{
    write(outfd, buffer, count);  // or do something else with buffer[0..count-1]
    total += count;
}
if (count < 0)
{
    perror("recv");
}

Or else you've done an abortive close at the sender which discards data in flight.

Or you got an error during recv() and didn't detect it in your code.

Or ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top