forked from Request2609/codeOfPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (49 loc) · 1.37 KB
/
Copy pathmain.cpp
File metadata and controls
53 lines (49 loc) · 1.37 KB
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
#include "processPool.h"
void handle(int sig) {
if(sig == SIGINT) {
exit(0) ;
}
}
int main(int argc, char** argv) {
if(argc != 3) {
cout << "error usage" << endl ;
return 1 ;
}
struct sockaddr_in addr ;
bzero(&addr, sizeof(addr)) ;
addr.sin_family = AF_INET ;
int ret = inet_pton(AF_INET, argv[1], &addr.sin_addr) ;
if(ret < 0) {
cout << __LINE__ << " " << __FILE__ << endl ;
return 0 ;
}
addr.sin_port = htons(atoi(argv[2])) ;
int listenFd = socket(AF_INET, SOCK_STREAM, 0) ;
if(listenFd < 0) {
cout << __LINE__ << " " << __FILE__ << endl ;
return 1;
}
int use = 1 ;
ret = setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR, &use, sizeof(use)) ;
if(ret < 0) {
cout << __LINE__ << " " << __FILE__ << endl ;
return 0;
}
ret = bind(listenFd, (const sockaddr*)&addr, sizeof(addr)) ;
if(ret < 0) {
cout << __FILE__ << " " << __LINE__ << endl ;
return 0 ;
}
ret = listen(listenFd, 100) ;
if(ret < 0) {
cout << "监听失败!" << endl ;
return -1 ;
}
//中断回收进程后再退出
signal(SIGINT, handle) ;
//创建
shared_ptr<processPool<cgiConn>> pool = processPool<cgiConn> :: create(listenFd, 2);
if(pool)
pool->run() ;
close(listenFd) ;
}