"Jesse Engle" <je****@elementalsn.com> wrote in message
news:_z******************@newssvr33.news.prodigy.c om...
i'm working on two client and server programs that send and recieve files
using sockets. i saw a c++ example using csockets, and thought i could use
the basic idea of what the code was doing to form my own c program.
i have an idea of how to send binary files via sockets, but the problem is
that to use send() i need to know the size (in bytes) of the file i want
to send.
any ideas on how to accomplish this?
I would not send the file in completion at once, instead break the message
up. Note, sockets are off topic here. But here is a general way of finding
the size of a file, however it is not fully portable IIRC.
// open file and get size
int lFileSize;
FILE *lfp;
if ( (lfp=fopen(mFileName, "rb")) == NULL)
{
// error opening
return;
}
fseek(lfp, 0, SEEK_END);
lFileSize = ftell(lfp); // cant use int if more than 2GB
fseek(lfp, 0, SEEK_SET); // back to start of file
once you then come to send the file, you can do something like
#define BUFF_SIZE 1024
char lBuf[BUFF_SIZE];
int lBuffDataLength;
int lSent=0;
while(lSent < lFileSize)
{
// set the number of bytes valid in the buffer
lBuffDataLength = BUFF_SIZE;
if (lFileSize - lSent < BUFF_SIZE)
{
// if there is not enough data left to fill buffer
// then set the length of data left to buffer
lBuffDataLength = lFileSize-lSent;
}
// read a chunk of the file
fread(lBuf, lBuffDataLength, 1, lfp);
send();// this part is implementation-specific
}
<OT>
Note if you are using windows sockets, send() will send UP TO the length you
specify. To guarantee sending you will need to implement your own send()
function wrapper.
</OT>
HTH
Allan