当前位置: 技术问答>linux和unix
200分相送,高手帮忙看一下,万分感谢,祝你国庆快乐!!! (2)
来源: 互联网 发布时间:2015-04-26
本文导语: void unix_send_fd(int unix_fd,int conn_fd) /*发送文件描述符*/ { struct msghdr msg; struct iovec iov[1]; char c; int ret; union { struct cmsghdr cm; char control[16]; }control_un; struct cmsghdr *cmptr; msg.msg_name=NULL; msg.msg_namelen=...
void unix_send_fd(int unix_fd,int conn_fd) /*发送文件描述符*/
{ struct msghdr msg;
struct iovec iov[1];
char c;
int ret;
union
{ struct cmsghdr cm;
char control[16];
}control_un;
struct cmsghdr *cmptr;
msg.msg_name=NULL;
msg.msg_namelen=0;
iov[0].iov_base=&
iov[0].iov_len=1;
msg.msg_iov=iov;
msg.msg_iovlen=1;
msg.msg_control=control_un.control;
msg.msg_controllen=sizeof(control_un.control);
cmptr=CMSG_FIRSTHDR(&msg);
cmptr->cmsg_len=16;
cmptr->cmsg_level=SOL_SOCKET;
cmptr->cmsg_type=SCM_RIGHTS;
*(int *)CMSG_DATA(cmptr)=conn_fd;
printf("unix_fd=%d,conn_fd=%d",unix_fd,conn_fd);
ret=sendmsg(unix_fd,&msg,0);
if(ret
{ struct msghdr msg;
struct iovec iov[1];
char c;
int ret;
union
{ struct cmsghdr cm;
char control[16];
}control_un;
struct cmsghdr *cmptr;
msg.msg_name=NULL;
msg.msg_namelen=0;
iov[0].iov_base=&
iov[0].iov_len=1;
msg.msg_iov=iov;
msg.msg_iovlen=1;
msg.msg_control=control_un.control;
msg.msg_controllen=sizeof(control_un.control);
cmptr=CMSG_FIRSTHDR(&msg);
cmptr->cmsg_len=16;
cmptr->cmsg_level=SOL_SOCKET;
cmptr->cmsg_type=SCM_RIGHTS;
*(int *)CMSG_DATA(cmptr)=conn_fd;
printf("unix_fd=%d,conn_fd=%d",unix_fd,conn_fd);
ret=sendmsg(unix_fd,&msg,0);
if(ret