用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 21:06:37 浏览: 290
使用C语言实现服务器/客户端的TCP通信
以下是一个简单的文件服务器和客户端的实现,使用TCP协议。
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#define PORT 8080
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
char current_dir[BUFFER_SIZE];
void list_files(int sock) {
DIR *dir;
struct dirent *ent;
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
dir = opendir(current_dir);
if (dir != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_DIR) {
strcat(buffer, "[");
}
strcat(buffer, ent->d_name);
if (ent->d_type == DT_DIR) {
strcat(buffer, "]");
}
strcat(buffer, "\n");
}
closedir(dir);
} else {
strcpy(buffer, "Error: cannot open directory.\n");
}
send(sock, buffer, strlen(buffer), 0);
}
void get_pwd(int sock) {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
getcwd(buffer, BUFFER_SIZE);
strcat(buffer, "\n");
send(sock, buffer, strlen(buffer), 0);
}
void change_dir(char *directory, int sock) {
int result = chdir(directory);
if (result == 0) {
get_pwd(sock);
} else {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
strcpy(buffer, "Error: cannot change directory.\n");
send(sock, buffer, strlen(buffer), 0);
}
}
void download(char *filename, int sock) {
char path[BUFFER_SIZE];
memset(path, 0, BUFFER_SIZE);
strcat(path, current_dir);
strcat(path, "/");
strcat(path, filename);
int fd = open(path, O_RDONLY);
if (fd == -1) {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
strcpy(buffer, "Error: cannot open file.\n");
send(sock, buffer, strlen(buffer), 0);
return;
}
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
int bytes_read;
while ((bytes_read = read(fd, buffer, BUFFER_SIZE)) != 0) {
send(sock, buffer, bytes_read, 0);
memset(buffer, 0, BUFFER_SIZE);
}
close(fd);
}
int main() {
int server_fd, client_fd, address_len;
struct sockaddr_in server_addr, client_addr;
char buffer[BUFFER_SIZE];
// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// Bind socket to address
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
exit(EXIT_FAILURE);
}
// Listen for connections
if (listen(server_fd, MAX_CLIENTS) == -1) {
perror("Error listening for connections");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", PORT);
while (1) {
// Accept client connection
address_len = sizeof(client_addr);
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &address_len)) == -1) {
perror("Error accepting connection");
continue;
}
printf("New client connected\n");
// Send welcome message
strcpy(buffer, "Welcome to the file server!\n");
send(client_fd, buffer, strlen(buffer), 0);
// Set current directory
memset(current_dir, 0, BUFFER_SIZE);
getcwd(current_dir, BUFFER_SIZE);
while (1) {
// Receive command from client
memset(buffer, 0, BUFFER_SIZE);
int bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (bytes_received == -1) {
perror("Error receiving data");
break;
} else if (bytes_received == 0) {
printf("Client disconnected\n");
break;
}
// Process command
char *command = strtok(buffer, " ");
char *argument = strtok(NULL, " ");
if (strcmp(command, "list") == 0) {
list_files(client_fd);
} else if (strcmp(command, "pwd") == 0) {
get_pwd(client_fd);
} else if (strcmp(command, "cd") == 0) {
if (argument == NULL) {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
strcpy(buffer, "Usage: cd directory\n");
send(client_fd, buffer, strlen(buffer), 0);
continue;
}
change_dir(argument, client_fd);
} else if (strcmp(command, "down") == 0) {
if (argument == NULL) {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
strcpy(buffer, "Usage: down filename\n");
send(client_fd, buffer, strlen(buffer), 0);
continue;
}
download(argument, client_fd);
} else if (strcmp(command, "exit") == 0) {
break;
} else {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
strcpy(buffer, "Invalid command\n");
send(client_fd, buffer, strlen(buffer), 0);
}
}
// Close client connection
close(client_fd);
}
// Close server socket
close(server_fd);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8080
#define BUFFER_SIZE 1024
void print_prompt() {
char cwd[BUFFER_SIZE];
memset(cwd, 0, BUFFER_SIZE);
getcwd(cwd, BUFFER_SIZE);
printf("%s> ", cwd);
fflush(stdout);
}
void handle_list(int sock) {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
int bytes_received;
while ((bytes_received = recv(sock, buffer, BUFFER_SIZE, 0)) != 0) {
printf("%s", buffer);
memset(buffer, 0, BUFFER_SIZE);
}
}
void handle_pwd(int sock) {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
int bytes_received = recv(sock, buffer, BUFFER_SIZE, 0);
if (bytes_received != -1) {
printf("%s", buffer);
}
}
void handle_cd(char *directory, int sock) {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
sprintf(buffer, "cd %s", directory);
send(sock, buffer, strlen(buffer), 0);
int bytes_received = recv(sock, buffer, BUFFER_SIZE, 0);
if (bytes_received != -1) {
printf("%s", buffer);
}
}
void handle_down(char *filename, int sock) {
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
sprintf(buffer, "down %s", filename);
send(sock, buffer, strlen(buffer), 0);
memset(buffer, 0, BUFFER_SIZE);
int fd = open(filename, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
int bytes_received;
while ((bytes_received = recv(sock, buffer, BUFFER_SIZE, 0)) != 0) {
write(fd, buffer, bytes_received);
memset(buffer, 0, BUFFER_SIZE);
}
close(fd);
}
int main() {
int sock;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(PORT);
// Connect to server
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to server");
exit(EXIT_FAILURE);
}
// Receive welcome message
int bytes_received = recv(sock, buffer, BUFFER_SIZE, 0);
if (bytes_received != -1) {
printf("%s", buffer);
}
while (1) {
print_prompt();
// Read command from user
memset(buffer, 0, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strlen(buffer) - 1] = '\0';
// Process command
char *command = strtok(buffer, " ");
char *argument = strtok(NULL, " ");
if (strcmp(command, "list") == 0) {
send(sock, "list", strlen("list"), 0);
handle_list(sock);
} else if (strcmp(command, "pwd") == 0) {
send(sock, "pwd", strlen("pwd"), 0);
handle_pwd(sock);
} else if (strcmp(command, "cd") == 0) {
if (argument == NULL) {
printf("Usage: cd directory\n");
continue;
}
handle_cd(argument, sock);
} else if (strcmp(command, "down") == 0) {
if (argument == NULL) {
printf("Usage: down filename\n");
continue;
}
handle_down(argument, sock);
} else if (strcmp(command, "lcd") == 0) {
if (argument == NULL) {
printf("Usage: lcd directory\n");
continue;
}
int result = chdir(argument);
if (result == -1) {
printf("Error: cannot change directory.\n");
}
} else if (strcmp(command, "exit") == 0) {
break;
} else {
printf("Invalid command\n");
}
}
// Close socket
close(sock);
return 0;
}
```
阅读全文