使用c++ 写一个远程控制代码,要求,多并发,实现功能,打开文件,关闭文件,上传文件,下载文件,远程监控,
时间: 2024-05-14 11:16:18 浏览: 14
下面是一个简单的远程控制代码实现,使用了多线程实现多并发:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>
#define MAX_CLIENTS 10
#define FILE_BUFFER_SIZE 1024
struct client_info {
int sock_fd;
struct sockaddr_in client_addr;
};
void *handle_client(void *arg) {
struct client_info *info = (struct client_info *) arg;
char buffer[1024];
int recv_len;
char filename[256];
FILE *fp;
int filesize;
char file_buffer[FILE_BUFFER_SIZE];
int sent_size;
int read_size;
printf("New client connected: %s:%d\n", inet_ntoa(info->client_addr.sin_addr), ntohs(info->client_addr.sin_port));
while (1) {
// 接收客户端发送的命令
recv_len = recv(info->sock_fd, buffer, sizeof(buffer), 0);
if (recv_len == 0) {
// 客户端关闭了连接
printf("Client disconnected: %s:%d\n", inet_ntoa(info->client_addr.sin_addr), ntohs(info->client_addr.sin_port));
close(info->sock_fd);
free(info);
pthread_exit(NULL);
} else if (recv_len < 0) {
// 接收数据失败
printf("Failed to receive data from client: %s:%d\n", inet_ntoa(info->client_addr.sin_addr), ntohs(info->client_addr.sin_port));
close(info->sock_fd);
free(info);
pthread_exit(NULL);
}
// 处理客户端发送的命令
if (strncmp(buffer, "open", 4) == 0) {
// 打开文件
sscanf(buffer + 5, "%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
send(info->sock_fd, "Failed to open file", strlen("Failed to open file"), 0);
} else {
send(info->sock_fd, "File opened", strlen("File opened"), 0);
fclose(fp);
}
} else if (strncmp(buffer, "close", 5) == 0) {
// 关闭文件
sscanf(buffer + 6, "%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
send(info->sock_fd, "Failed to close file", strlen("Failed to close file"), 0);
} else {
send(info->sock_fd, "File closed", strlen("File closed"), 0);
fclose(fp);
}
} else if (strncmp(buffer, "upload", 6) == 0) {
// 上传文件
sscanf(buffer + 7, "%s %d", filename, &filesize);
fp = fopen(filename, "w");
if (fp == NULL) {
send(info->sock_fd, "Failed to create file", strlen("Failed to create file"), 0);
} else {
send(info->sock_fd, "Ready to receive file", strlen("Ready to receive file"), 0);
sent_size = 0;
while (sent_size < filesize) {
read_size = recv(info->sock_fd, file_buffer, FILE_BUFFER_SIZE, 0);
if (read_size < 0) {
printf("Failed to receive file from client: %s:%d\n", inet_ntoa(info->client_addr.sin_addr), ntohs(info->client_addr.sin_port));
fclose(fp);
remove(filename);
break;
}
fwrite(file_buffer, 1, read_size, fp);
sent_size += read_size;
}
fclose(fp);
}
} else if (strncmp(buffer, "download", 8) == 0) {
// 下载文件
sscanf(buffer + 9, "%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
send(info->sock_fd, "Failed to open file", strlen("Failed to open file"), 0);
} else {
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);
sprintf(buffer, "Ready to send file %d", filesize);
send(info->sock_fd, buffer, strlen(buffer), 0);
sent_size = 0;
while (sent_size < filesize) {
read_size = fread(file_buffer, 1, FILE_BUFFER_SIZE, fp);
if (read_size < 0) {
printf("Failed to read file: %s\n", filename);
break;
}
send(info->sock_fd, file_buffer, read_size, 0);
sent_size += read_size;
}
fclose(fp);
}
} else if (strncmp(buffer, "monitor", 7) == 0) {
// 远程监控
printf("Start monitoring client: %s:%d\n", inet_ntoa(info->client_addr.sin_addr), ntohs(info->client_addr.sin_port));
while (1) {
// 接收客户端发送的数据
recv_len = recv(info->sock_fd, buffer, sizeof(buffer), 0);
if (recv_len == 0) {
// 客户端关闭了连接
printf("Client disconnected: %s:%d\n", inet_ntoa(info->client_addr.sin_addr), ntohs(info->client_addr.sin_port));
close(info->sock_fd);
free(info);
pthread_exit(NULL);
} else if (recv_len < 0) {
// 接收数据失败
printf("Failed to receive data from client: %s:%d\n", inet_ntoa(info->client_addr.sin_addr), ntohs(info->client_addr.sin_port));
close(info->sock_fd);
free(info);
pthread_exit(NULL);
}
printf("Received data from client: %s:%d\n", inet_ntoa(info->client_addr.sin_addr), ntohs(info->client_addr.sin_port));
printf("%s", buffer);
}
}
}
}
int main(int argc, char *argv[]) {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
pthread_t threads[MAX_CLIENTS];
int i = 0;
// 创建服务器套接字
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
// 绑定服务器地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(atoi(argv[1]));
if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
perror("Failed to bind socket");
exit(EXIT_FAILURE);
}
// 监听来自客户端的连接请求
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("Failed to listen on socket");
exit(EXIT_FAILURE);
}
printf("Server started at port %d\n", atoi(argv[1]));
while (1) {
// 接收来自客户端的连接请求
client_addr_len = sizeof(client_addr);
client_fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
if (client_fd < 0) {
perror("Failed to accept client connection");
continue;
}
// 创建线程处理客户端连接
struct client_info *info = (struct client_info *) malloc(sizeof(struct client_info));
info->sock_fd = client_fd;
info->client_addr = client_addr;
pthread_create(&threads[i++], NULL, handle_client, info);
if (i == MAX_CLIENTS) {
i = 0;
while (i < MAX_CLIENTS) {
pthread_join(threads[i++], NULL);
}
i = 0;
}
}
return 0;
}
```
这个程序实现了以下功能:
- 打开文件:客户端发送 `open filename` 命令,服务器会尝试打开指定的文件,如果打开成功,会向客户端发送 "File opened" 消息,否则会向客户端发送 "Failed to open file" 消息。
- 关闭文件:客户端发送 `close filename` 命令,服务器会尝试关闭指定的文件,如果关闭成功,会向客户端发送 "File closed" 消息,否则会向客户端发送 "Failed to close file" 消息。
- 上传文件:客户端发送 `upload filename filesize` 命令,服务器会尝试创建指定的文件并准备接收客户端发送的数据。如果创建文件失败,会向客户端发送 "Failed to create file" 消息,否则会向客户端发送 "Ready to receive file" 消息,并开始接收客户端发送的数据,直到接收完整个文件。
- 下载文件:客户端发送 `download filename` 命令,服务器会尝试打开指定的文件并获取文件大小。如果打开文件失败,会向客户端发送 "Failed to open file" 消息,否则会向客户端发送 "Ready to send file filesize" 消息,并开始发送文件数据,直到发送完整个文件。
- 远程监控:客户端发送 `monitor` 命令,服务器会进入远程监控模式,接收客户端发送的数据并打印到控制台上。客户端可以随时关闭连接来结束监控模式。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)