-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTCPEchoClient_UnixFd.c
75 lines (68 loc) · 2.05 KB
/
TCPEchoClient_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
#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 <errno.h>
#include "../../tool/PathTool.h"
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);
}
struct sockaddr_un svraddr;
bzero(&svraddr, sizeof(svraddr));
strcpy(svraddr.sun_path, getAbsolutePath("unix_hanson"));
svraddr.sun_family = AF_LOCAL;
if (connect(fd, (struct sockaddr *)&svraddr, SUN_LEN(&svraddr)) < 0) {
perror("connect error!");
exit(1);
}
fd_set rset, allset;
FD_SET(fd, &allset);
FD_SET(fileno(stdin), &allset);
int maxfd = fd > fileno(stdin) ? fd : fileno(stdin);
char buf[1024];
ssize_t nread = 0, nwrite = 0;
for (;;) {
rset = allset;
if (select(maxfd + 1, &rset, NULL, NULL, NULL) < 0) {
perror("select error!");
exit(1);
}
if (FD_ISSET(fileno(stdin), &rset)) {
if ((nread = read(fileno(stdin), buf, sizeof(buf))) < 0) {
perror("read error!");
exit(1);
} else if (nread == 0) {
printf("client finished!\n");
shutdown(fd, SHUT_WR);
FD_CLR(fileno(stdin), &allset);
} else {
if ((nwrite = write(fd, buf, nread)) != nread) {
perror("write error!");
exit(1);
}
}
}
if (FD_ISSET(fd, &rset)) {
if ((nread = read(fd, buf, sizeof(buf))) < 0) {
perror("read error!");
exit(1);
} else if (nread == 0) {
printf("server terminated\n");
exit(1);
} else {
if ((nwrite = write(fileno(stdout), buf, nread)) != nread) {
perror("write error!");
exit(1);
}
}
}
}
return 0;
}