Re: Receive binary file through recv

phil-news-nospam_at_ipal.net
Date: 06/25/03


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/    |
-----------------------------------------------------------------


Relevant Pages

  • Re: Doubt in Socket Program
    ... > Here i pasted Both Server and client code.. ... > I am sending Three message using multiple sends in same server code. ... > char *b; ... behavior by asking recv to write to where ever it happens to points to. ...
    (comp.lang.c)
  • Re: Segmented Data Over Sockets
    ... Just as others explained, if you sendN bytes from the client, it does not ... mean that a single corresponding recv() on the server will receive N bytes. ... each byte of the local structure on the receiving end, ...
    (microsoft.public.win32.programmer.networks)
  • Re: select(), sending and receiving data
    ... > I'm trying to write a little server and a corresponding client. ... > recv() doesn't work in the program, ... [pjb@thalassa tmp]$ gcc -o a a.c ...
    (comp.unix.programmer)
  • Receive binary file through recv
    ... I am trying to send a binary file from the client to the server. ... in a buffer,, and on the server side, I wonder when I ... Directly recv to file: ...
    (comp.unix.programmer)
  • Problem closing sockets
    ... I have a client and a server, the server executes bind, receives data, and then I closethe file descriptor. ... Everything works fine, but when I restart the server it gives me an error in bind, the error description is "Address already in use" or something like that, and it works just fine about a minute after I first close the socket. ...
    (comp.lang.c)