-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cpp
137 lines (115 loc) · 2.41 KB
/
Server.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <memory.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
#define PORT 8889
#define IP "192.168.2.18"
#define BUFFERISZE (1024*1024*4)
int MakeDaemon()
{
int err = 0;
err = daemon(0, 0);
if (err != 0) {
err = errno;
}
return err;
}
int main()
{
int err = 0;
int fd = -1;
int client = -1;
struct sockaddr_in localaddr;
char buf[BUFFERISZE] = {0};
char output[BUFFERISZE] = {0};
FILE * handle = NULL;
do {
/* serves as daemon*/
#if 0
err = MakeDaemon();
if (err != 0) {
break;
}
#endif
/* create socket */
fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == fd) {
err = errno;
std::cerr<<"create socket failed!"<<std::endl;
break;
}
memset(&localaddr, 0, sizeof(localaddr));
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons(PORT);
localaddr.sin_addr.s_addr = inet_addr(IP);
err = bind(fd, (struct sockaddr*)&localaddr, sizeof(localaddr));
if (0 != err) {
err = errno;
std::cerr<<"bind failed!"<<std::endl;
break;
}
err = listen(fd, 10);
if (0 != err) {
err = errno;
std::cerr<<"listen failed!"<<std::endl;
break;
}
while(1) {
/*loop to handle request*/
struct sockaddr clientaddr;
int ret = 0;
socklen_t len = sizeof(clientaddr);
client = accept(fd, (struct sockaddr*)&clientaddr, &len);
if (client < 0) {
err = errno;
std::cerr<<"accept failed!"<<std::endl;
break;
}
while(1) {
memset(buf, 0, BUFFERISZE);
ret = recv(client, buf, BUFFERISZE, 0);
if (ret < 0) {
std::cerr<<" recv failed!"<<std::endl;
break;
} else if (ret == 0) {
std::cout<<"client closed!"<<std::endl;
break;
} else {
handle = popen(buf, "r");
if (NULL == handle) {
std::cerr<<"open failed, command: " << buf<<std::endl;
break;
}
/*read exec output*/
char * temp = NULL;
do {
memset(output, 0, BUFFERISZE);
temp = fgets(output, BUFFERISZE, handle);
err = send(client, output, strlen(output), 0);
if (err <= 0) {
std::cout<<"send failed!"<<std::endl;
err = errno;
break;
}
} while(NULL != temp);
fclose(handle);
}
if (-1 != client) {
close(client);
client = -1;
break;
}
}
}
} while(0);
if (-1 != fd) {
close(fd);
fd = -1;
}
return err;
}