用C语言实现利用TCP协议编写一个简单的文件服务器和客户端。 支持功能: 1. list:列表远端目录下的文件和目录信息(目录用方括号括起以示区分) 2. pwd: 查看远端当前目录 3. lpwd: 查看本地当前目录 4. cd xxxxx: 切换远端当前目录为xxxxx 5. lcd xxxxx: 切换本地当前目录为xxxxx 6. down xxxxx: 下载远端当前目录下的文件xxxxx到客户端的当前目录下 7. exit:断开连接并退出。
时间: 2023-06-27 19:07:10 浏览: 184
FTP服务器源代码C语言.rar_FTP服务器_FTP服务器端源代码_c语言ftp服务器_ftp源代码_合作开发 FTP
以下是基于C语言实现的简单文件服务器和客户端的代码,支持上述功能:
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#define PORT 9090
#define BUFFER_SIZE 1024
#define MAX_COMMAND_LENGTH 100
#define MAX_PATH_LENGTH 200
void list_files(int client_socket, char *current_directory);
void change_directory(int client_socket, char *current_directory, char *new_directory);
void download_file(int client_socket, char *current_directory, char *filename);
int main()
{
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t client_address_size;
char buffer[BUFFER_SIZE];
char current_directory[MAX_PATH_LENGTH];
char command[MAX_COMMAND_LENGTH];
// create server socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if(server_socket == -1)
{
perror("Failed to create server socket");
exit(EXIT_FAILURE);
}
// set server address
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(PORT);
// bind server socket to address
if(bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) == -1)
{
perror("Failed to bind server socket");
exit(EXIT_FAILURE);
}
// listen for client connections
if(listen(server_socket, 5) == -1)
{
perror("Failed to listen for connections");
exit(EXIT_FAILURE);
}
printf("Server started and listening on port %d...\n", PORT);
// main server loop
while(1)
{
client_address_size = sizeof(client_address);
// accept client connection
client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_size);
if(client_socket == -1)
{
perror("Failed to accept client connection");
continue;
}
printf("Client connected from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
// send welcome message to client
strcpy(buffer, "Welcome to the file server!\n");
send(client_socket, buffer, strlen(buffer), 0);
// set initial current directory to server's working directory
getcwd(current_directory, MAX_PATH_LENGTH);
// main client loop
while(1)
{
// receive command from client
memset(buffer, 0, BUFFER_SIZE);
recv(client_socket, buffer, BUFFER_SIZE, 0);
sscanf(buffer, "%s", command);
// handle command
if(strcmp(command, "list") == 0)
{
list_files(client_socket, current_directory);
}
else if(strcmp(command, "pwd") == 0)
{
sprintf(buffer, "%s\n", current_directory);
send(client_socket, buffer, strlen(buffer), 0);
}
else if(strcmp(command, "cd") == 0)
{
char new_directory[MAX_PATH_LENGTH];
sscanf(buffer, "%*s %s", new_directory);
change_directory(client_socket, current_directory, new_directory);
}
else if(strcmp(command, "down") == 0)
{
char filename[MAX_PATH_LENGTH];
sscanf(buffer, "%*s %s", filename);
download_file(client_socket, current_directory, filename);
}
else if(strcmp(command, "exit") == 0)
{
printf("Client disconnected from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
close(client_socket);
break;
}
else
{
strcpy(buffer, "Invalid command\n");
send(client_socket, buffer, strlen(buffer), 0);
}
}
}
return 0;
}
void list_files(int client_socket, char *current_directory)
{
DIR *dir;
struct dirent *entry;
char buffer[BUFFER_SIZE];
dir = opendir(current_directory);
if(dir == NULL)
{
perror("Failed to open directory");
return;
}
while((entry = readdir(dir)) != NULL)
{
if(entry->d_type == DT_DIR)
{
sprintf(buffer, "[%s]\n", entry->d_name);
}
else
{
sprintf(buffer, "%s\n", entry->d_name);
}
send(client_socket, buffer, strlen(buffer), 0);
}
closedir(dir);
}
void change_directory(int client_socket, char *current_directory, char *new_directory)
{
char buffer[BUFFER_SIZE];
if(chdir(new_directory) == -1)
{
perror("Failed to change directory");
strcpy(buffer, "Failed to change directory\n");
send(client_socket, buffer, strlen(buffer), 0);
}
else
{
getcwd(current_directory, MAX_PATH_LENGTH);
sprintf(buffer, "Changed directory to %s\n", current_directory);
send(client_socket, buffer, strlen(buffer), 0);
}
}
void download_file(int client_socket, char *current_directory, char *filename)
{
char buffer[BUFFER_SIZE];
char filepath[MAX_PATH_LENGTH];
int file_descriptor, bytes_read;
// create file path
sprintf(filepath, "%s/%s", current_directory, filename);
// open file for reading
file_descriptor = open(filepath, O_RDONLY);
if(file_descriptor == -1)
{
perror("Failed to open file for reading");
strcpy(buffer, "Failed to open file for reading\n");
send(client_socket, buffer, strlen(buffer), 0);
return;
}
// send file contents to client
while((bytes_read = read(file_descriptor, buffer, BUFFER_SIZE)) > 0)
{
send(client_socket, buffer, bytes_read, 0);
}
close(file_descriptor);
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_IP "127.0.0.1"
#define PORT 9090
#define BUFFER_SIZE 1024
#define MAX_COMMAND_LENGTH 100
#define MAX_PATH_LENGTH 200
int main()
{
int client_socket;
struct sockaddr_in server_address;
char buffer[BUFFER_SIZE];
char command[MAX_COMMAND_LENGTH];
char current_directory[MAX_PATH_LENGTH];
// create client socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if(client_socket == -1)
{
perror("Failed to create client socket");
exit(EXIT_FAILURE);
}
// set server address
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(SERVER_IP);
server_address.sin_port = htons(PORT);
// connect to server
if(connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) == -1)
{
perror("Failed to connect to server");
exit(EXIT_FAILURE);
}
// receive welcome message from server
memset(buffer, 0, BUFFER_SIZE);
recv(client_socket, buffer, BUFFER_SIZE, 0);
printf("%s", buffer);
// set initial current directory to client's working directory
getcwd(current_directory, MAX_PATH_LENGTH);
// main client loop
while(1)
{
// get user command
printf("> ");
fgets(command, MAX_COMMAND_LENGTH, stdin);
command[strlen(command)-1] = '\0'; // remove newline character
// send command to server
send(client_socket, command, strlen(command), 0);
// handle command
if(strcmp(command, "list") == 0)
{
// receive file list from server
memset(buffer, 0, BUFFER_SIZE);
while(recv(client_socket, buffer, BUFFER_SIZE, 0) > 0)
{
printf("%s", buffer);
memset(buffer, 0, BUFFER_SIZE);
}
}
else if(strcmp(command, "pwd") == 0)
{
// receive current directory from server
memset(buffer, 0, BUFFER_SIZE);
recv(client_socket, buffer, BUFFER_SIZE, 0);
printf("%s", buffer);
}
else if(strcmp(command, "cd") == 0)
{
// receive change directory result from server
memset(buffer, 0, BUFFER_SIZE);
recv(client_socket, buffer, BUFFER_SIZE, 0);
printf("%s", buffer);
}
else if(strcmp(command, "down") == 0)
{
char filename[MAX_PATH_LENGTH];
FILE *file;
int bytes_received;
// get filename from user
printf("Filename: ");
fgets(filename, MAX_PATH_LENGTH, stdin);
filename[strlen(filename)-1] = '\0'; // remove newline character
// open file for writing
file = fopen(filename, "w");
if(file == NULL)
{
perror("Failed to open file for writing");
continue;
}
// receive file contents from server
while((bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0)
{
fwrite(buffer, 1, bytes_received, file);
}
fclose(file);
printf("File downloaded successfully\n");
}
else if(strcmp(command, "exit") == 0)
{
close(client_socket);
printf("Disconnected from server\n");
break;
}
else
{
printf("Invalid command\n");
}
}
return 0;
}
```
阅读全文