用c++写个连接ftp服务器获取文件的代码
时间: 2024-05-09 12:19:20 浏览: 128
以下是使用C语言连接FTP服务器并获取文件的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment (lib, "ws2_32.lib")
int main(int argc, char *argv[]) {
if (argc < 5) {
printf("Usage: %s <ftp_server> <ftp_username> <ftp_password> <remote_file_path>\n", argv[0]);
return 1;
}
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
printf("WSAStartup failed with error: %d\n", WSAGetLastError());
return 1;
}
SOCKET controlSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (controlSocket == INVALID_SOCKET) {
printf("socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(21);
serverAddress.sin_addr.s_addr = inet_addr(argv[1]);
if (connect(controlSocket, (SOCKADDR*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
printf("connect failed with error: %d\n", WSAGetLastError());
closesocket(controlSocket);
WSACleanup();
return 1;
}
char buffer[2048];
int bytesReceived = recv(controlSocket, buffer, sizeof(buffer), 0);
buffer[bytesReceived] = '\0';
printf("%s", buffer);
char username[100], password[100];
sprintf(username, "USER %s\r\n", argv[2]);
sprintf(password, "PASS %s\r\n", argv[3]);
if (send(controlSocket, username, strlen(username), 0) == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(controlSocket);
WSACleanup();
return 1;
}
bytesReceived = recv(controlSocket, buffer, sizeof(buffer), 0);
buffer[bytesReceived] = '\0';
printf("%s", buffer);
if (send(controlSocket, password, strlen(password), 0) == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(controlSocket);
WSACleanup();
return 1;
}
bytesReceived = recv(controlSocket, buffer, sizeof(buffer), 0);
buffer[bytesReceived] = '\0';
printf("%s", buffer);
char pasvCommand[] = "PASV\r\n";
if (send(controlSocket, pasvCommand, strlen(pasvCommand), 0) == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(controlSocket);
WSACleanup();
return 1;
}
bytesReceived = recv(controlSocket, buffer, sizeof(buffer), 0);
buffer[bytesReceived] = '\0';
printf("%s", buffer);
int ip1, ip2, ip3, ip4, port1, port2;
sscanf(buffer, "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n", &ip1, &ip2, &ip3, &ip4, &port1, &port2);
struct sockaddr_in dataAddress;
dataAddress.sin_family = AF_INET;
char dataIp[20];
sprintf(dataIp, "%d.%d.%d.%d", ip1, ip2, ip3, ip4);
dataAddress.sin_addr.s_addr = inet_addr(dataIp);
dataAddress.sin_port = htons((port1 << 8) | port2);
SOCKET dataSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (dataSocket == INVALID_SOCKET) {
printf("socket failed with error: %d\n", WSAGetLastError());
closesocket(controlSocket);
WSACleanup();
return 1;
}
if (connect(dataSocket, (SOCKADDR*)&dataAddress, sizeof(dataAddress)) == SOCKET_ERROR) {
printf("connect failed with error: %d\n", WSAGetLastError());
closesocket(dataSocket);
closesocket(controlSocket);
WSACleanup();
return 1;
}
char retrCommand[100];
sprintf(retrCommand, "RETR %s\r\n", argv[4]);
if (send(controlSocket, retrCommand, strlen(retrCommand), 0) == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(dataSocket);
closesocket(controlSocket);
WSACleanup();
return 1;
}
bytesReceived = recv(controlSocket, buffer, sizeof(buffer), 0);
buffer[bytesReceived] = '\0';
printf("%s", buffer);
FILE *fp = fopen(argv[4], "wb");
if (fp == NULL) {
printf("Unable to open file '%s'\n", argv[4]);
closesocket(dataSocket);
closesocket(controlSocket);
WSACleanup();
return 1;
}
while (1) {
bytesReceived = recv(dataSocket, buffer, sizeof(buffer), 0);
if (bytesReceived == 0) {
break;
}
fwrite(buffer, sizeof(char), bytesReceived, fp);
}
fclose(fp);
closesocket(dataSocket);
closesocket(controlSocket);
WSACleanup();
printf("File '%s' downloaded successfully\n", argv[4]);
return 0;
}
```
请注意,此代码仅适用于Windows操作系统,因为它使用了Windows特定的API。如果您想在其他操作系统上运行此代码,您需要进行相应的修改。
阅读全文