-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
67 lines (58 loc) · 1.55 KB
/
main.cpp
File metadata and controls
67 lines (58 loc) · 1.55 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <pthread.h>
#include "httpd.h"
#include "thread_pool.h"
void* accept_request(void* arg)
{
long long sock = (long long)arg;
//线程分离,无需主线程等待
//pthread_detach(pthread_self()); //线程池不需要线程退出
return (void*)request_handle(sock);
}
int main(int argc, char* argv[])
{
signal(SIGPIPE, SIG_IGN);
int listen_sock;
if(argc == 2)
listen_sock = startup(atoi(argv[1]));
else if(argc == 1) //使用默认端口
listen_sock = startup(8888);
else
{
printf("Usage: %s [listen port]\n", argv[0]);
exit(0);
}
daemon(1, 0);//守护进程
//第一个参数为0工作目录更改为根目录/
//第二个参数为0默认打开的三个文件描述符重定向到/dev/null
struct sockaddr_in remote;
socklen_t len = sizeof(remote);
//创建线程池
if (tpool_create(8) != 0) {
print_log("tpool_create failed", FATAL, __FILE__, __LINE__);
exit(1);
}
while(1)
{
int conn_sock = accept(listen_sock, (struct sockaddr*)&remote, &len);
if(conn_sock < 0)
{
print_log(strerror(errno), WARNING, __FILE__, __LINE__);
continue;
}
//printf("New Connection! %s:%d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
//pthread_t id;
//if(pthread_create(&id, NULL, accept_request, (void*)conn_sock) != 0)
//{
// print_log(strerror(errno), FATAL, __FILE__, __LINE__);
// close(conn_sock);
//}
if(tpool_add_work(accept_request, (void*)conn_sock) != 0)
{
print_log("tpool_add_work failed", FATAL, __FILE__, __LINE__);
close(conn_sock);
}
}
close(listen_sock);
tpool_destroy();
return 0;
}