-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
139 lines (119 loc) · 2.65 KB
/
main.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
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
136
137
138
139
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include "threadpool.h"
struct threadpool *gThreadPool;
/*
* === FUNCTION ======================================================================
* Name: get_listen_socket
* Description: 创建监听socket
* =====================================================================================
*/
int get_listen_socket()
{
int sock,res;
int reuse = 1;
struct sockaddr_in addr;
sock = socket(AF_INET,SOCK_STREAM,0);
if(0 >= sock)
{
perror("socket error!");
exit(0);
}
bzero(&addr, sizeof(addr));
addr.sin_addr.s_addr = inet_addr("0.0.0.0");
addr.sin_port = htons(51005);
addr.sin_family = AF_INET;
res = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if(res)
{
perror("setsockopt error!");
exit(0);
}
res = bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr));
if(res)
{
perror("bind error!");
exit(0);
}
res = listen(sock,10);
if(res)
{
perror("listen error!");
exit(0);
}
return sock;
}
/*
* === FUNCTION ======================================================================
* Name: do_job
* Description: 业务处理函数,client:客户端socket。
* =====================================================================================
*/
void do_job(client)
{
char buf[1024];
int len;
while(1)
{
len = read(client,buf,sizeof(buf));
if(len <= 0)
{
perror("read error!");
break;
}
if(!memcmp("exit",buf,len))
{
send(client,"GoodBye",7,0);
break;
}
len = send(client,buf,len,0);
if(len <= 0)
{
perror("send error!");
break;
}
}
close(client);
}
/*
* === FUNCTION ======================================================================
* Name: work
* Description: 领导者/追随者模式实例函数,arg:监听socket。
* =====================================================================================
*/
void *work(void *arg)
{
int *listensock = (int *)arg;
int client;
struct sockaddr_in remote;
socklen_t len;
client = accept(*listensock,(struct sockaddr *)&remote,&len);
if(client < 0)
{
perror("accept error!");
return;
}
threadpool_add_job( gThreadPool, work, (void *)listensock );
do_job(client);
}
int main(int argc, char *argv[])
{
int *listensock;
signal(SIGPIPE, SIG_IGN);
listensock = (int *)malloc(sizeof(int));
*listensock = get_listen_socket();
gThreadPool = threadpool_init(6,20);
//work(listensock);
threadpool_add_job( gThreadPool, work, (void *)listensock );
while(1)
{
sleep(10);
}
return 0;
}