Struct std::os::unix::net::UnixDatagram 1.10.0[−][src]
pub struct UnixDatagram(_);
Expand description
Unix 数据报套接字。
Examples
use std::os::unix::net::UnixDatagram; fn main() -> std::io::Result<()> { let socket = UnixDatagram::bind("/path/to/my/socket")?; socket.send_to(b"hello world", "/path/to/other/socket")?; let mut buf = [0; 100]; let (count, address) = socket.recv_from(&mut buf)?; println!("socket {:?} sent {:?}", address, &buf[..count]); Ok(()) }Run
Implementations
将套接字连接到指定地址。
send
方法可用于将数据发送到指定的地址。
recv
recv_from
将仅从该地址接收数据。
Examples
use std::os::unix::net::UnixDatagram; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; match sock.connect("/path/to/the/socket") { Ok(sock) => sock, Err(e) => { println!("Couldn't connect: {:?}", e); return Err(e) } }; Ok(()) }Run
为基础套接字创建一个新的独立拥有的句柄。
返回的 UnixDatagram
是与此 object 引用相同的套接字的引用。
两个句柄均可用于接受传入的连接,并且在一侧设置的选项会影响另一侧。
Examples
use std::os::unix::net::UnixDatagram; fn main() -> std::io::Result<()> { let sock = UnixDatagram::bind("/path/to/the/socket")?; let sock_copy = sock.try_clone().expect("try_clone failed"); Ok(()) }Run
从套接字接收数据。
成功后,返回读取的字节数和数据到达的地址。
Examples
use std::os::unix::net::UnixDatagram; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; let mut buf = vec![0; 10]; let (size, sender) = sock.recv_from(buf.as_mut_slice())?; println!("received {} bytes from {:?}", size, sender); Ok(()) }Run
pub fn recv_vectored_with_ancillary_from(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<(usize, bool, SocketAddr)>
[src]
pub fn recv_vectored_with_ancillary_from(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<(usize, bool, SocketAddr)>
[src]从套接字接收数据和辅助数据。
成功时,如果数据被截断,并且返回 msg 的地址,则返回读取的字节数。
Examples
#![feature(unix_socket_ancillary_data)] use std::os::unix::net::{UnixDatagram, SocketAncillary, AncillaryData}; use std::io::IoSliceMut; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; let mut buf1 = [1; 8]; let mut buf2 = [2; 16]; let mut buf3 = [3; 8]; let mut bufs = &mut [ IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2), IoSliceMut::new(&mut buf3), ][..]; let mut fds = [0; 8]; let mut ancillary_buffer = [0; 128]; let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); let (size, _truncated, sender) = sock.recv_vectored_with_ancillary_from(bufs, &mut ancillary)?; println!("received {}", size); for ancillary_result in ancillary.messages() { if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() { for fd in scm_rights { println!("receive file descriptor: {}", fd); } } } Ok(()) }Run
pub fn recv_vectored_with_ancillary(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<(usize, bool)>
[src]
pub fn recv_vectored_with_ancillary(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<(usize, bool)>
[src]从套接字接收数据和辅助数据。
成功后,返回读取的字节数以及数据是否被截断。
Examples
#![feature(unix_socket_ancillary_data)] use std::os::unix::net::{UnixDatagram, SocketAncillary, AncillaryData}; use std::io::IoSliceMut; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; let mut buf1 = [1; 8]; let mut buf2 = [2; 16]; let mut buf3 = [3; 8]; let mut bufs = &mut [ IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2), IoSliceMut::new(&mut buf3), ][..]; let mut fds = [0; 8]; let mut ancillary_buffer = [0; 128]; let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); let (size, _truncated) = sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?; println!("received {}", size); for ancillary_result in ancillary.messages() { if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() { for fd in scm_rights { println!("receive file descriptor: {}", fd); } } } Ok(()) }Run
将套接字上的数据发送到套接字的对等方。
可以通过 connect
方法设置对等地址,如果尚未连接套接字,则此方法将返回错误。
成功时,返回写入的字节数。
Examples
use std::os::unix::net::UnixDatagram; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; sock.connect("/some/sock").expect("Couldn't connect"); sock.send(b"omelette au fromage").expect("send_to function failed"); Ok(()) }Run
pub fn send_vectored_with_ancillary_to<P: AsRef<Path>>(
&self,
bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>,
path: P
) -> Result<usize>
[src]
pub fn send_vectored_with_ancillary_to<P: AsRef<Path>>(
&self,
bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>,
path: P
) -> Result<usize>
[src]将套接字上的数据和辅助数据发送到指定地址。
成功时,返回写入的字节数。
Examples
#![feature(unix_socket_ancillary_data)] use std::os::unix::net::{UnixDatagram, SocketAncillary}; use std::io::IoSlice; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; let buf1 = [1; 8]; let buf2 = [2; 16]; let buf3 = [3; 8]; let bufs = &[ IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3), ][..]; let fds = [0, 1, 2]; let mut ancillary_buffer = [0; 128]; let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); ancillary.add_fds(&fds[..]); sock.send_vectored_with_ancillary_to(bufs, &mut ancillary, "/some/sock") .expect("send_vectored_with_ancillary_to function failed"); Ok(()) }Run
pub fn send_vectored_with_ancillary(
&self,
bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<usize>
[src]
pub fn send_vectored_with_ancillary(
&self,
bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<usize>
[src]在套接字上发送数据和辅助数据。
成功时,返回写入的字节数。
Examples
#![feature(unix_socket_ancillary_data)] use std::os::unix::net::{UnixDatagram, SocketAncillary}; use std::io::IoSlice; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; let buf1 = [1; 8]; let buf2 = [2; 16]; let buf3 = [3; 8]; let bufs = &[ IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3), ][..]; let fds = [0, 1, 2]; let mut ancillary_buffer = [0; 128]; let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); ancillary.add_fds(&fds[..]); sock.send_vectored_with_ancillary(bufs, &mut ancillary) .expect("send_vectored_with_ancillary function failed"); Ok(()) }Run
设置套接字的读取超时。
如果提供的值为 None
,则 recv
和 recv_from
调用将无限期阻塞。
如果将零 Duration
传递给此方法,则返回 Err
。
Examples
use std::os::unix::net::UnixDatagram; use std::time::Duration; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; sock.set_read_timeout(Some(Duration::new(1, 0))) .expect("set_read_timeout function failed"); Ok(()) }Run
use std::io; use std::os::unix::net::UnixDatagram; use std::time::Duration; fn main() -> std::io::Result<()> { let socket = UnixDatagram::unbound()?; let result = socket.set_read_timeout(Some(Duration::new(0, 0))); let err = result.unwrap_err(); assert_eq!(err.kind(), io::ErrorKind::InvalidInput); Ok(()) }Run
设置套接字的写超时。
如果提供的值为 None
,则 send
和 send_to
调用将无限期阻塞。
如果将零 Duration
传递给此方法,则返回 Err
。
Examples
use std::os::unix::net::UnixDatagram; use std::time::Duration; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; sock.set_write_timeout(Some(Duration::new(1, 0))) .expect("set_write_timeout function failed"); Ok(()) }Run
use std::io; use std::os::unix::net::UnixDatagram; use std::time::Duration; fn main() -> std::io::Result<()> { let socket = UnixDatagram::unbound()?; let result = socket.set_write_timeout(Some(Duration::new(0, 0))); let err = result.unwrap_err(); assert_eq!(err.kind(), io::ErrorKind::InvalidInput); Ok(()) }Run
返回此套接字的读取超时。
Examples
use std::os::unix::net::UnixDatagram; use std::time::Duration; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; sock.set_read_timeout(Some(Duration::new(1, 0))) .expect("set_read_timeout function failed"); assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0))); Ok(()) }Run
返回此套接字的写入超时。
Examples
use std::os::unix::net::UnixDatagram; use std::time::Duration; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; sock.set_write_timeout(Some(Duration::new(1, 0))) .expect("set_write_timeout function failed"); assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0))); Ok(()) }Run
移动套接字以将 unix 凭据作为 SocketAncillary
中的控制消息传递。
设置套接字选项 SO_PASSCRED
。
Examples
#![feature(unix_socket_ancillary_data)] use std::os::unix::net::UnixDatagram; fn main() -> std::io::Result<()> { let sock = UnixDatagram::unbound()?; sock.set_passcred(true).expect("set_passcred function failed"); Ok(()) }Run
获取用于在 SocketAncillary
中传递 unix 凭据的套接字的当前值。
可以通过 set_passcred
更改此值。
获取套接字选项 SO_PASSCRED
。
从套接字所连接的远程地址接收套接字上的数据,而无需从队列中删除该数据。
成功时,返回偷看的字节数。
连续调用返回相同的数据。
这是通过将 MSG_PEEK
作为标志传递到基础 recv
系统调用来完成的。
Examples
#![feature(unix_socket_peek)] use std::os::unix::net::UnixDatagram; fn main() -> std::io::Result<()> { let socket = UnixDatagram::bind("/tmp/sock")?; let mut buf = [0; 10]; let len = socket.peek(&mut buf).expect("peek failed"); Ok(()) }Run
在套接字上接收单个数据报消息,而无需将其从队列中删除。 成功时,返回读取的字节数和源。
必须使用足够大的有效字节数组 buf
来调用函数,以容纳消息字节。
如果消息太长而无法容纳在提供的缓冲区中,则多余的字节可能会被丢弃。
连续调用返回相同的数据。
这是通过将 MSG_PEEK
作为标志传递到基础 recvfrom
系统调用来完成的。
不要使用此函数来实现繁忙等待,而应使用 libc::poll
来同步一个或多个套接字上的 IO 事件。
Examples
#![feature(unix_socket_peek)] use std::os::unix::net::UnixDatagram; fn main() -> std::io::Result<()> { let socket = UnixDatagram::bind("/tmp/sock")?; let mut buf = [0; 10]; let (len, addr) = socket.peek_from(&mut buf).expect("peek failed"); Ok(()) }Run