According to this solution to send out an image through TCP. Since the code is very elegant compared to other ways and both image and file are data, I believe that we can use almost the same code to send out a file.
So if I want to send a file from a client to a server.
On the client side
- get file size
- send file size // Above steps will always work, so I will only show code after here
read file content into a buffer
char buf[size]; read(fs,buf,size);
send the buffer
int bytes = 0; for (uint i = 0;i<size;i+=bytes) { if ((bytes = send(sock,buf+i,size-i,0))<0) { fprintf(stderr,"Can not send file\n"); close(fd); return false; } fprintf(stderr,"bytes write = %d\n",bytes); }
And on the server side
- recv file size
recv stuff into a buffer with size from step 1
char buf[size]; int bytes=0; for (uint i = 0;i<size;i+=bytes) { if ((bytes = recv(sock,buf+i,size-i,0))<0) { fprintf(stderr,"Can not receive file\n"); return false; } fprintf(stderr,"bytes read = %d\n",bytes); }
write buffer to a file
fwrite(buf,sizeof(char),size,fs);
This code will compile and run.
When I send out an cpp binary file(24k) from client to server, since both client and server are on the same machine (OS X), this binary file will be received and can be executed.
But if the server forward the file back to the client, and client forward this file back to the server multiple times, this binary file will be corrupted. But the number of bytes sent and number of bytes received are the same, and the file size is still 24k.
I am wondering what is going wrong here.
Is this an OS bug?
Thanks,