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,而端口由系统分配。这里的实现具有以下特点:
● unix仅支持af_unix或af_local地址族,windows仅支持af_init和af_init6地址族。
● 仅限于进程内或父子进程间通信,需文件系统路径名的机制以支持无关进程间的通信。
● unix使用它作进程间通信比标准套接字高效。
接口
socketpair创建成功返回0,否则返回-1。
1#ifdef WIN32
2#include <winsock2.h>
3#pragma comment(lib,"ws2_32.lib")
4#endif
5
6#ifdef WIN32
7typedef SOCKET socket_t;
8int socketpair(int family,int type,int protocol,SOCKET sock[2]);
9#else
10typedef int socket_t;
11#include <sys/socket.h>
12#endif
实现
字节流类型由socketpair_stream实现,数据报类型由socketpair_dgram实现。
1#ifdef WIN32
2#include <ws2tcpip.h>
3
4static int socketpair_stream(struct addrinfo* ai,SOCKET 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 socketpair_dgram(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
110int socketpair(int family,int type,int protocol,SOCKET 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 = socketpair_stream(ai,sock);
130 else if(SOCK_DGRAM==type)
131 ret = socketpair_dgram(ai,sock);
132 freeaddrinfo(ai);
133 }
134 return ret;
135}
138#endif
应用
1 //ipv4字节流套接字管道
2 ret = socketpair(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 = socketpair(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 = socketpair(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 = socketpair(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
posted on 2012-06-17 03:02
春秋十二月 阅读(2945)
评论(3) 编辑 收藏 引用 所属分类:
Network