venturasan
04-11-2003, 02:24 PM
Can anyone help me?
I have a question as to how to receive and send binary files across a tcp socket (i am workin in C of course).
i have associated the socket stream with 2 files, one is for sending and one is for receiving.
e.g
receive = fdopen (sock, "rb");
where sock is my socket.
the binary files are transferred in blocks of up to 128 characters.
Each block commences with the size, specified as a single byte, followed by bytes of file data. may be any value in the range 0 to 128 inclusive.
The end of file is indicated by a null (byte value 0, control-@) occurring instead of a data block.
i'm trying to use fread to read my receive file but i'm getting more bytes than i should store in my file.
any ideas? :oops: hope u can understand my code
here's my code:
FILE *tmpf; //file to save data to...
int i, j, ret, size;
int t =0;
char szBuf[128];
char c;
tmpf = fopen(av[q], "wb"); //make new file for writing from command line
// Receive data from the client
j=1;
while (j >0)
{
memset(szBuf, 0, sizeof(szBuf));// clear buffer
ret = fread(szBuf,1, 1, receive); //reading from socket
i =0;
j= ret;
while (ret > i)
{
c = szBuf[i];
i++;
if (c != NULL)
{
putc(c,tmpf);
}
}
I have a question as to how to receive and send binary files across a tcp socket (i am workin in C of course).
i have associated the socket stream with 2 files, one is for sending and one is for receiving.
e.g
receive = fdopen (sock, "rb");
where sock is my socket.
the binary files are transferred in blocks of up to 128 characters.
Each block commences with the size, specified as a single byte, followed by bytes of file data. may be any value in the range 0 to 128 inclusive.
The end of file is indicated by a null (byte value 0, control-@) occurring instead of a data block.
i'm trying to use fread to read my receive file but i'm getting more bytes than i should store in my file.
any ideas? :oops: hope u can understand my code
here's my code:
FILE *tmpf; //file to save data to...
int i, j, ret, size;
int t =0;
char szBuf[128];
char c;
tmpf = fopen(av[q], "wb"); //make new file for writing from command line
// Receive data from the client
j=1;
while (j >0)
{
memset(szBuf, 0, sizeof(szBuf));// clear buffer
ret = fread(szBuf,1, 1, receive); //reading from socket
i =0;
j= ret;
while (ret > i)
{
c = szBuf[i];
i++;
if (c != NULL)
{
putc(c,tmpf);
}
}