Reputation: 703
If this has been asked before in a different wording, I apologize.
Heres my issue. I have a Linux TCP server client relationship and in a certain part of my code, the server will write() chunks (lets say 512 bytes) of a file buffer to the client. I am wanting the client to reconstruct the file on the other side by somehow redirecting the buffer chunks outward to a already made file (mimicking a copy effect). I was considering a middle man whom would translate the buffer into characters and then use fprintf etc. to push the information to a file (this is the only way I can conceive how). However, this idea does not work when working with binary files as ascii would mess up the binary.
Is there a way to redirect a file buffer pointer into another file? Thanks!
Upvotes: 1
Views: 1288
Reputation: 490048
Read a buffer from the socket using recv
, and write it to the file using fwrite
. When you open the local file, before sure to use "wb"
as the mode.
Edit: Here's a sketch of the general idea:
socket input = open_socket(server, server_port);
FILE *file = fopen("localfile.bin", "wb");
char buffer[some_size];
do {
size_t received = recv(input, some_size);
fwrite(buffer, received, 1, file);
} while (received != 0);
Upvotes: 4