⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendfd.c

📁 目前已经有很多介绍计算机网络的书籍
💻 C
字号:
/* sendfd.c * * Send a file descriptor : */#include "common.h"/* * Send a file descriptor via socket: * * ARGUMENTS: *  s       Socket to send on *  fd      Open file descriptor to send *  addr    Ptr to UDP address or NULL *  alen    Size of addr or zero * * * RETURNS: *  0       Successful *  -1      Failed: check errno */intsend_fd(int s,int fd,void *addr,socklen_t alen) {    int z;    struct msghdr msgh;  /* Message header */    struct iovec iov[1];     /* I/O vector */    struct cmsghdr *cmsgp = NULL;    char buf[CMSG_SPACE(sizeof fd)];    int er=0;   /* "No error" code of zero */    /*     * Clear message areas :     */    memset(&msgh,0,sizeof msgh);    memset(buf,0,sizeof buf);    /*     * Supply socket address (if any) :     */    msgh.msg_name = addr;    msgh.msg_namelen = alen;    /*     * Install our I/O vector :     */    msgh.msg_iov = iov;    msgh.msg_iovlen = 1;    /*     * Initialize the I/O vector to send     * the value in "er" (which is zero).     * This is done because data must be     * transmitted to send the fd.     */    iov[0].iov_base = &er;    iov[0].iov_len = sizeof er;    /*     * Establish control buffer:     */    msgh.msg_control = buf;    msgh.msg_controllen = sizeof buf;    /*     * Configure the message to send     * a file descriptor :     */    cmsgp = CMSG_FIRSTHDR(&msgh);    cmsgp->cmsg_level = SOL_SOCKET;    cmsgp->cmsg_type = SCM_RIGHTS;    cmsgp->cmsg_len = CMSG_LEN(sizeof fd);    /*     * Install the file descriptor value :     */    *((int *)CMSG_DATA(cmsgp)) = fd;    msgh.msg_controllen = cmsgp->cmsg_len;    /*     * Send it to the client process :     */    do  {        z = sendmsg(s,&msgh,0);    } while ( z == -1 && errno == EINTR );    return z == -1 ? -1 : 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -