Monday, May 24, 2010

I need help - file transfer in binary mode in C programming.?

This is a college level question so plz dont answer if you dont know C programming very well. I need to implement transferring files between a client and a server in C. The transfer has to be in binary mode and the entire file has to transferred in chunks or blocks. I am not talking about send() -ing a char string over a socket. I need to send and receive actual files over a tcp socket. If anyone knows how to get this done it would be really helpful to me.


I need some command for reading a file, not just a text files contents.


If anyone answers, thnx beforehand.

I need help - file transfer in binary mode in C programming.?
You have your work cut out for you. If you have a program that can send a string over a socket, then you can adapt that to your task. You will need some controls to set up the transfer. Send a header that contains the number of blocks. Then a trailer when it is completed to verify.


Calculate a CRC on the file before you send it. Let the remote side recalculate the CRC to ensure that the file is right.


The client side flow would be something like this:


Command line params: ip address, port filename


open socket


open file


send header with name of file


send blocks until whole file is sent


(calculate CRC on file as you go.)


send trailer that includes CRC.





Server side:


start up and listen on socket


accept connection


if read header initiate file, open local file in temp location.


if read block, ensure in reading file status (else error)


if read trailer ensure reading file status. check CRC. close file. Put file where it needs to go.
Reply:For file reading, look at fopen() and fread(). The mode parameter allows binary reading. use fopen(path_name, "rb");. fread() allows you to read in whatever size blocks you want.





Socket programming is complex, but after you setup the socket connection the socket is a binary stream. The header (for Windows) is winsock2.h. I'm not going into the details of socket programming. I'm not experienced and doing it in C is a big job.





The bit I understand is that the server side creates a socket with the socket() function. It calls bind() to bind it to an address. Then it calls listen to wait for a client connection. When it receives a connection it calls accept().





The client side creates the socket with socket() and then calls connect(). Either side can then call send() and recv() as necessary.





Still, once you have read a block from the file, call send() whichi will send the data as binary. The other end will call recv(), which collects the data as binary.





The details of sizes and buffering and all that are up to you.





If you are doing the control and data send all on a single connection, you will need some kind of protocol to distinguish control commands (such as file name) from actual file data.
Reply:First of all, you cant just call an operating system command to copy the file to a different path. Its the simplest way to do what you want and in practice you always use the simplest method.





If you have to send the data in little blocks, well, sending a block of data is exactly the same as sending a char string. A 64 character string is just 64 bytes of data exactly the same as a 64 byte block.





The only difference i can see is that you might start by send a piece of data describeing the file you are sending. File size, file name, intended path, block size, that sort of thing.


No comments:

Post a Comment