LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-15-2006, 09:10 AM   #1
JohnDaVinci
LQ Newbie
 
Registered: Jul 2006
Posts: 9

Rep: Reputation: 0
Talking 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
 
Old 09-15-2006, 09:50 AM   #2
abcdefghij
LQ Newbie
 
Registered: Nov 2003
Location: Germany
Distribution: Debian
Posts: 26

Rep: Reputation: 15
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).
 
Old 09-15-2006, 11:44 AM   #3
JohnDaVinci
LQ Newbie
 
Registered: Jul 2006
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 09-15-2006, 12:03 PM   #4
abcdefghij
LQ Newbie
 
Registered: Nov 2003
Location: Germany
Distribution: Debian
Posts: 26

Rep: Reputation: 15
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
 
Old 09-15-2006, 05:32 PM   #5
wwwdev
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
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?
 
Old 09-15-2006, 08:58 PM   #6
pankaj99
Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 47

Rep: Reputation: 15
--mistake--

Last edited by pankaj99; 09-15-2006 at 09:03 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bad file descriptor on PEEK of a connected socket DavidHB Programming 3 08-23-2006 04:57 PM
my modem is connected but no bytes send or recieve in RHEL Atifkhan Red Hat 2 06-09-2006 09:37 PM
I have no send/receive when connected to internet zeusys Linux - Networking 1 06-03-2006 10:38 AM
RT2500 connected but no send/receive mproberts Linux - Laptop and Netbook 3 02-17-2005 09:31 AM
Dante client problem: control-socket: Transport endpoint is not connected phonic Linux - Networking 1 07-14-2004 04:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration