我不确定有关 ftp 文件传输的一些事情。我正在编写一个 ftp 服务器,并试图弄清楚如何使文件传输正常工作。到目前为止,它以某种方式起作用,但我有一些疑问。这是我的文件传输功能(目前只检索):
void RETRCommand(int & clie_sock, int & c_data_sock, char buffer[]){
ifstream file; //clie_sock is used for commands and c_data_sock for data transfer
char *file_name, packet[PACKET_SIZE]; //packet size is 2040
int packet_len, pre_pos = 0, file_end;
file_name = new char[strlen(buffer + 5)];
strcpy(file_name, buffer + 5);
sprintf(buffer, "150 Opening BINARY mode data connection for file transfer\r\n");
if (send(clie_sock, buffer, strlen(buffer), 0) == -1) {
perror("Error while writing ");
close(clie_sock);
exit(1);
}
cout << "sent: " << buffer << endl;
file_name[strlen(file_name) - 2] = '\0';
file.open(file_name, ios::in | ios::binary);
if (file.is_open()) {
file.seekg(0, file.end);
file_end = (int) file.tellg();
file.seekg(0, file.beg);
while(file.good()){
pre_pos = file.tellg();
file.read(packet, PACKET_SIZE);
if ((int) file.tellg() == -1)
packet_len = file_end - pre_pos;
else
packet_len = PACKET_SIZE;
if (send(c_data_sock, packet, packet_len, 0) == -1) {
perror("Error while writing ");
close(clie_sock);
exit(1);
}
cout << "sent some data" << endl;
}
}
else {
sprintf(buffer, "550 Requested action not taken. File unavailable\r\n", packet);
if (send(clie_sock, buffer, packet_len + 2, 0) == -1) {
perror("Error while writing ");
close(clie_sock);
exit(1);
}
cout << "sent: " << buffer << endl;
delete(file_name);
return;
}
sprintf(buffer, "226 Transfer complete\r\n");
if (send(clie_sock, buffer, strlen(buffer), 0) == -1) {
perror("Error while writing ");
close(clie_sock);
exit(1);
}
cout << "sent: " << buffer << endl;
close(c_data_sock);
delete(file_name);
}
所以一个问题是数据传输本身。我不确定它应该如何工作。现在它是这样工作的:服务器将所有数据发送到 c_data_sock,关闭这个套接字,然后客户端开始做一些事情。客户端不应该在服务器发送数据时接收数据吗?另一个问题是 abor 命令。我应该如何接收 abor 命令?我尝试了将标志设置为 MSG_OOB 的 recv,但随后我收到一条错误消息,提示“参数无效”。如果有人能给我一个提示或一个如何正确做的例子,我会很高兴,因为我自己似乎无法弄清楚。
谢谢,
约翰