grabbing file size

J

Jesse Engle

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?
 
K

Kutty Banerjee

Jesse Engle said:
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?

hi,
lets not just put it as sending files thru sockets , rather as sending
data thru sockets . split your file and therefore send your data in fixed
size buffers of say size 1024 bytes or (choose). thus you send 1024 and
receive (thereby block) on 1024 bytes. Do this inside a loop while there
still is a packet of size 1024 to send. the last send will comprise less
than 1024. immediately after that, you could close the socket using
shutdown(socket,how) and closesocket(socket). shutdown ensures that your
receive function receives the data last send (man shutdown).

kutty
 
J

Joona I Palaste

hi,
lets not just put it as sending files thru sockets , rather as sending
data thru sockets . split your file and therefore send your data in fixed
size buffers of say size 1024 bytes or (choose). thus you send 1024 and
receive (thereby block) on 1024 bytes. Do this inside a loop while there
still is a packet of size 1024 to send. the last send will comprise less
than 1024. immediately after that, you could close the socket using
shutdown(socket,how) and closesocket(socket). shutdown ensures that your
receive function receives the data last send (man shutdown).

Please discuss POSIX-speficic solutions on comp.unix.programmer or
another POSIX newsgroup, thanks. None of the functions shutdown or
closesocket are ISO standard C.
Oh, and there is no such word as "thru".
 
A

Allan Bruce

Jesse Engle said:
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
 
C

CBFalconer

Joona said:
.... snip ...

Oh, and there is no such word as "thru".

Unfortunately there is. There was an American spelling reform
movement about a century ago, which recommended the use of thru,
tho, and others. These didn't really take.
 
J

Joona I Palaste

CBFalconer said:
Joona I Palaste wrote:
... snip ...
Unfortunately there is. There was an American spelling reform
movement about a century ago, which recommended the use of thru,
tho, and others. These didn't really take.

Aw, crap. To my eyes "thru" looks just as like w4r3z d00dsp33k as "u"
or "plz" (oh, please!) does. It's amazing how some people can write an
accurate technical response spelling difficult technical words of over
5 syllables correctly, but still write "thanks" as "thx" and "please"
as "plz".
English spelling is unusually difficult anyway. Those spelling reforms
should have had real effect, such as that which ended with "...wud
finali hav kum tru".
 

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

Members online

Forum statistics

Threads
474,141
Messages
2,570,816
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top