在Windows环境下编写具体的C语言代码实现远程log保存、查询功能 1、实现一个tcp server端 2、实现一个tcp client端 3、server端和client自己设计简单的协议(如read_log write_log get_res 等等) 4、log保存在server端 5、client 通过socket向server端发送请求 写一条log/读所有log/计算数据采集结果 6、server通过socket返回响应Client查询结果 7、当client异常退出时,server端应该有对应的处理机制(自行设计处理机制) 8、server端的log打印不做任何要求,但是client正常/异常离线需要有对应的log,请编写出具体的代码给我
时间: 2024-02-16 08:00:51 浏览: 64
以下是一个简单的C语言代码实现远程log保存、查询功能的示例:
server.c
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define PORT 8888
#define BUF_SIZE 1024
#define LOG_FILE "log.txt"
struct client_info {
SOCKET socket;
int is_online;
};
void save_log(const char *log) {
FILE *fp = fopen(LOG_FILE, "a");
if (fp == NULL) {
printf("Failed to open log file\n");
return;
}
fprintf(fp, "%s\n", log);
fclose(fp);
}
void handle_request(SOCKET socket, const char *buf, struct client_info *clients, int num_clients) {
char response[BUF_SIZE];
char cmd[BUF_SIZE], arg[BUF_SIZE];
int i, online_clients = 0;
sscanf(buf, "%s %s", cmd, arg);
if (strcmp(cmd, "write_log") == 0) {
save_log(arg);
sprintf(response, "OK");
} else if (strcmp(cmd, "read_log") == 0) {
FILE *fp = fopen(LOG_FILE, "r");
if (fp == NULL) {
sprintf(response, "Failed to open log file");
} else {
response[0] = '\0';
while (fgets(buf, BUF_SIZE, fp) != NULL) {
strcat(response, buf);
}
fclose(fp);
}
} else if (strcmp(cmd, "get_res") == 0) {
for (i = 0; i < num_clients; i++) {
if (clients[i].is_online) {
online_clients++;
}
}
sprintf(response, "Online clients: %d", online_clients);
} else {
sprintf(response, "Invalid command");
}
send(socket, response, strlen(response), 0);
}
void handle_client(SOCKET socket, struct client_info *clients, int num_clients) {
char buf[BUF_SIZE];
int nbytes, i, client_index = -1;
for (i = 0; i < num_clients; i++) {
if (clients[i].socket == INVALID_SOCKET) {
client_index = i;
break;
}
}
if (client_index == -1) {
printf("Too many clients, closing connection\n");
closesocket(socket);
return;
}
clients[client_index].socket = socket;
clients[client_index].is_online = 1;
printf("Client connected\n");
while ((nbytes = recv(socket, buf, BUF_SIZE, 0)) > 0) {
buf[nbytes] = '\0';
printf("Received: %s\n", buf);
handle_request(socket, buf, clients, num_clients);
}
printf("Client disconnected\n");
closesocket(socket);
clients[client_index].socket = INVALID_SOCKET;
clients[client_index].is_online = 0;
}
int main() {
WSADATA wsa;
SOCKET listen_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int addr_len = sizeof(client_addr);
struct client_info clients[10];
int i;
for (i = 0; i < 10; i++) {
clients[i].socket = INVALID_SOCKET;
clients[i].is_online = 0;
}
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("Failed to initialize Winsock\n");
return 1;
}
if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Failed to create socket\n");
return 1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(listen_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("Failed to bind socket\n");
return 1;
}
if (listen(listen_socket, 10) == SOCKET_ERROR) {
printf("Failed to listen on socket\n");
return 1;
}
printf("Server listening on port %d\n", PORT);
while (1) {
if ((client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &addr_len)) == INVALID_SOCKET) {
printf("Failed to accept connection\n");
continue;
}
handle_client(client_socket, clients, 10);
}
closesocket(listen_socket);
WSACleanup();
return 0;
}
```
client.c
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define SERVER_ADDR "127.0.0.1"
#define PORT 8888
#define BUF_SIZE 1024
int send_request(SOCKET socket, const char *buf) {
int nbytes = send(socket, buf, strlen(buf), 0);
if (nbytes == SOCKET_ERROR) {
printf("Failed to send request\n");
return 0;
}
return 1;
}
int main() {
WSADATA wsa;
SOCKET socket;
struct sockaddr_in server_addr;
char buf[BUF_SIZE];
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("Failed to initialize Winsock\n");
return 1;
}
if ((socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Failed to create socket\n");
return 1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
server_addr.sin_port = htons(PORT);
if (connect(socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("Failed to connect to server\n");
return 1;
}
printf("Connected to server\n");
while (1) {
printf("> ");
fgets(buf, BUF_SIZE, stdin);
if (buf[strlen(buf) - 1] == '\n') {
buf[strlen(buf) - 1] = '\0';
}
if (strcmp(buf, "quit") == 0) {
break;
}
if (!send_request(socket, buf)) {
continue;
}
if (recv(socket, buf, BUF_SIZE, 0) > 0) {
buf[strlen(buf)] = '\0';
printf("Response: %s\n", buf);
} else {
printf("Failed to receive response\n");
}
}
closesocket(socket);
WSACleanup();
return 0;
}
```
启动server后,可以使用client向server发送请求,如:
```
write_log test log message
read_log
get_res
```
server会对请求进行处理,并返回相应的响应。
阅读全文