编写 ftp 客户端程序来列出服务器上的文件的最佳方法?
我正在尝试在 Windows 中用 C 语言编写客户端-服务器程序。目的是从服务器接收目录列表。现在我正在尝试以利用大多数资源的方式开发客户端-服务器。
一种实现方法是服务器进行一次 send() 调用来发送单个文件的信息。因此,如果有 100 个文件,则会进行 100 次调用。但我觉得这是浪费网络资源。据我所知,windows中send()或recv()的缓冲区大小是8kb。但单个文件的信息不会超过1kb。那么有没有办法让 send() 调用发送多个文件信息(文件信息存储在结构中。所以它们基本上形成一个链接列表)?也许我可以在一次 Send() 调用中发送至少 8 个文件的信息。这应该将 send() 调用总数减少到最大 13 次。
那么基本上有没有一种方法可以通过 send() 发送链接列表?如果您能想到任何替代方法,请告诉我。
I am trying to write a client-server program in C in windows. The objective is to receive the directory listing from the server. Now I was trying to develop the client-server in such a way to utilize most resources.
One way to implement is that server makes a single send() call to send info of a single file. So if there are 100 files, it makes 100 calls. But I feel its a wastage of network resources. As far as I know the buffer size for send() or recv() in windows is 8kb. But the info of a single file will be hardly 1kb. So is there a way to make send() call to send multiple files info (file info are stored in structures. So they basically form a linked list) ? May be I can send info of atleast 8 files in a single Send() call. That should reduce the total send() calls to maximum 13.
So basically is there a way to send a linked list via send() ?? Plz let me know if you can think of any alternative method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好问题!为此+1。
但是您真的想要或需要编写代码来使用 Winsock 吗?这样做有充分的理由——包括它既有趣又充满挑战。但如果您不需要,您可能需要考虑使用 libcurl ftp库,它是免费的、多平台的(当然包括win32),可以正常工作,并且可能会让您的工作变得更加轻松。
Good question! +1 for that.
But do you really want or need to write your code to use Winsock? There are good reasons to do so -- including that it's fun and a challenge. But if you don't need to, you might want to consider using the libcurl ftp library, which is free, multi-platform (including win32, of course), just works, and might make your job a lot easier.
据我所知,使用 FTP 执行此操作的唯一方法是使用与 FTP 服务器的多个连接。如果服务器允许这样做,则可以提高列表性能,因为列出完整文件夹树所需的许多协议交换可以并行运行。
平均值,
马丁
The only way I know of to do this with FTP is to use multiple connections to the FTP server. If this is allowed by the server, there can be a list performance boost because the many protocol exchanges needed to list a complete folder tree can be run in parallel.
Rgds,
Martin
TCP是字节流。不保证您要发送的项目数与
send()
(或recv()
)调用次数之间存在 1 对 1 的关系你需要做。 TCP 根本不是这样工作的。您按照需要的方式格式化数据,然后继续调用send()
直到它告诉您所有数据已发送。关于FTP,请阅读RFC 959和RFC 959 ietf.org/rfc/rfc3659.txt" rel="nofollow">RFC 3659 了解 ftp 协议的实际工作原理。在引入
MLST
和MLSD
命令之前,目录列表没有标准化格式。 FTP 服务器可以自由使用他们想要的任何格式。许多服务器只是通过操作系统自己的dir
或list
命令传输原始数据。例如,Indy 在其 FTP 客户端中包含数十个解析器,用于处理非标准目录列表。TCP is a byte stream. There is no guarantee of a 1-to-1 relation between the number of items you want to send and the number of calls to
send()
(orrecv()
) you need to make. That is simply not how TCP works. You format the data the way you need to, and then you keep callingsend()
until it tells you that all of the data has been sent.Regarding FTP, please read RFC 959 and RFC 3659 to learn how the ftp protocol actually works. Before the introduction of the
MLST
andMLSD
commands, directory listings had no standardized format. FTP servers were free to use whatever formatting they wanted. Many servers just piped the raw data from the OS's owndir
orlist
commands. Indy, for example, includes several dozen parsers in its FTP client for handling non-standard directory listings.