socket pair,也稱套接字管道,主要用來實現進程內或進程間的一對一的全雙工或半雙工通信,在IO復用模型(如select,poll,epoll等)中起到通知中斷退出循環的作用,在類UNIX系統中已經有現成的實現,API為socketpair,但在WINDOWS系統中沒有,因此本文主要講述WINDOWS平台下soketpair的實現,支持IPv4和IPv6下的tcp、udp套接字管道。對tcp的實現原理是一端在回環地址和某端口上監聽接受另一端的連接;而udp的實現原理是先在兩端各自綁定回環地址和某端口,然後設定對端地址(調用connect實現)。綁定的回環地址在IPv4和IPv6下分別是127.0.0.1、0:0:0:0:0:0:0:1,而端口由系統分配。
接口
1#ifdef __cplusplus
2extern "C" {
3#endif
4
5#ifdef WIN32
6#include <winsock2.h>
7#pragma comment(lib,"ws2_32.lib")
8#endif
9
10#ifdef WIN32
11#define socket_t SOCKET
12#else
13#define socket_t int
14#endif
15
16 int socket_pair(int family,int type,int protocol,socket_t sock[2]);
17
18#ifdef __cplusplus
19}
20#endif
實現
1#ifdef WIN32
2#include <ws2tcpip.h>
3
4static int __stream_socketpair(struct addrinfo* ai,socket_t sock[2])
5{
6 SOCKET listener,client = INVALID_SOCKET,server = INVALID_SOCKET;
7 int opt = 1;
8
9 listener = socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol);
10 if (INVALID_SOCKET==listener)
11 goto fail;
12
13 setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,(const char*)&opt, sizeof(opt));
14
15 if(SOCKET_ERROR==bind(listener,ai->ai_addr,ai->ai_addrlen))
16 goto fail;
17
18 if (SOCKET_ERROR==getsockname(listener,ai->ai_addr,(int*)&ai->ai_addrlen))
19 goto fail;
20
21 if(SOCKET_ERROR==listen(listener,SOMAXCONN))
22 goto fail;
23
24 client = socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol);
25 if (INVALID_SOCKET==client)
26 goto fail;
27
28 if (SOCKET_ERROR==connect(client,ai->ai_addr,ai->ai_addrlen))
29 goto fail;
30
31 server = accept(listener,0,0);
32 if (INVALID_SOCKET==server)
33 goto fail;
34
35 closesocket(listener);
36 sock[0] = client, sock[1] = server;
37 return 0;
38
39fail:
40 if(INVALID_SOCKET!=listener)
41 closesocket(listener);
42 if (INVALID_SOCKET!=client)
43 closesocket(client);
44 return -1;
45}
46
47static int __dgram_socketpair(struct addrinfo* ai,SOCKET sock[2])
48{
49 SOCKET client = INVALID_SOCKET,server=INVALID_SOCKET;
50 struct addrinfo addr,*res = NULL;
51 const char* address;
52 int opt = 1;
53
54 server = socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol);
55 if (INVALID_SOCKET==server)
56 goto fail;
57
58 setsockopt(server,SOL_SOCKET,SO_REUSEADDR,(const char*)&opt, sizeof(opt));
59
60 if(SOCKET_ERROR==bind(server,ai->ai_addr,ai->ai_addrlen))
61 goto fail;
62
63 if (SOCKET_ERROR==getsockname(server,ai->ai_addr,(int*)&ai->ai_addrlen))
64 goto fail;
65
66 client = socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol);
67 if (INVALID_SOCKET==client)
68 goto fail;
69
70 memset(&addr,0,sizeof(addr));
71 addr.ai_family = ai->ai_family;
72 addr.ai_socktype = ai->ai_socktype;
73 addr.ai_protocol = ai->ai_protocol;
74
75 if (AF_INET6==addr.ai_family)
76 address = "0:0:0:0:0:0:0:1";
77 else
78 address = "127.0.0.1";
79
80 if (getaddrinfo(address,"0",&addr,&res))
81 goto fail;
82
83 setsockopt(client,SOL_SOCKET,SO_REUSEADDR,(const char*)&opt, sizeof(opt));
84 if(SOCKET_ERROR==bind(client,res->ai_addr,res->ai_addrlen))
85 goto fail;
86
87 if (SOCKET_ERROR==getsockname(client,res->ai_addr,(int*)&res->ai_addrlen))
88 goto fail;
89
90 if (SOCKET_ERROR==connect(server,res->ai_addr,res->ai_addrlen))
91 goto fail;
92
93 if (SOCKET_ERROR==connect(client,ai->ai_addr,ai->ai_addrlen))
94 goto fail;
95
96 freeaddrinfo(res);
97 sock[0] = client, sock[1] = server;
98 return 0;
99
100fail:
101 if (INVALID_SOCKET!=client)
102 closesocket(client);
103 if (INVALID_SOCKET!=server)
104 closesocket(server);
105 if (res)
106 freeaddrinfo(res);
107 return -1;
108}
109
110static int win32_socketpair(int family,int type,int protocol,socket_t sock[2])
111{
112 const char* address;
113 struct addrinfo addr,*ai;
114 int ret = -1;
115
116 memset(&addr,0,sizeof(addr));
117 addr.ai_family = family;
118 addr.ai_socktype = type;
119 addr.ai_protocol = protocol;
120
121 if (AF_INET6==family)
122 address = "0:0:0:0:0:0:0:1";
123 else
124 address = "127.0.0.1";
125
126 if (0==getaddrinfo(address,"0",&addr,&ai))
127 {
128 if (SOCK_STREAM==type)
129 ret = __stream_socketpair(ai,sock);
130 else if(SOCK_DGRAM==type)
131 ret = __dgram_socketpair(ai,sock);
132 freeaddrinfo(ai);
133 }
134 return ret;
135}
136#else
137#include <sys/socket.h>
138#endif
139
140int socket_pair(int family,int type,int protocol,socket_t sock[2])
141{
142#ifndef WIN32
143 return socketpair(family,type,protocol,sock);
144#else
145 return win32_socketpair(family,type,protocol,sock);
146#endif
147}
示例
1 //ipv4字節流套接字管道
2 ret = socket_pair(AF_INET,SOCK_STREAM,0,sock);
3 if (-1==ret) return -1;
4
5 ret = send(sock[0],"ipv4 tcp: hello sock 1\n",24,0);
6 ret = recv(sock[1],buf,sizeof(buf),0);
7 OutputDebugStringA(buf);
8
9 ret = send(sock[1],"ipv4 tcp: hello sock 0\n",24,0);
10 ret = recv(sock[0],buf,sizeof(buf),0);
11 OutputDebugStringA(buf);
12
13 //ipv4數據報套接字管道
14 ret = socket_pair(AF_INET,SOCK_DGRAM,0,sock);
15 if (-1==ret) return -1;
16
17 ret = send(sock[0],"ipv4 udp: hello sock 1\n",24,0);
18 ret = recv(sock[1],buf,sizeof(buf),0);
19 OutputDebugStringA(buf);
20
21 ret = sendto(sock[1],"ipv4 udp: hello sock 0\n",24,0,NULL,0);
22 ret = recvfrom(sock[0],buf,sizeof(buf),0,(struct sockaddr*)&r_addr,&r_len);
23 OutputDebugStringA(buf);
24
25 //ipv6字節流套接字管道
26 ret = socket_pair(AF_INET6,SOCK_STREAM,IPPROTO_TCP,sock);
27 if (-1==ret) return -1;
28
29 ret = send(sock[0],"ipv6 tcp: hello sock 1\n",24,0);
30 ret = recv(sock[1],buf,sizeof(buf),0);
31 OutputDebugStringA(buf);
32
33 ret = send(sock[1],"ipv6 tcp: hello sock 0\n",24,0);
34 ret = recv(sock[0],buf,sizeof(buf),0);
35 OutputDebugStringA(buf);
36
37 //ipv6數據報套接字管道
38 ret = socket_pair(AF_INET6,SOCK_DGRAM,IPPROTO_UDP,sock);
39 if (-1==ret) return -1;
40
41 ret = send(sock[0],"ipv6 udp: hello sock 1\n",24,0);
42 ret = recv(sock[1],buf,sizeof(buf),0);
43 OutputDebugStringA(buf);
44
45 ret = sendto(sock[1],"ipv6 udp: hello sock 0\n",24,0,NULL,0);
46 ret = recvfrom(sock[0],buf,sizeof(buf),0,NULL,0);
47 OutputDebugStringA(buf);
從上可得,對於已連接的udp套接字,可調用recv或recvfrom接收數據,調用send或sendto發送數據,但調用sendto則不能指定目標地址。測試主機需支持IPv4和IPv6雙協議棧,輸出如下:
ipv4 tcp: hello sock 1
ipv4 tcp: hello sock 0
ipv4 udp: hello sock 1
ipv4 udp: hello sock 0
ipv6 tcp: hello sock 1
ipv6 tcp: hello sock 0
ipv6 udp: hello sock 1
ipv6 udp: hello sock 0
作者: 春秋十二月