FTP中的RETR命令,服务器如何向客户端发送文件?
我正在实现一个非常简单的 FTP 服务器程序,它能够检索和存储文本文件。我的问题是,当 ftp 客户端请求一个包含以下“RETR test.txt”的文件时,服务器如何发送该文件?它是否打开该文本文件并将内容复制到缓冲区中并简单地发送它还是还有更多内容?我不确定如何实现这一点,有人可以澄清基本想法吗?
edit::
if (strncmp(receive_buffer,"RETR",4)==0) {
sprintf(send_buffer,"150 Opening ASCII mode data connection... \r\n");
printf("<< DEBUG INFO. >>: REPLY sent to CLIENT: %s\n", send_buffer);
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
if (bytes < 0) break;
closesocket(ns);
char temp_buffer2[80];
FILE *fin=fopen("test.txt","r");//open test.txt
while (!feof(fin)){
fgets(temp_buffer2,78,fin);
sprintf(send_buffer,"%s",temp_buffer2);
printf("%s",send_buffer);
if (active==0) {
printf("***active is 0");
send(ns_data, send_buffer, strlen(send_buffer), 0);
}
else {
printf("***active is 1+");
send(s_data_act, send_buffer, strlen(send_buffer), 0);
}
}
fclose(fin);
sprintf(send_buffer,"226 File transfer complete. \r\n");
printf("<< DEBUG INFO. >>: REPLY sent to CLIENT: %s\n", send_buffer);
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
if (active==0 )closesocket(ns_data);
else closesocket(s_data_act);
}
这就是我所做的,对于 RETR 命令,它会打开 test.txt 来尝试发送它。但这会导致服务器断开连接..
I'm implementing a very simple FTP server program that is able to retrieve and store text files. my question is, when a ftp client request say a file with the following "RETR test.txt" how does the server send this file?. Does it open that text file copy the contents into a buffer and simply send it or is there more to it?. I'm not sure how to implement this, could someone clarify the basic idea?.
edit::
if (strncmp(receive_buffer,"RETR",4)==0) {
sprintf(send_buffer,"150 Opening ASCII mode data connection... \r\n");
printf("<< DEBUG INFO. >>: REPLY sent to CLIENT: %s\n", send_buffer);
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
if (bytes < 0) break;
closesocket(ns);
char temp_buffer2[80];
FILE *fin=fopen("test.txt","r");//open test.txt
while (!feof(fin)){
fgets(temp_buffer2,78,fin);
sprintf(send_buffer,"%s",temp_buffer2);
printf("%s",send_buffer);
if (active==0) {
printf("***active is 0");
send(ns_data, send_buffer, strlen(send_buffer), 0);
}
else {
printf("***active is 1+");
send(s_data_act, send_buffer, strlen(send_buffer), 0);
}
}
fclose(fin);
sprintf(send_buffer,"226 File transfer complete. \r\n");
printf("<< DEBUG INFO. >>: REPLY sent to CLIENT: %s\n", send_buffer);
bytes = send(ns, send_buffer, strlen(send_buffer), 0);
if (active==0 )closesocket(ns_data);
else closesocket(s_data_act);
}
this is what I have done, and for the RETR command it opens test.txt to try and send it. But this causes the server to disconnect..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你如何发送并不重要。您可以一次读取一个字节,并为每个字节调用
Send
。或者您可以创建一个 8192 字节的缓冲区并一次发送这么多字节。或任何其他字节数。重要的是 FTP 服务器处于主动模式还是被动模式: http://slacksite.com/other/ ftp.html
It doesnt matter how you send it. You can read one byte at a time and call
Send
for each byte. Or you could create a buffer of 8192 bytes and send that much at a time. Or any other number of bytes.What do matter is if the FTP server is in active or passive mode: http://slacksite.com/other/ftp.html
代码中的一个错误是,您在发送初始回复后、通过数据连接发送文件之前调用
closesocket(ns)
来断开客户端的命令连接。One bug in your code is that you are calling
closesocket(ns)
to disconnect the client's command connection after sending the intial reply, before sending the file over the data connection.