UNIX域协议(无名套接字)

时间:2023-12-28 21:54:14

关于什么是UNIX域套接字可以参考:http://www.cnblogs.com/xcywt/p/8185597.html
这里主要介绍非命名的UNIX域套接字的用法。

1.socketpair函数
先看man手册:

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

int socketpair(int domain, int type, int protocol, int sv[2]);

DESCRIPTION
       The  socketpair()  call creates an unnamed pair of connected sockets in
       the specified domain, of the specified type, and using  the  optionally
       specified  protocol.   For  further  details  of  these  arguments, see
       socket(2).

The descriptors used in referencing the new  sockets  are  returned  in
       sv[0] and sv[1].  The two sockets are indistinguishable.

RETURN VALUE
       On  success,  zero is returned.  On error, -1 is returned, and errno is
       set appropriately.

功能:创建一个全双工的流管道
参数:
domain:协议家族,为AF_LOCAL或AF_UNIX
type:套接字类型。可以是SOCK_STREAM或者SOCK_DGRAM。这两种都是可靠的
protocol:协议类型。为0
sv:返回套接字对,这个是输出参数。返回的两个描述符都是可读可写的。
返回值:成功返回0,失败返回-1.
补充:pipe创建的匿名管道的半双工的,pipefd[0]用于读,pipefd[1]用于写。
注意:由于创建的每个套接字都是没有名字的,这就意味着无关进程不能使用它们。

2.一个简单的例子:
父进程给子进程发送一个数据给子进程,子进程收到数据后最数据进行加一操作,再发回给父进程。
先上自己的一个头文件:
comm.h

#ifndef __COMM_H__
#define __COMM_H__ #include<errno.h>
#include<stdlib.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while() #endif // __COMM_H__

再上主程序:

#include<stdio.h>
#include<unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h> #include"comm.h" int main()
{
int sockfds[];
if(socketpair(PF_UNIX, SOCK_STREAM, , sockfds) < )
ERR_EXIT("sockerpair"); pid_t pid;
pid = fork();
if(pid < )
ERR_EXIT("fork");
else if(pid > )
{
int val = ;
close(sockfds[]);
while()
{
++val;
printf("Sending data: %d\n", val);
write(sockfds[], &val, sizeof(val));
read(sockfds[], &val, sizeof(val));
printf(" Recv data: %d\n", val);
sleep();
}
}
else
{
close(sockfds[]);
int val;
while()
{
read(sockfds[], &val, sizeof(val));
++val;
write(sockfds[], &val, sizeof(val));
}
}
return ;
}

运行:

xcy@xcy-virtual-machine:~/test/sock14_socketpair$ ./sockpair
Sending data:
Recv data:
Sending data:
Recv data:
Sending data:
Recv data:
Sending data:
Recv data:
Sending data:
Recv data:
Sending data:
Recv data:
Sending data:
Recv data:

3.UNIX域传递文件描述符

需要用到sendmsg和recvmsg函数:

#include <sys/types.h>
       #include <sys/socket.h>
       ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
       ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);

主要封装了两个函数,recv_fd和send_fd。
直接上代码:

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/socket.h>
#include"comm.h" // 这个文件见上面的例子有 void send_fd(int sock_fd, int send_fd)
{
int ret = ;
struct msghdr msg;
struct cmsghdr *p_cmsg;
struct iovec vec;
char cmsgbuf[CMSG_SPACE(sizeof(send_fd))];
int *p_fds;
char sendchar = ;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf); p_cmsg = CMSG_FIRSTHDR(&msg);
p_cmsg->cmsg_level = SOL_SOCKET;
p_cmsg->cmsg_type = SCM_RIGHTS;
p_cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
p_fds = (int*)CMSG_DATA(p_cmsg);
*p_fds = send_fd; msg.msg_name = NULL;
msg.msg_namelen = ;
msg.msg_iov = &vec;
msg.msg_iovlen = ;
msg.msg_flags = ; vec.iov_base = &sendchar;
vec.iov_len = sizeof(sendchar);
ret = sendmsg(sock_fd, &msg, );
if(ret < )
ERR_EXIT("sendmsg");
}
int recv_fd(const int sock_fd)
{
int ret = ;
struct msghdr msg;
char recvchar;
struct iovec vec;
int recv_fd;
char cmsgbuf[CMSG_SPACE(sizeof(recv_fd))];
struct cmsghdr *p_cmsg;
int *p_fd = NULL;
vec.iov_base = &recvchar;
vec.iov_len = sizeof(recvchar);
msg.msg_name = NULL;
msg.msg_namelen = ;
msg.msg_iov = &vec;
msg.msg_iovlen = ;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
msg.msg_flags = ; p_fd = (int *)CMSG_DATA(CMSG_FIRSTHDR(&msg));
*p_fd = -;
ret = recvmsg(sock_fd, &msg, );
if(ret < )
ERR_EXIT("recvmsg"); p_cmsg = CMSG_FIRSTHDR(&msg);
if(p_cmsg == NULL)
ERR_EXIT("no passed fd"); p_fd = (int*)CMSG_DATA(p_cmsg);
recv_fd = *p_fd;
if(recv_fd == -)
ERR_EXIT("no passed fd"); return recv_fd;
} #define OPEN_FILE "test.txt" int main()
{
int sockfds[];
if(socketpair(PF_UNIX, SOCK_STREAM, , sockfds) < )
ERR_EXIT("socketpair");
pid_t pid = fork();
if(pid < )
ERR_EXIT("fork");
else if(pid > )
{
close(sockfds[]);
char buf[] = {};
int fd = recv_fd(sockfds[]);
read(fd, buf, sizeof(buf));
printf("Get Data:\n%s", buf);
}
else
{
close(sockfds[]);
int fd = open(OPEN_FILE, O_RDONLY);
if(fd < )
ERR_EXIT("open");
send_fd(sockfds[], fd);
}
return ;
}

分析:子进程中打开一个文件描述符,然后通过send_fd发送给主进程,主进程通过recv_fd接收这个文件描述符。再通过这个文件描述符读取文件的内容。
运行,需要先创建一个test.txt文件,再运行:

xcy@xcy-virtual-machine:~/test/sock14_socketpair$ ./sendfd
Get Data:
I am xiaochongyong
How are you?
I'm fine. And you!
xcy@xcy-virtual-machine:~/test/sock14_socketpair$

补充:
1)虽然文件描述符是一个整形数字,但是单纯发一个数字过去是没有用的。
2)普通的TCP UDP套接字是不能传递文件描述符的