要实现一个简单的FTP客户端和服务器,可以使用C语言编写。这是一个基本示例,展示了如何创建一个简单的FTP服务器和客户端。
FTP服务器代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define PORT 21
#define BUFFER_SIZE 1024
void handle_client(int client_socket) {
char buffer[BUFFER_SIZE];
int bytes_received;
send(client_socket, "220 Welcome to the simple FTP serverrn", 39, 0);
while ((bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[bytes_received] = '';
printf("Received: %sn", buffer);
if (strncmp(buffer, "USER", 4) == 0) {
send(client_socket, "331 User name okay, need passwordrn", 35, 0);
} else if (strncmp(buffer, "PASS", 4) == 0) {
send(client_socket, "230 User logged in, proceedrn", 29, 0);
} else if (strncmp(buffer, "QUIT", 4) == 0) {
send(client_socket, "221 Goodbyern", 13, 0);
break;
} else {
send(client_socket, "502 Command not implementedrn", 29, 0);
}
}
close(client_socket);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
close(server_socket);
exit(EXIT_FAILURE);
}
if (listen(server_socket, 1) < 0) {
perror("listen");
close(server_socket);
exit(EXIT_FAILURE);
}
printf("FTP server is listening on port %dn", PORT);
while ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len)) >= 0) {
printf("Client connectedn");
handle_client(client_socket);
}
close(server_socket);
return 0;
}
FTP客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define PORT 21
#define BUFFER_SIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
int bytes_received;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器地址
server_addr.sin_port = htons(PORT);
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
close(client_socket);
exit(EXIT_FAILURE);
}
printf("Connected to the FTP servern");
while ((bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[bytes_received] = '';
printf("Server: %s", buffer);
if (strncmp(buffer, "220", 3) == 0) {
send(client_socket, "USER anonymousrn", 16, 0);
} else if (strncmp(buffer, "331", 3) == 0) {
send(client_socket, "PASS anonymousrn", 16, 0);
} else if (strncmp(buffer, "230", 3) == 0) {
send(client_socket, "QUITrn", 6, 0);
} else if (strncmp(buffer, "221", 3) == 0) {
break;
}
}
close(client_socket);
return 0;
}
编译和运行
-
编译服务器代码:
gcc -o ftp_server ftp_server.c
-
编译客户端代码:
gcc -o ftp_client ftp_client.c
-
运行服务器:
./ftp_server
-
在另一个终端窗口运行客户端:
./ftp_client
上述代码实现了一个非常基础的FTP服务器和客户端。服务器能够处理基本的FTP命令,如USER
、PASS
和QUIT
,客户端则模拟了一个简单的FTP会话。请根据需要进行扩展和改进。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/190823.html