I'm doing a super simple ftp server for class in C++, Client: Read from a file write in the socket. server: Read from socket write on file. I have finished all the networking and everything works but ofstream.write has a very odd behavior: It doesn't write the last packet that is smaller than 1024byte.
I have tried changing the last buffer size so that it would be the number of byte read, I have tried looking at the error flags but none occured yet nothing was written, I have tried writing 1024 byte, I does write but there is trash at the end (^@ chars).
private:
ofstream &out_stream;
for (;;)
{
nread = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&peer_addr, &peer_addr_len);
out_stream.write(buf, nread);
if (nread < 1024)
break;
}
The written file should contain 1687 bytes but contains only 1024 bytes and if I try out_stream.write(buf, 1024);
it contains 2048 bytes. I checked that the programs goes twice in the loop as it should, so the condition is not the problem.