Function nix::sys::socket::sendmsg

source ·
pub fn sendmsg<S>(
    fd: RawFd,
    iov: &[IoSlice<'_>],
    cmsgs: &[ControlMessage<'_>],
    flags: MsgFlags,
    addr: Option<&S>
) -> Result<usize>
where S: SockaddrLike,
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];
let cmsg = ControlMessage::ScmRights(&fds);
sendmsg::<()>(fd1, &iov, &[cmsg], MsgFlags::empty(), None).unwrap();

When directing to a specific address, the generic type will be inferred.

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];
let cmsg = ControlMessage::ScmRights(&fds);
sendmsg(fd, &iov, &[cmsg], MsgFlags::empty(), Some(&localhost)).unwrap();