1 | #include <assert.h>
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <string.h>
|
---|
4 | #include <unistd.h>
|
---|
5 | #include <sys/socket.h>
|
---|
6 | #include <sys/select.h>
|
---|
7 |
|
---|
8 | int
|
---|
9 | main(int argc, char **argv)
|
---|
10 | {
|
---|
11 | const char *test = "test string";
|
---|
12 | int pair[2];
|
---|
13 |
|
---|
14 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
|
---|
15 | printf("socketpair failed\n");
|
---|
16 | return 1;
|
---|
17 | }
|
---|
18 | write(pair[0], test, strlen(test)+1);
|
---|
19 | shutdown(pair[0], SHUT_WR);
|
---|
20 | for (;;) {
|
---|
21 | char buf[256];
|
---|
22 | int len;
|
---|
23 | fd_set set;
|
---|
24 |
|
---|
25 | FD_ZERO(&set);
|
---|
26 | FD_SET(pair[1], &set);
|
---|
27 | if (select(pair[1] + 1, &set, NULL, NULL, NULL) < 0) {
|
---|
28 | printf("select failed\n");
|
---|
29 | return 1;
|
---|
30 | }
|
---|
31 | assert(FD_ISSET(pair[1], &set));
|
---|
32 | len = read(pair[1], buf, sizeof(buf));
|
---|
33 | if (len < 0) {
|
---|
34 | printf("recv failed\n");
|
---|
35 | return 1;
|
---|
36 | } else if (len > 0) {
|
---|
37 | printf("recv: %d bytes\n", len);
|
---|
38 | } else {
|
---|
39 | printf("recv: EOF\n");
|
---|
40 | break;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | close(pair[0]);
|
---|
44 | close(pair[1]);
|
---|
45 | return 0;
|
---|
46 | }
|
---|