Function sendmsg

Source
pub fn sendmsg<S>(
    fd: RawFd,
    iov: &[IoSlice<'_>],
    cmsgs: &[ControlMessage<'_>],
    flags: MsgFlags,
    addr: Option<&S>,
) -> Result<usize>
where S: SockaddrLike,
Available on crate features socket and uio only.
Expand description

Send data in scatter-gather vectors to a socket, possibly accompanied by ancillary data. Optionally direct the message at the given address, as with sendto.

Allocates if cmsgs is nonempty.

ยงExamples

When not directing to any specific address, use () for the generic type

let (fd1, fd2) = socketpair(AddressFamily::Unix, SockType::Stream, None,
    SockFlag::empty())
    .unwrap();
let (r, w) = pipe().unwrap();

let iov = [IoSlice::new(b"hello")];
let fds = [r.as_raw_fd()];
let cmsg = ControlMessage::ScmRights(&fds);
sendmsg::<()>(fd1.as_raw_fd(), &iov, &[cmsg], MsgFlags::empty(), None).unwrap();

When directing to a specific address, the generic type will be inferred. Note that SCM_RIGHTS ancillary data are valid only for AF_UNIX sockets on Solaris.

let localhost = SockaddrIn::from_str("1.2.3.4:8080").unwrap();
let fd = socket(AddressFamily::Inet, SockType::Datagram, SockFlag::empty(),
    None).unwrap();
let (r, w) = pipe().unwrap();

let iov = [IoSlice::new(b"hello")];
let fds = [r.as_raw_fd()];
let cmsg = ControlMessage::ScmRights(&fds);
#[cfg(not(target_os = "solaris"))]
sendmsg(fd.as_raw_fd(), &iov, &[cmsg], MsgFlags::empty(), Some(&localhost)).unwrap();