一.概念及用途
一个问题:如何创建全双工管道?
直接的办法当然是pipe两次,创建两组管道,但是有没有更简单的呢?
socketpair就可以了,man socketpair:
socketpair - create a pair of connected sockets, The two sockets are indistinguishable,也就是说,用socketpair创建出来的两个描述符应该是等价的。
二.用法
#define DA
#define DA
#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("fork error %d ", errno);
exit(1);
}
if (child != 0) { /* this is the parent */
/* close child's end of socket */
close( sockets[0] );
/* read message from child */
if ( read(sockets[1], buf, sizeof(buf) ) < 0) {
printf("error %d reading socket ", errno);
exit(1);
}
printf("parent:-->%s \n", buf);
/* write message to child */
if ( write(sockets[1], DA
printf("error %d writing socket ", errno);
exit(1);
}
/* finished */
close(sockets[1]);
}
else
{ /* the child */
/* close parent's end of socket */
close(sockets[1]);
/* send message to parent */
if (write(sockets[0], DA
printf("error %d writing socket ", errno);
exit(1);
}
/* get message from parent */
if (read(sockets[0], buf, sizeof(buf)) < 0) {
printf("error %d reading socket ", errno);
exit(1);
}
printf("child: -->%s\n ", 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)
int af;
int type;
int protocol;
int fd[2];
{
int listen_socket;
struct sockaddr_in sin[2];
int len;
/* The following is on
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("creating listen_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 our client 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("creating client_socket");
return -1;
}
/* Put the client socket in non-blocking connecting 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 incoming connection 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 domain socket的技术,首先创建一个监听socket,因为是临时性的,其绑定的端口和Ip地址都可以任意(由系统指定即可), 然后执行listen;接着创建client socket,其描述符为fd[1],执行connect;最后返回服务端,执行accept,返回的描述符就是实际通信的描述符fd[0]。
四.补充
socketpair还常用于描述符传递的处理中。
我们知道父进程在子进程被fork出来之前打开的文件描述符是能被子进程继承下来的,但是一旦子进程已经创建后,父进程打开的文件描述符要怎样才能传递给子进程呢?Unix提供相应的技术来满足这一需求,这就是同一台主机上进程间的文件描述符传递,很美妙而且强大的技术。
想象一下我们试图实现一个服务器,接收多个客户端的连接,我们欲采用多个子进程并发的形式来处理多客户端的同时连接,这时候我们可能有两种想法:
1、客户端每建立一条连接,我们fork出一个子进程负责处理该连接;
2、预先创建一个进程池,客户端每建立一条链接,服务器就从该池中选出一个空闲(Idle)子进程来处理该连接。
后者显然更高效,因为减少了子进程创建的性能损耗,反应的及时性大大增强。这里恰恰就出现了我们前面提到的问题,所有子进程都是在服务器Listen到一条连接以前就已经fork出来了,也就是说新的连接描述符子进程是不知道的,需要父进程传递给它,它接收到相应的连接描述符后,才能与相应的客户端进行通信处理。这里我们就可以使用'传递文件描述符'的方式来实现。
本文介绍了如何使用socketpair创建全双工管道,通过示例代码展示了父进程和子进程间的数据交换过程,并探讨了文件描述符传递的方法。
5349

被折叠的 条评论
为什么被折叠?



