LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Send a file over a connected socket? (https://www.linuxquestions.org/questions/programming-9/send-a-file-over-a-connected-socket-483749/)

JohnDaVinci 09-15-2006 09:10 AM

Send a file over a connected socket?
 
I have a C program which connects with sockets (AF_INET).

What I wondering is, because the socket behaves like a file after it's been set up, where you can fread and fwrite etc., is there any easy way to transfer a file to the server or the client? Is there something like an fsend or anything which can be easily used to send the file?

One way I could do it, but it seems very round-about and time is of the essense, is to load the file up, send it with an fwrite/fread, and then put it back into a file, but that just seems like worthless processing time, when all I really want to do is transfer the whole file.

Thanks,
John DaVinci

abcdefghij 09-15-2006 09:50 AM

Not sure if this is what you're looking for, but there's a sendfile command available:
http://www.die.net/doc/linux/man/man2/sendfile.2.html

Apparently it is only there for performance reasons (manually reading the file and writing it to disk should give the same result, only slower); and it seems to have the drawback that it's not really portable.

On the receiving side, you will probably still have to manually write the received data to a file. And of course the sendfile call only transfers the file contents, not the name or permissions or whatever (if you want to transfer that as well, you probably have to write your own protocol or use an existing protocol which supports that).

JohnDaVinci 09-15-2006 11:44 AM

What I'm seeing if I try that is that my server which is receiving thinks it received something, but when I display it nothing has been added to the structure.

My client (sender) is doing this:

sendfile(FileFD, SockFD, 0, sizeof(SOMEStruct));

My server (reader) is doing this:

read(client_sockfd, &(SOMEStruct), sizeof(SOMEStruct);

The SOMEStruct is a structure was stored directly into the FileFD file.

Any clue why sendfile isn't working?

I can do the same thing all manually, but I would like to streamline it to reduce the overhead because the project this is for is based on getting data moved around fast (data is only useful for a short amount of time).

Thanks,
Jon DaVinci

abcdefghij 09-15-2006 12:03 PM

Looking at the manpage I would say you have to call sendfile like this:

off_t offset = 0;
sendfile(FileFD, SockFD, &offset, sizeof(SOMEStruct));

Also, what does sendfile return to your program? Does it give you an error? Has the read call received as many bytes as expected? Does the whole thing work if you use read/write instead of sendfile (ie. are you sure the problem is in the sender application)?

A comment apart from the sendfile stuff: you probably know this, but storing or transferring structs might lead to problems as soon as you transfer them to another machine or even to a program compiled with another compiler version... A real serialization solution might be more reliable (but also slower - so I assume you know about the dangers and accept them for performance reasons :)

wwwdev 09-15-2006 05:32 PM

Hi, JohnDaVinci

Quote:

Originally Posted by JohnDaVinci
My client (sender) is doing this:

sendfile(FileFD, SockFD, 0, sizeof(SOMEStruct));

ssize_t sendfile(int out_fd, int in_fd, off_t *offset, ssize_t count);

sendfile() send data from in_fd to out_fd. Do you send from SockFD to FileFD ??

Quote:

My server (reader) is doing this:

read(client_sockfd, &(SOMEStruct), sizeof(SOMEStruct);

The SOMEStruct is a structure was stored directly into the FileFD file.

Any clue why sendfile isn't working?

pankaj99 09-15-2006 08:58 PM

--mistake--


All times are GMT -5. The time now is 02:40 PM.