-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathPractise_05_server.c
127 lines (113 loc) · 3.15 KB
/
Practise_05_server.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
#include "../../tool/AddrTool.h"
typedef void(*sig_handler)(int);
sig_handler fuck(int signo, sig_handler newhandler)
{
struct sigaction newact, oldact;
bzero(&newact, sizeof(newact));
newact.sa_handler = newhandler;
if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
newact.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
newact.sa_flags |= SA_RESTART;
#endif
}
if (sigaction(signo, &newact, &oldact) < 0)
return SIG_ERR;
return oldact.sa_handler;
}
void collect_child(int signo)
{
int status;
pid_t pid;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
printf("collect child %ld\n", (long)pid);
if (pid < 0 && errno != ECHILD) {
perror("waipid error!");
exit(1);
}
}
int main(int argc, char const *argv[])
{
if (argc != 2) {
printf("usage : %s <port>\n", argv[0]);
exit(1);
}
int fd = -1;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket fd create fail!");
exit(1);
}
struct sockaddr_in svraddr, cliaddr;
bzero(&svraddr, sizeof(svraddr));
svraddr.sin_family = AF_INET;
svraddr.sin_port = htons(atoi(argv[1]));
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
int on = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
perror("setsockopt error!");
exit(1);
}
if (bind(fd, (struct sockaddr *)&svraddr, sizeof(svraddr)) < 0) {
perror("bind error!");
exit(1);
}
if (listen(fd, 10000) < 0) {
perror("listen error!");
exit(1);
}
fuck(SIGCHLD, collect_child);
char buf[1024];
int connfd = -1;
socklen_t clilen = sizeof(cliaddr);
for (;;) {
if ((connfd = accept(fd, (struct sockaddr *)&cliaddr, &clilen)) < 0) {
perror("accept error!");
continue;
}
char *srcInfo = getAddrInfo(&cliaddr);
char *dstInfo = getSockInfo(connfd);
printf("TCP connection from %s to %s\n", srcInfo, dstInfo);
free(srcInfo);
free(dstInfo);
pid_t pid = -1;
if ((pid = fork()) < 0) {
perror("fork error!");
exit(1);
} else if (pid == 0) {
FILE *fpin = NULL, *fpout = NULL;
if ((fpin = fdopen(connfd, "r")) == NULL) {
perror("fdopen error!");
exit(1);
}
if ((fpout = fdopen(connfd, "w")) == NULL) {
perror("fdopen error!");
exit(1);
}
/* 将缓冲方式设置为无缓冲 */
// setvbuf(fpout, NULL, _IONBF, 0);
while (fgets(buf, sizeof(buf), fpin) != NULL) {
if (fputs(buf, fpout) == EOF) {
printf("fputs error!");
exit(1);
}
/* 强制刷清缓冲区 */
fflush(fpout);
}
exit(0);
}
close(connfd);
}
return 0;
}