Linux Network programming socket (16)

Source: Internet
Author: User
Tags data structures sendmsg socket unix domain socket linux

Pass descriptor and SENDMSG/RECVMSG functions through UNIX domain sockets

In the front we introduced UNIX domain socket programming, and more importantly UNIX domain sockets can pass file descriptors between processes on the same host.

Let's take a look at two functions:

#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);

They are similar to the SendTo and recvfrom functions, but can transmit more complex data structures, not only to transmit general data, but also to transmit additional data, i.e. file descriptors. Here's a look at the structural body MSGHDR:

struct MSGHDR {

void *msg_name; /* Optional Address * *

Socklen_t Msg_namelen; /* Size of address * *

struct Iovec *msg_iov; /* Scatter/gather Array * *

size_t Msg_iovlen; * * Elements in Msg_iov * *

void *msg_control; /* Ancillary data, below * *

size_t Msg_controllen; /* Ancillary Data buffer len * *

int msg_flags; * Flags on received message * *

};

As shown in the following illustration:

1, Msg_name: The peer's address pointer, do not care when set to null;

2, Msg_namelen: address length, do not care when set to 0;

3, Msg_iov: Is the structure body Iovec the pointer.

struct Iovec {

void *iov_base; /* Starting Address * *

size_t Iov_len; /* Number of bytes to transfer * *

};

Member Iov_base can be considered to be the size of the BUF when the normal data is transmitted Buf,iov_len.

4, Msg_iovlen: When there are n Iovec structure body, this value is n;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.