一个问题:如何创建全双工管道?
直接的办法当然是pipe两次,创建两组管道,但是有没有更简单的呢?
socketpair就可以了,man socketpair:
socketpair - create a pair of connected sockets, The two socketsareindistinguishable,也就是说,用socketpair创建出来的两个描述符应该是等价的。二.用法
#define DATA1 "test string1"
#define DATA2 "test string 2"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>main()
{
int sockets[2], child;
char buf[1024];
/* Get the socket pair */
if (socketpair(AF_UNIX, SOCK_STREAM,
0, sockets) < 0) {
printf("error %d on socketpair ", errno);
exit(1);
}
/* create child process */
if ((child = fork()) == -1) {
printf("forkerror %d ", errno);
exit(1);
}
if (child != 0) { /* this is the parent */
/* closechild's end of socket */
close(sockets[0]);
/* readmessage from child */
if(read(sockets[1], buf, sizeof(buf)) < 0) {
printf("error %d reading socket ", errno);
exit(1);
}
printf("parent:-->%s n", buf);
/* writemessage to child */
if(write(sockets[1], DATA1, sizeof(DATA1)) < 0) {
printf("error %d writing socket ", errno);
exit(1);
}
/* finished*/
close(sockets[1]);
} else { /* the child */
/* closeparent's end of socket */
close(sockets[1]);
/* sendmessage to parent */
if(write(sockets[0], DATA2, sizeof(DATA1)) < 0) {
printf("error %d writ ing socket ", errno);

exit(1);
}
/* getmessage from parent */
if(read(sockets[0], buf, sizeof(buf)) < 0) {
printf("error %d reading socket ", errno);
exit(1);
}
printf("child: -->%sn ", buf);
/* finished*/
close(sockets[0]);
}
}
从上面的代码中,我们可以发现,父进程从sockets[1]中读写,子进程从sockets[0]中读写,的确是全双工形态。
唯一值得考虑的是为什么在父子进程中,要分别关闭sockets[0]和sockets[1]呢?三。一个实现
#include<sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <stdio.h>socketpair(af, type, protocol, fd)
intaf;
inttype;
intprotocol;
intfd[2];
{
intlisten_socket;
struct sockaddr_in sin[2];
intlen;/* The following is only valid if type ==SOCK_STREAM */
if (type != SOCK_STREAM)
return -1;/* Create a temporary listen socket;temporary, so any port is good */
listen_socket = socket(af, type, protocol);
if (listen_socket < 0)
{
perror("creatinglisten_socket");
return -1;
}
sin[0].sin_family = af;
sin[0].sin_port = 0; /* Use any port number*/
sin[0].sin_addr.s_addr =inet_makeaddr(INADDR_ANY, 0);
if (bind(listen_socket, &sin[0],sizeof(sin[0])) < 0)
{
perror("bind");
return -1;
}
len = sizeof(sin[0]);/* Read the port number we got, so that ourclient can connect to it */
if (getsockname(listen_socket, &sin[0],&len) < 0)
{
perror("getsockname");
return -1;
}/* Put the listen socket in listening mode*/
if (listen(listen_socket, 5) < 0)
{
perror("listen");
return -1;
}/* Create the client socket */
fd[1] = socket(af, type, protocol);
if (fd[1] < 0)
{
perror("creatingclient_socket");
return -1;
}/* Put the client socket in non-blockingconnecting mode */
fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL, 0) |O_NDELAY);
if (connect(fd[1], &sin[0], sizeof(sin[0]))< 0)
{
perror("connect");
return -1;
}/* At the listen-side, accept the incomingconnection we generated */
len = sizeof(sin[1]);
if ((fd[0] = accept(listen_socket, &sin[1],&len)) < 0)
{
perror("accept");
return -1;
}/* Reset the client socket to blocking mode*/
fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL, 0)& ~O_NDELAY);close(listen_socket);return 0;
}
采用的是unix domainsocket的技术,首先创建一个监听socket,因为是临时性的,其绑定的端口和Ip地址都可以任意(由系统指定即可),然后执行listen;接着创建clientsocket,其描述符为fd[1],执行connect;最后返回服务端,执行accept,返回的描述符就是实际通信的描述符fd[0]。四.补充
socketpair还常用于描述符传递的处理中。