socketpair的用途以及实现
来源:互联网 发布:crf 算法 特征模板 编辑:程序博客网 时间:2024/05/17 23:12
一个问题:如何创建全双工管道?
直接的办法当然是pipe两次,创建两组管道,但是有没有更简单的呢?
socketpair就可以了,man socketpair:
socketpair - create a pair of connected sockets, The two sockets are indistinguishable,也就是说,用socketpair创建出来的两个描述符应该是等价的。
socketpair函数概要如下:
#include <sys/types.h>
#include <sys/socket.h>
int socketpair(int domain, int type, int protocol, int sv[2]);
sys/types.h文件需要用来定义一些C宏常量。sys/socket.h文件必须包含进来定义socketpair函数原型。
socketpair函数需要四个参数。他们是:
套接口的域
类型参数声明了我们希望创建哪种类型的套接口。socketpair函数的选择如下:
SOCK_STREAM
SOCK_DGRAM
对于socketpair函数,protocol参数必须提供为0。
参数sv[2]是接收代表两个套接口的整数数组。每一个文件描述符代表一个套接口,并且与另一个并没有区别。
如果函数成功,将会返回0值。否则将会返回-1表明创建失败,并且errno来表明特定的错误号。
关于流程。socketpair()函数创建出两个进程,fork()之后这两个进程都会执行主程序中的代码,这个一定要注意!尤其是bind的时候,如果bind两次的话,那就会出错了。一般会在子进程里调用一个带死循环的函数,这样就好了。(这个情况的例子会在综合运用中讲解)
二.用法
- #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()
- {
-
sockets[2], child; -
buf[1024]; -
-
(socketpair(AF_UNIX, SOCK_STREAM, -
0, sockets) < 0) { -
printf( %d ,on socketpair " errno); -
exit(1); -
} -
- if
((child = fork()) == -1) { -
printf( error ,%d " errno); -
exit(1); -
} -
(child != 0) { -
-
close(sockets[0]); -
-
(read(sockets[1], sizeof(buf))buf, < 0) { -
printf( %d ,reading socket " errno); -
exit(1); -
} -
printf( /n" ,buf); -
-
(write(sockets[1], sizeof(DATA1))DATA1, < 0) { -
printf( %d ,writing socket " errno); -
exit(1); -
} -
-
close(sockets[1]); -
} else{ -
-
close(sockets[1]); -
-
(write(sockets[0], sizeof(DATA1))DATA2, < 0) { -
printf( %d ,writing socket " errno); -
exit(1); -
} -
-
(read(sockets[0], sizeof(buf))buf, < 0) { -
printf( %d ,reading socket " errno); -
exit(1); -
} -
printf( -->%s/n ," buf); -
-
close(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; -
-
if (type != SOCK_STREAM) -
-1; -
-
listen_socket = socket(af, type, protocol); -
if (listen_socket < 0) -
{ -
perror( listen_socket" ); -
-1; -
} -
sin[0].sin_family = af; -
sin[0].sin_port = 0; -
sin[0].sin_addr.s_addr = inet_makeaddr(INADDR_ANY, 0); -
if (bind(listen_socket, sizeof(sin[0]))&sin[0], < 0) -
{ -
perror( -
-1; -
} -
len sizeof(sin[0]);= -
-
if (getsockname(listen_socket, &sin[0], &len) < 0) -
{ -
perror( -
-1; -
} -
-
if (listen(listen_socket, 5) < 0) -
{ -
perror( -
-1; -
} -
-
fd[1] = socket(af, type, protocol); -
if (fd[1] < 0) -
{ -
perror( client_socket" ); -
-1; -
} -
-
fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL, 0) | O_NDELAY); -
if (connect(fd[1], sizeof(sin[0]))&sin[0], < 0) -
{ -
perror( -
-1; -
} -
-
len sizeof(sin[1]);= -
if ((fd[0] = accept(listen_socket, &sin[1], &len)) < 0) -
{ -
perror( -
-1; -
} -
-
fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL, 0) & ~O_NDELAY); -
close(listen_socket); -
return 0; -
}
我们知道父进程在子进程被fork出来之前打开的文件描述符是能被子进程继承下来的,但是一旦子进程已经创建后,父进程打开的文件描述符要怎样才能传递给子进程呢?Unix提供相应的技术来满足这一需求,这就是同一台主机上进程间的文件描述符传递,很美妙而且强大的技术。
想象一下我们试图实现一个服务器,接收多个客户端的连接,我们欲采用多个子进程并发的形式来处理多客户端的同时连接,这时候我们可能有两种想法:
1、客户端每建立一条连接,我们fork出一个子进程负责处理该连接;
2、预先创建一个进程池,客户端每建立一条链接,服务器就从该池中选出一个空闲(Idle)子进程来处理该连接。
后者显然更高效,因为减少了子进程创建的性能损耗,反应的及时性大大增强。这里恰恰就出现了我们前面提到的问题,所有子进程都是在服务器Listen到一条连接以前就已经fork出来了,也就是说新的连接描述符子进程是不知道的,需要父进程传递给它,它接收到相应的连接描述符后,才能与相应的客户端进行通信处理。这里我们就可以使用'传递文件描述符'的方式来实现。
在'UNIX网络编程第1卷'的14.7小节中对这种技术有详细的阐述,实际上这种技术就是利用sendmsg和recvmsg在一定的UNIX域套接口(或者是某种管道)上发送和接收一种特殊的消息,这种消息可以承载'文件描述符'罢了,当然操作系统内核对这种消息作了特殊的处理。在具体一点儿'文件描述符'是作为辅助数据(Ancillary Data)通过msghdr结构中的成员msg_control(老版本中称为msg_accrights)发送和接收的。值得一提的是发送进程在将'文件描述符'发送出去后,即使立即关闭该文件描述符,该文件描述符对应的文件设备也没有被真正的关闭,其引用计数仍然大于一,直到接收进程成功接收后,再关闭该文件描述符,如果这时文件设备的引用计数为0,那么才真正关闭该文件设备。
OK,下面是一个简单的文件描述符传递的例子,该例子实现这样一个功能:即子进程负责在父进程传递给它的文件描述符对应的文件尾加上特定的'LOGO'字符串。例子环境为Solaris 9 + GCC 3.2
-
- #include
<stdio.h> - #include
<string.h> - #include
<stdlib.h> -
- #include
<unistd.h> - #include
<sys/wait.h> - #include
<sys/types.h> - #include
<sys/stat.h> - #include
<fcntl.h> - #include
<errno.h> -
- #include
<sys/socket.h> -
- #define
MY_LOGO "-- Tony Bai" -
- static
int send_fd( intfd, intfd_to_send) - {
-
iovec iov[1]; -
msghdr msg; -
buf[1]; -
-
(fd_to_send >= 0) { -
msg.msg_accrights = (caddr_t)&fd_to_send; -
msg.msg_accrightslen = -
} { -
msg.msg_accrights = (caddr_t)NULL; -
msg.msg_accrightslen = 0; -
} -
-
msg.msg_name = NULL; -
msg.msg_namelen = 0; -
-
iov[0].iov_base = buf; -
iov[0].iov_len = 1; -
msg.msg_iov = iov; -
msg.msg_iovlen = 1; -
-
&msg, 0) < 0) { -
printf( error, ,errno is %d\n" errno); -
errno; -
} -
-
0; - }
-
- static
int recv_fd( intfd, int*fd_to_recv) - {
-
iovec iov[1]; -
msghdr msg; -
buf[1]; -
-
msg.msg_accrights = (caddr_t)fd_to_recv; -
msg.msg_accrightslen = -
-
msg.msg_name = NULL; -
msg.msg_namelen = 0; -
-
iov[0].iov_base = buf; -
iov[0].iov_len = 1; -
msg.msg_iov = iov; -
msg.msg_iovlen = 1; -
-
(recvmsg(fd, &msg, 0) < 0) { -
errno; -
} -
-
!= sizeof(int)){ -
*fd_to_recv = -1; -
} -
-
0; - }
-
- int
x_sock_set_block( intsock, inton) - {
-
val; -
rv; -
-
val = fcntl(sock, F_GETFL, 0); -
(on) { -
rv = fcntl(sock, F_SETFL, ~O_NONBLOCK&val); -
} { -
rv = fcntl(sock, F_SETFL, O_NONBLOCK|val); -
} -
-
(rv) { -
errno; -
} -
-
0; - }
-
- int
main() { -
pid_t pid; -
sockpair[2]; -
rv; -
fname[256]; -
fd; -
-
rv = socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair); -
(rv < 0) { -
printf( socketpair ,error, errno is %d\n" errno); -
errno; -
} -
-
pid = fork(); -
(pid == 0) { -
-
close(sockpair[1]); -
-
( ; ; ) { -
rv = x_sock_set_block(sockpair[0], 1); -
(rv != 0) { -
printf( x_sock_set_block ,error, errno is %d\n" rv); -
-
} -
-
rv = recv_fd(sockpair[0], &fd); -
(rv < 0) { -
printf( recv_fd ,error, errno is %d\n" rv); -
-
} -
-
(fd < 0) { -
printf( child );process exit normally!\n" -
-
} -
-
-
rv = write(fd, MY_LOGO, strlen(MY_LOGO)); -
(rv < 0) { -
printf( write ,error, errno is %d\n" rv); -
} { -
printf( append );logo successfully\n" -
} -
close(fd); -
} -
-
exit(0); -
} -
-
-
( ; ; ) { -
memset(fname, 0, -
printf( please );enter filename:\n" -
scanf( fname); -
-
(strcmp(fname, "exit")== 0) { -
rv = send_fd(sockpair[1], -1); -
(rv < 0) { -
printf( send_fd ,error, errno is %d\n" rv); -
} -
-
} -
-
fd = open(fname, O_RDWR | O_APPEND); -
(fd < 0) { -
(errno == ENOENT) { -
printf( can't ,find file '%s'\n" fname); -
-
} -
printf( open ,file error, errno is %d\n" errno); -
} -
-
rv = send_fd(sockpair[1], fd); -
(rv != 0) { -
printf( send_fd ,error, errno is %d\n" rv); -
} -
-
close(fd); -
} -
-
wait(NULL); -
0; - }
编译:gcc -o test_fdpass -lsocket -lnsl test_fdpass.c
执行:test_fdpass(事先在同一目录下创建一个文件kk.log)
[PARENT]: please enter filename:
kk.log
[CHILD]: append logo successfully
[PARENT]: please enter filename:
cc.log
[PARENT]: can't find file 'cc.log'
exit
[CHILD]: child process exit normally!
关于文件描述符传递的更多细节, W. Richard Stevens的'UNIX网络编程第1卷'和'UNIX环境高级编程'两本书中都有详细说明,参读即可
- socketpair的用途以及实现
- socketpair的用途以及实现
- socketpair的用途以及实现
- 双工pipe的实现: socketpair的用途以及实现
- parse_url的用法,以及用途
- socketpair的使用
- socketpair的使用
- socketpair的使用
- socketpair的使用
- socketpair的使用
- socketpair的使用
- socketpair的问题
- socketpair的用法
- socketpair的使用
- socketpair的使用
- socketpair
- socketpair
- socketpair
- Servlet/ServletContextListener/ServletContextEvent 说明及使用
- CSS3-JS-小练习
- CentOS安装Code::Blocks
- SOAP有意义的一晚 wsimport
- leetcode-242-Valid Anagram
- socketpair的用途以及实现
- hdu2159 Fate(二维费用背包)
- 链式栈的实现
- Ubuntu16.04安装MySQL5.7
- thinlphp 中 Foreach标签相对比volist标签
- GitHub
- leetcode-217-Contains Duplicate
- 自定义表盘
- 集中式vs分布式