-
Notifications
You must be signed in to change notification settings - Fork 4
/
socket_pair.c
143 lines (129 loc) · 4.09 KB
/
socket_pair.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <memory.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <unistd.h>
int evutil_socketpair(int family, int type, int protocol, int fd[2])
{
/* This code is originally from Tor. Used with permission. */
/* This socketpair does not work when localhost is down. So
* it's really not the same thing at all. But it's close enough
* for now, and really, when localhost is down sometimes, we
* have other problems too.
*/
int listener = -1;
int connector = -1;
int acceptor = -1;
struct sockaddr_in listen_addr;
struct sockaddr_in connect_addr;
int size;
int saved_errno = -1;
if (protocol
#ifdef AF_UNIX
|| family != AF_UNIX
#endif
) {
return -1;
}
if (!fd) {
return -1;
}
listener = socket(AF_INET, type, 0);
if (listener < 0)
return -1;
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
listen_addr.sin_port = 0; /* kernel chooses port. */
if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
== -1)
goto tidy_up_and_fail;
printf("listen_addr.sin_addr.s_addr=0x%x string=%s--listen_addr.sin_port=%d\n", listen_addr.sin_addr.s_addr, (char*)inet_ntoa(listen_addr.sin_addr), listen_addr.sin_port);
if (listen(listener, 1) == -1)
goto tidy_up_and_fail;
connector = socket(AF_INET, type, 0);
if (connector < 0)
goto tidy_up_and_fail;
/* We want to find out the port number to connect to. */
size = sizeof(connect_addr);
if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
goto tidy_up_and_fail;
printf("connect_addr.sin_addr.s_addr=0x%x string=%s--connect_addr.sin_port=%d\n", connect_addr.sin_addr.s_addr, (char*)inet_ntoa(connect_addr.sin_addr), connect_addr.sin_port);
if (size != sizeof (connect_addr))
goto abort_tidy_up_and_fail;
if (connect(connector, (struct sockaddr *) &connect_addr,
sizeof(connect_addr)) == -1)
goto tidy_up_and_fail;
size = sizeof(listen_addr);
acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
if (acceptor < 0)
goto tidy_up_and_fail;
if (size != sizeof(listen_addr))
goto abort_tidy_up_and_fail;
/* Now check we are talking to ourself by matching port and host on the
two sockets. */
printf("listen_addr.sin_addr.s_addr=0x%x string=%s--listen_addr.sin_port=%d\n", listen_addr.sin_addr.s_addr, (char*)inet_ntoa(listen_addr.sin_addr), listen_addr.sin_port);
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
goto tidy_up_and_fail;
printf("connect_addr.sin_addr.s_addr=0x%x string=%s--connect_addr.sin_port=%d\n", connect_addr.sin_addr.s_addr, (char*)inet_ntoa(connect_addr.sin_addr), connect_addr.sin_port);
if (size != sizeof (connect_addr)
|| listen_addr.sin_family != connect_addr.sin_family
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|| listen_addr.sin_port != connect_addr.sin_port)
goto abort_tidy_up_and_fail;
fd[0] = connector;
fd[1] = acceptor;
return 0;
abort_tidy_up_and_fail:
tidy_up_and_fail:
return -1;
}
void* work_thread(void* argc)
{
int* fd = (int*)argc;
while(1)
{
char c[50];
int n = recv(fd[1], &c, sizeof(c), 0);
printf("recv%dbyte=%s\n", n, c);
// sleep(1);
char b[]="Goodbye!";
n = send(fd[1], &b, sizeof(b), 0);
printf("send%dbyte=%s\n", n, b);
}
}
int main(int argc, char* argv[])
{
int socket_pair[2];
pthread_t tid;
/**
if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair) == -1)
{
perror("create socketpair failed\n");
return -1;
}
**/
if(socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair) == -1)
{
perror("create socketpair failed\n");
return -1;
}
pthread_create(&tid, NULL,
work_thread, &socket_pair[0]);
while(1)
{
char c[] = "Hello,thread!";
int n = send(socket_pair[0], &c, sizeof(c), 0);
printf("++send%dbyte=%s\n", n, c);
sleep(1);
n = recv(socket_pair[0], &c, sizeof(c), 0);
printf("++recv%dbyte=%s\n", n, c);
}
pthread_join(tid, NULL);
close(socket_pair[0]);
close(socket_pair[1]);
return 0;
}