transfering a file over TCP

本文讨论了通过TCP连接进行文件传输的基本方法及注意事项,包括使用缓冲区读取文件、添加EOF协议、错误检测算法的应用等。此外,还提到了现有的一些文件传输协议如FTP以及Linux系统下的sendfile函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question:
  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?

Answer:
   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?
   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.


 

转载于:https://www.cnblogs.com/JeffChen/archive/2006/09/22/511687.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值