您的位置:首页 > 大数据 > 人工智能

socketpair的用途以及实现

2007-06-27 13:56 267 查看
一.概念及用途
一个问题:如何创建全双工管道?
直接的办法当然是pipe两次,创建两组管道,但是有没有更简单的呢?
socketpair就可以了,man socketpair:
socketpair - create a pair of connected sockets, The two sockets are indistinguishable,也就是说,用socketpair创建出来的两个描述符应该是等价的。

二.用法
#define DATA1 "test string 1"
#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("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], DATA1, sizeof(DATA1)) < 0) {
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], DATA2, sizeof(DATA1)) < 0) {
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 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("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还常用于描述符传递的处理中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: