Hi there,

I'm putting together a simple client and server to receive packets containing video data. Here is the part where data is read. The data packets contain typically around 1000 - 1500 bytes of data.

int getNoPackets = 0;
int packetsWrittenToFile = 0;
for (i=0; i<NPACK; i++) {
getNoPackets = recvfrom(s, buf, 10000, 0, &si_other, &slen);
if(getNoPackets == -1){
diep("recvfrom()"); }
else{

packetsWrittenToFile = fwrite(buf, 1, getNoPackets, destinationFile);
printf("Received packet number: %i  containing %i bytes  toFile %i \n", i, getNoPackets, packetsWrittenToFile);
}

When i have a look at the file after the transmission is finished, all data is received correctly except the last portion of the data. I have tried this with both small and large data transfers (12.1kb sent has 12.0kb written in the file and 235kb has 233kb written in the file).

The final print statement also tells me that all packets are received, the number of bytes that the packet contains, and also the amount of data written to the file.

The following is output to the terminal:

Received packet number: 0  containing 13 bytes  toFile 13 
Received packet number: 1  containing 9 bytes  toFile 9 
Received packet number: 2  containing 7904 bytes  toFile 7904 
Received packet number: 3  containing 1239 bytes  toFile 1239 
Received packet number: 4  containing 1625 bytes  toFile 1625 
Received packet number: 5  containing 1636 bytes  toFile 1636

This says that 13+9+7904+1239+1625+1636 = 12426 bytes were written to the file, whereas in reality only 12288 bytes were written.

Does anyone know the reason behind this?

Recommended Answers

All 7 Replies

Are you calling fclose when you're done with the file? fclose will make sure that all data that's left in the stream will be pushed to the file its pointing to before the program shuts down. It looks like data is getting stuck in the stream before the program terminates from your output.

Yes, i close the file on the following line. Here is the code including the close command.

int getNoPackets = 0;
int packetsWrittenToFile = 0;
for (i=0; i<NPACK; i++) {
getNoPackets = recvfrom(s, buf, 10000, 0, &si_other, &slen);
if(getNoPackets == -1){
diep("recvfrom()"); }
else{
packetsWrittenToFile = fwrite(buf, 1, getNoPackets, destinationFile);
printf("Received packet number: %i  containing %i bytes  toFile %i \n", i, getNoPackets, packetsWrittenToFile);
}
}
fclose(destinationFile);
close(s);
return 0;
}

A simple suggestion, perhaps try to see what ferror() says?

ferror doesn't flag up any errors.

A remote possibility ... maybe fclose() fails?

Is the file opened in text or binary mode?

The file is opened in binary mode.

I'm pretty sure i've found the solution anyway. Quite simple really, the for loop never terminates so the programme doesn't reach the close function.

I'll very this tonight/tomorrow.

Thanks for the replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.