-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_threads.c
401 lines (336 loc) · 9.87 KB
/
os_threads.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAXLINE 8192
#define SERV_PORT 91
struct s_info { //定义一个结构体, 将地址结构跟cfd捆绑
struct sockaddr_in cliaddr;
int connfd;
};
void perr_exit(const char *s)
{
perror(s);
exit(-1);
}
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
int n;
again:
if ((n = accept(fd, sa, salenptr)) < 0) {
if ((errno == ECONNABORTED) || (errno == EINTR))
goto again;
//else
// perr_exit("accept error");
}
return n;
}
int Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
int n;
if ((n = bind(fd, sa, salen)) < 0)
perr_exit("bind error");
return n;
}
int Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
int n;
if ((n = connect(fd, sa, salen)) < 0)
perr_exit("connect error");
return n;
}
int Listen(int fd, int backlog)
{
int n;
if ((n = listen(fd, backlog)) < 0)
perr_exit("listen error");
return n;
}
int Socket(int family, int type, int protocol)
{
int n;
if ((n = socket(family, type, protocol)) < 0)
perr_exit("socket error");
return n;
}
ssize_t Read(int fd, void *ptr, size_t nbytes)
{
ssize_t n;
again:
if ( (n = read(fd, ptr, nbytes)) == -1) {
if (errno == EINTR)
goto again;
else
return -1;
}
return n;
}
ssize_t Write(int fd, const void *ptr, size_t nbytes)
{
ssize_t n;
again:
if ( (n = write(fd, ptr, nbytes)) == -1) {
if (errno == EINTR)
goto again;
else
return -1;
}
return n;
}
int Close(int fd)
{
int n;
if ((n = close(fd)) == -1)
perr_exit("close error");
return n;
}
ssize_t Readn(int fd, void *vptr, size_t n)
{
size_t nleft; //usigned int 剩余未读取的字节数
ssize_t nread; //int 实际读到的字节数
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ((nread = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR)
nread = 0;
else
return -1;
} else if (nread == 0)
break;
nleft -= nread;
ptr += nread;
}
return n - nleft;
}
ssize_t Writen(int fd, const void *vptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0;
else
return -1;
}
nleft -= nwritten;
ptr += nwritten;
}
return n;
}
static ssize_t my_read(int fd, char *ptr)
{
static int read_cnt;
static char *read_ptr;
static char read_buf[100];
if (read_cnt <= 0) {
again:
if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
if (errno == EINTR)
goto again;
return -1;
} else if (read_cnt == 0)
return 0;
read_ptr = read_buf;
}
read_cnt--;
*ptr = *read_ptr++;
return 1;
}
ssize_t Readline(int fd, void *vptr, size_t maxlen)
{
ssize_t n, rc;
char c, *ptr;
ptr = vptr;
for (n = 1; n < maxlen; n++) {
if ( (rc = my_read(fd, &c)) == 1) {
*ptr++ = c;
if (c == '\n')
break;
} else if (rc == 0) {
*ptr = 0;
return n - 1;
} else
return -1;
}
*ptr = 0;
return n;
}
//////////////////////////////////////////////////////////////////////
int file_size(const char* filePath) {
if(filePath == NULL) return 0;
struct stat sb;
if(stat(filePath, &sb) < 0)
return 0;
return sb.st_size;
}
void *do_work_file(void *arg)
{
int n,i;
struct s_info *ts = (struct s_info*)arg;
char buf[MAXLINE];
char str[INET_ADDRSTRLEN];
memset(buf, 0, sizeof(buf));
n = Read(ts->connfd, buf, MAXLINE); //读客户端
if (n == 0)
{
printf("the client %d closed...\n", ts->connfd);
goto end;
}
printf("received from %s at PORT %d\n",
inet_ntop(AF_INET, &(*ts).cliaddr.sin_addr, str, sizeof(str)),
ntohs((*ts).cliaddr.sin_port)); //打印客户端信息(IP/PORT)
printf("%s\n", buf);
/// open file & send it
const char *filename = "1.log";
int size = file_size(filename);
if (size == 0) {
goto end;
}
char resp_1024[1024] = {0};
sprintf(resp_1024, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n", size);
Write(ts->connfd, resp_1024, strlen(resp_1024)); //回写给客户端
printf("%s\n", resp_1024);
FILE *fin = fopen(filename, "r");
if (!fin) {
goto end;
}
char fbuf[1024*4]={0};
int blockSize = sizeof(fbuf);
int writelen = 0;
i = 0;
while (size > 0)
{
if (size < blockSize)
blockSize = size;
int t = fread(fbuf, sizeof(char), blockSize, fin);
writelen = Write(ts->connfd, fbuf, blockSize); //回写给客户端
size = size - blockSize;
i++;
if (i == 10) {
//break;
}
if(writelen != blockSize)
{
goto end;
}
}
end:
if (fin) {
fclose(fin);
}
sleep(1);
Close(ts->connfd);
free(ts);
return (void *)0;
}
void *do_post(void *arg)
{
int n,i;
struct s_info *ts = (struct s_info*)arg;
char buf[MAXLINE];
char str[INET_ADDRSTRLEN];
char resp_1024[1024] = "HTTP/1.1 200 OK\r\nSet-Cookie:k1=v1\r\nContent-Length: 12\r\n\r\nabcdefghijkl";
if (1)
{
memset(buf, 0, sizeof(buf));
for (i = 0; i < 1; i++) {
n = Read(ts->connfd, buf, MAXLINE); //读客户端
if (n == 0)
{
printf("the client %d closed...\n", ts->connfd);
break; //跳出循环,关闭cfd
}
}
printf("received from %s at PORT %d\n",
inet_ntop(AF_INET, &(*ts).cliaddr.sin_addr, str, sizeof(str)),
ntohs((*ts).cliaddr.sin_port)); //打印客户端信息(IP/PORT)
//sleep(10);
printf("%s\n", buf);
Write(ts->connfd, resp_1024, strlen(resp_1024)); //回写给客户端
printf("%s\n", resp_1024);
}
Close(ts->connfd);
free(ts);
return (void *)0;
}
void *do_work(void *arg)
{
int n,i;
struct s_info *ts = (struct s_info*)arg;
char buf[MAXLINE];
char str[INET_ADDRSTRLEN];
char resp_1024[1024] = "HTTP/1.1 200 OK\r\nSet-Cookie:k1=v1\r\nContent-type: text/plain\r\nContent-Length: 15\r\nConnection: Keep-Alive\r\n\r\nabcdefghijkldef";
while (1)
{
memset(buf, 0, sizeof(buf));
n = Read(ts->connfd, buf, MAXLINE); //读客户端
if (n == 0)
{
printf("the client %d closed...\n", ts->connfd);
break; //跳出循环,关闭cfd
}
printf("received from %s at PORT %d\n",
inet_ntop(AF_INET, &(*ts).cliaddr.sin_addr, str, sizeof(str)),
ntohs((*ts).cliaddr.sin_port)); //打印客户端信息(IP/PORT)
printf("%s\n", buf);
Write(ts->connfd, resp_1024, strlen(resp_1024)); //回写给客户端
printf("%s\n", resp_1024);
break;
}
Close(ts->connfd);
free(ts);
return (void *)0;
}
int main(void)
{
signal(SIGPIPE, SIG_IGN);
struct sockaddr_in servaddr, cliaddr;
socklen_t cliaddr_len;
int listenfd, connfd;
pthread_t tid;
int i = 0;
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
int opt = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
Bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
Listen(listenfd, 128);
printf("Accepting client connect ...\n");
while (1)
{
cliaddr_len = sizeof(cliaddr);
connfd = Accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
printf("accept fd: %d\n", connfd);
struct s_info* ts = (struct s_info*)malloc(sizeof(struct s_info));
ts->cliaddr = cliaddr;
ts->connfd = connfd;
/* 达到线程最大数时,pthread_create出错处理, 增加服务器稳定性 */
//pthread_create(&tid, NULL, do_work, (void*)ts);
pthread_create(&tid, NULL, do_post, (void*)ts);
//pthread_create(&tid, NULL, do_work_file, (void*)ts);
pthread_detach(tid);
i++;
}
return 0;
}