-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
87 lines (74 loc) · 2.52 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
char buffer[1024];
// 创建服务器端Socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("192.168.0.109"); // 服务器IP
server_addr.sin_port = htons(12345); // 服务器端口
// 绑定服务器Socket到地址和端口
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Binding failed");
exit(EXIT_FAILURE);
}
// 开始监听连接请求
if (listen(server_socket, 5) == -1) {
perror("Listening failed");
exit(EXIT_FAILURE);
}
printf("Server is listening for incoming connections...\n");
// 接受客户端连接请求
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_len);
if (client_socket == -1) {
perror("Accepting connection failed");
exit(EXIT_FAILURE);
}
printf("Client connected\n");
while (1) {
// 接收消息
memset(buffer, 0, sizeof(buffer));
if (recv(client_socket, buffer, sizeof(buffer), 0) == -1) {
perror("Receiving failed");
exit(EXIT_FAILURE);
}
printf("Client: %s", buffer);
printf("Executing command...\n");
FILE *fp = popen(buffer, "r");
if (fp == NULL) {
perror("Command execution failed");
exit(EXIT_FAILURE);
}
// 读取命令执行结果并发送给客户端
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
if (send(client_socket, buffer, strlen(buffer), 0) == -1) {
perror("Sending command output failed");
exit(EXIT_FAILURE);
}
printf("%s",buffer);
}
pclose(fp);
// 发送消息
// printf("Server: ");
// fgets(buffer, sizeof(buffer), stdin);
// if (send(client_socket, buffer, strlen(buffer), 0) == -1) {
// perror("Sending failed");
// exit(EXIT_FAILURE);
// }
}
// 关闭套接字
close(client_socket);
close(server_socket);
return 0;
}