-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTCPEchoServer_UnixFd.c
108 lines (96 loc) · 2.69 KB
/
TCPEchoServer_UnixFd.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include "../../tool/PathTool.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("waitpid error!");
}
int main(int argc, char const *argv[])
{
int fd = -1;
if ((fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
perror("socket fd create fail!");
exit(1);
}
unlink(getAbsolutePath("unix_hanson"));
struct sockaddr_un svraddr, cliaddr;
strcpy(svraddr.sun_path, getAbsolutePath("unix_hanson"));
svraddr.sun_family = AF_LOCAL;
if (bind(fd, (struct sockaddr *)&svraddr, SUN_LEN(&svraddr)) < 0) {
perror("bind error!");
exit(1);
}
if (listen(fd, 1000) < 0) {
perror("listen error!");
exit(1);
}
fuck(SIGCHLD, collect_child);
int connfd = -1;
char buf[1024];
socklen_t clilen = sizeof(cliaddr);
ssize_t nread = 0, nwrite = 0;
for (;;) {
if ((connfd = accept(fd, (struct sockaddr *)&cliaddr, &clilen)) < 0) {
perror("accept error!");
continue;
}
printf("new client connected cliaddr.sun_path=%s\n", cliaddr.sun_path);
pid_t pid = 0;
if ((pid = fork()) < 0) {
perror("fork error!");
continue;
} else if (pid == 0) {
for (;;) {
if ((nread = read(connfd, buf, sizeof(buf))) < 0) {
perror("read error!");
exit(1);
} else if (nread == 0) {
printf("client close connection\n");
close(connfd);
break;
} else {
if ((nwrite = write(connfd, buf, nread)) != nread) {
perror("write error!");
close(connfd);
break;
}
}
}
exit(0);
}
close(connfd);
}
return 0;
}