ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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).
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.
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).
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.