For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2006 > transfering a file over TCP









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author transfering a file over TCP
shilpa.saraogi@gmail.com

2006-09-19, 7:02 pm

Hello all,

I am a nertwork programming newbie. I am trying to write a
client-server pair such that the client should be able to transfer a
file over TCP connection to the server.

The only way I know of, is to open the file, read into buffer and write
into the socket. Is there any function call to just transfer the file?

Thank you
Shilpa

Mikko Rauhala

2006-09-19, 7:02 pm

On 19 Sep 2006 09:08:44 -0700, shilpa.saraogi@gmail.com
<shilpa.saraogi@gmail.com> wrote:
> The only way I know of, is to open the file, read into buffer and write
> into the socket. Is there any function call to just transfer the file?


Not portably without some utility library or another. Some systems might
have calls like sendfile which are designed for this purpose, however
you should not use them in portable programs (or at least you should
fall back at compile time to a read/write solution).

--
Mikko Rauhala - mjr@iki.fi - <URL:http://www.iki.fi/mjr/>
Transhumanist - WTA member - <URL:http://www.transhumanism.org/>
Singularitarian - SIAI supporter - <URL:http://www.singinst.org/>

Pascal Bourguignon

2006-09-19, 7:02 pm

"shilpa.saraogi@gmail.com" <shilpa.saraogi@gmail.com> writes:

> Hello all,
>
> I am a nertwork programming newbie. I am trying to write a
> client-server pair such that the client should be able to transfer a
> file over TCP connection to the server.
>
> The only way I know of, is to open the file, read into buffer and write
> into the socket.


This is basically what you must do.

But you should probably add a EOF protocol (eg: write the file size
before sending the data). You may also want to transfer several files
(eg sending the file directory/name and perhaps other attributes such
as owner, access rights, etc).

Now, TCP corrects some errors, but it normally computes a 2-byte check
sum on each packet, so the probability to have a undetected bad packet
is still rather high. And the more so when you transfer long files.

So, I would add a more sophisticated error detection algorithm
(eg. doing md5sums on each 100 KB or 1 MB blocks).


Often, you want to get the directory listing of the remote side before
sending or receiving a file. So you need to add some protocol to send
such commands and transfer directory "files".

Sometimes you don't want the file data to be sent in clear over TCP
either. Anybody on the network could see it... So you may want to
encrypt the data, using ssl perhaps?


Are you aware that there is already this FTP protocol (and some other
ISO file transfer protocols), and implementations, in addition to scp
and sftp?



> Is there any function call to just transfer the file?


In Linux, there's sendfile(2). There's no recvfile though.

Other Unixes often implement sendfile with different semantics
and prototypes. It should not be used in portable programs.

--
__Pascal Bourguignon__ http://www.informatimago.com/

CONSUMER NOTICE: Because of the "uncertainty principle," it is
impossible for the consumer to simultaneously know both the precise
location and velocity of this product.
Mark Rafn

2006-09-19, 7:02 pm

shilpa.saraogi@gmail.com <shilpa.saraogi@gmail.com> wrote:
>I am a nertwork programming newbie. I am trying to write a
>client-server pair such that the client should be able to transfer a
>file over TCP connection to the server.


Why? If this is just a learning excercise, fine. If it's for production use,
there are LOTS of existing ways to do this, and you should almost certainly
reuse one.

>The only way I know of, is to open the file, read into buffer and write
>into the socket.


Yup. And probably with a protocol for transferring metadata (filename, size,
permissions?), along with partial transfers or multiple file handling.

>Is there any function call to just transfer the file?


system("scp filename remote:remotefilename);
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Nils O. Selåsdal

2006-09-20, 4:00 am

shilpa.saraogi@gmail.com wrote:
> Hello all,
>
> I am a nertwork programming newbie. I am trying to write a
> client-server pair such that the client should be able to transfer a
> file over TCP connection to the server.
>
> The only way I know of, is to open the file, read into buffer and write
> into the socket. Is there any function call to just transfer the file?


That's the usual way, and indeed very simple too.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com