-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSocketPairTest.c
49 lines (40 loc) · 994 Bytes
/
SocketPairTest.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char const *argv[])
{
int fds[2];
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) < 0) {
perror("socketpair error!");
exit(1);
}
int svrfd = fds[0];
int clifd = fds[1];
char *msg = "hello world";
ssize_t nwrite = 0, nread = 0;
if ((nwrite = write(svrfd, msg, strlen(msg))) < 0) {
perror("write error!");
exit(1);
}
char buf[1024];
if ((nread = read(clifd, buf, sizeof(buf))) < 0) {
perror("read error!");
exit(1);
}
buf[nread] = 0;
printf("%s\n", buf);
if ((nwrite = write(clifd, msg, strlen(msg))) < 0) {
perror("write error!");
exit(1);
}
if ((nread = read(svrfd, buf, sizeof(buf))) < 0) {
perror("read error!");
exit(1);
}
buf[nread] = 0;
printf("%s\n", buf);
return 0;
}