transfer file using recv() and fwrite()

M

michelle

Hi, I am trying to transfer a binary file from a client program to the
server, so on the server side I am using recv() to receive the binary
data to the buffer and then write to a file. The follwoing is my
code. Somehow, the newly transfered file is different from the
original file. I don't know if the error is at recv() or fwrite().
For receving, I already check that the bytes being sent equals to the
btyes being received. So anyone knows what's wrong with my code?
Thanks.

char buf[length]; (length is how many bytes of data)
fid=fopen("dataio_testing.bit", "wb"); (open a file to write binary
data)
//this part is right, coz it receive right amount of bytes.
while (bytes != length) {
bytes += recv(clisock, buf, length-bytes, 0);

}

num=fwrite(buf, 1, bytes, fid); (write data into file, in unit of
bytes)
fclose(fid);
 
R

Rob van der Leek

Hi, I am trying to transfer a binary file from a client program to the
server, so on the server side I am using recv() to receive the binary
data to the buffer and then write to a file. The follwoing is my
code. Somehow, the newly transfered file is different from the
original file. I don't know if the error is at recv() or fwrite().
For receving, I already check that the bytes being sent equals to the
btyes being received. So anyone knows what's wrong with my code?
Thanks.

char buf[length]; (length is how many bytes of data)
fid=fopen("dataio_testing.bit", "wb"); (open a file to write binary
data)
//this part is right, coz it receive right amount of bytes.
while (bytes != length) {
bytes += recv(clisock, buf, length-bytes, 0);

Depending on the size of the data and the size of the messages, it could
happen that you receive multiple messages. The recv() function returns
the number of bytes received so that adds up nicely to the amount of bytes
you expect, but you store all messages at the start of the buffer
(overwriting older messages with newer ones.) You could use the number of
bytes received as an offset in the buffer for new messages.
}

num=fwrite(buf, 1, bytes, fid); (write data into file, in unit of
bytes)
fclose(fid);


Regards,
Rob van der Leek
 
M

michelle

Hi, Rob. Your solution is right, that solves the problem in seconds
verse I was trying to debug whole day yesterday. Thank you very
much!!!!

Michelle

Hi, I am trying to transfer a binary file from a client program to the
server, so on the server side I am using recv() to receive the binary
data to the buffer and then write to a file. The follwoing is my
code. Somehow, the newly transfered file is different from the
original file. I don't know if the error is at recv() or fwrite().
For receving, I already check that the bytes being sent equals to the
btyes being received. So anyone knows what's wrong with my code?
Thanks.

char buf[length]; (length is how many bytes of data)
fid=fopen("dataio_testing.bit", "wb"); (open a file to write binary
data)
//this part is right, coz it receive right amount of bytes.
while (bytes != length) {
bytes += recv(clisock, buf, length-bytes, 0);

Depending on the size of the data and the size of the messages, it could
happen that you receive multiple messages. The recv() function returns
the number of bytes received so that adds up nicely to the amount of bytes
you expect, but you store all messages at the start of the buffer
(overwriting older messages with newer ones.) You could use the number of
bytes received as an offset in the buffer for new messages.
}

num=fwrite(buf, 1, bytes, fid); (write data into file, in unit of
bytes)
fclose(fid);


Regards,
Rob van der Leek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top