Re: Receive binary file through recv
phil-news-nospam_at_ipal.net
Date: 06/25/03
- Next message: llewelly: "Re: Switching make from Sun's cc to GNU's gcc ..."
- Previous message: michelle: "Receive binary file through recv"
- In reply to: michelle: "Receive binary file through recv"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: 24 Jun 2003 23:37:43 GMT
On 24 Jun 2003 15:19:11 -0700 michelle <theintangible1@hotmail.com> wrote:
| Hi, I am trying to send a binary file from the client to the server.
| On the client side, I have the file that I want to send to be stored
| in a buffer, (array of bytes), and on the server side, I wonder when I
| receive it, instead of allocate a buffer could I create a file and set
| the parameter ptr in recv() to the file descriptor of the newly
| created file? But either way it didn't work, and my code is
| following, can someone tell me what I did wrong? Thanks!
|
| Directly recv to file:
| fid=fopen("testing", "w");
| bytes=recv(clisock, fid, length, 0);
That's not what recv() does. The second argument must be a memory
location pointer. There is no way for it to know you have provided
a file descriptor because all that is really passed to the function
is a bit pattern containing the pointer. If the file descriptor is
number 4, it would just think you meant to put the data bytes into
memory location 4 (on most systems).
Some systems have a function called sendfile() which might serve
your needs. But you need to thoroughly understand how it works to
use it properly and that's something you didn't do with recv().
| Recv to buffer then write to file:
| char *buf=(char *)malloc(length*sizeof(char); (length is how many
| bytes of data)
| fid=fopen("testing", "w");
| bytes=recv(clisock, buf, length, 0);
| fwrite(buf, 1, length, fid); (the data is in unit of bytes)
| fclose(fid);
And why are you trying to avoid doing this?
-- ----------------------------------------------------------------- | Phil Howard - KA9WGN | Dallas | http://linuxhomepage.com/ | | phil-nospam@ipal.net | Texas, USA | http://ka9wgn.ham.org/ | -----------------------------------------------------------------
- Next message: llewelly: "Re: Switching make from Sun's cc to GNU's gcc ..."
- Previous message: michelle: "Receive binary file through recv"
- In reply to: michelle: "Receive binary file through recv"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|