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(())
}
RunImplementations§
source§impl UnixDatagram
impl UnixDatagram
1.70.0 · sourcepub fn bind_addr(socket_addr: &SocketAddr) -> Result<UnixDatagram>
pub fn bind_addr(socket_addr: &SocketAddr) -> Result<UnixDatagram>
创建绑定到地址的 Unix 数据报套接字。
Examples
use std::os::unix::net::{UnixDatagram};
fn main() -> std::io::Result<()> {
let sock1 = UnixDatagram::bind("path/to/socket")?;
let addr = sock1.local_addr()?;
let sock2 = match UnixDatagram::bind_addr(&addr) {
Ok(sock) => sock,
Err(err) => {
println!("Couldn't bind: {err:?}");
return Err(err);
}
};
Ok(())
}
Runsourcepub fn unbound() -> Result<UnixDatagram>
pub fn unbound() -> Result<UnixDatagram>
sourcepub fn pair() -> Result<(UnixDatagram, UnixDatagram)>
pub fn pair() -> Result<(UnixDatagram, UnixDatagram)>
sourcepub fn connect<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn connect<P: AsRef<Path>>(&self, path: P) -> Result<()>
将套接字连接到指定的路径地址。
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(())
}
Run1.70.0 · sourcepub fn connect_addr(&self, socket_addr: &SocketAddr) -> Result<()>
pub fn connect_addr(&self, socket_addr: &SocketAddr) -> Result<()>
将套接字连接到一个地址。
Examples
use std::os::unix::net::{UnixDatagram};
fn main() -> std::io::Result<()> {
let bound = UnixDatagram::bind("/path/to/socket")?;
let addr = bound.local_addr()?;
let sock = UnixDatagram::unbound()?;
match sock.connect_addr(&addr) {
Ok(sock) => sock,
Err(e) => {
println!("Couldn't connect: {e:?}");
return Err(e)
}
};
Ok(())
}
Runsourcepub fn try_clone(&self) -> Result<UnixDatagram>
pub fn try_clone(&self) -> Result<UnixDatagram>
为底层套接字创建一个新的独立的拥有所有权的句柄。
返回的 UnixDatagram
是与此对象引用相同的套接字的引用。
两个句柄均可用于接受传入的连接,并且在一侧设置的选项会影响另一侧。
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(())
}
Runsourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
sourcepub fn recv_vectored_with_ancillary_from(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<(usize, bool, SocketAddr)>
🔬This is a nightly-only experimental API. (unix_socket_ancillary_data
#76915)
pub fn recv_vectored_with_ancillary_from( &self, bufs: &mut [IoSliceMut<'_>], ancillary: &mut SocketAncillary<'_> ) -> Result<(usize, bool, SocketAddr)>
unix_socket_ancillary_data
#76915)从套接字接收数据和辅助数据。
成功时,如果数据被截断,并且返回 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(())
}
Runsourcepub fn recv_vectored_with_ancillary(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<(usize, bool)>
🔬This is a nightly-only experimental API. (unix_socket_ancillary_data
#76915)
pub fn recv_vectored_with_ancillary( &self, bufs: &mut [IoSliceMut<'_>], ancillary: &mut SocketAncillary<'_> ) -> Result<(usize, bool)>
unix_socket_ancillary_data
#76915)从套接字接收数据和辅助数据。
成功后,返回读取的字节数以及数据是否被截断。
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(())
}
Run1.70.0 · sourcepub fn send_to_addr(
&self,
buf: &[u8],
socket_addr: &SocketAddr
) -> Result<usize>
pub fn send_to_addr( &self, buf: &[u8], socket_addr: &SocketAddr ) -> Result<usize>
将套接字上的数据发送到指定的 SocketAddr。
成功时,返回写入的字节数。
Examples
use std::os::unix::net::{UnixDatagram};
fn main() -> std::io::Result<()> {
let bound = UnixDatagram::bind("/path/to/socket")?;
let addr = bound.local_addr()?;
let sock = UnixDatagram::unbound()?;
sock.send_to_addr(b"bacon egg and cheese", &addr).expect("send_to_addr function failed");
Ok(())
}
Runsourcepub fn send(&self, buf: &[u8]) -> Result<usize>
pub fn send(&self, buf: &[u8]) -> Result<usize>
将套接字上的数据发送到套接字的对等方。
可以通过 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(())
}
Runsourcepub fn send_vectored_with_ancillary_to<P: AsRef<Path>>(
&self,
bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>,
path: P
) -> Result<usize>
🔬This is a nightly-only experimental API. (unix_socket_ancillary_data
#76915)
pub fn send_vectored_with_ancillary_to<P: AsRef<Path>>( &self, bufs: &[IoSlice<'_>], ancillary: &mut SocketAncillary<'_>, path: P ) -> Result<usize>
unix_socket_ancillary_data
#76915)将套接字上的数据和辅助数据发送到指定地址。
成功时,返回写入的字节数。
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(())
}
Runsourcepub fn send_vectored_with_ancillary(
&self,
bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<usize>
🔬This is a nightly-only experimental API. (unix_socket_ancillary_data
#76915)
pub fn send_vectored_with_ancillary( &self, bufs: &[IoSlice<'_>], ancillary: &mut SocketAncillary<'_> ) -> Result<usize>
unix_socket_ancillary_data
#76915)在套接字上发送数据和辅助数据。
成功时,返回写入的字节数。
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(())
}
Runsourcepub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
设置套接字的读取超时。
如果提供的值为 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(())
}
Runuse 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(())
}
Runsourcepub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>
设置套接字的写超时。
如果提供的值为 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(())
}
Runuse 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(())
}
Runsourcepub fn read_timeout(&self) -> Result<Option<Duration>>
pub fn read_timeout(&self) -> Result<Option<Duration>>
返回此套接字的读取超时。
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(())
}
Runsourcepub fn write_timeout(&self) -> Result<Option<Duration>>
pub fn write_timeout(&self) -> Result<Option<Duration>>
返回此套接字的写入超时。
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(())
}
Runsourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
sourcepub fn set_passcred(&self, passcred: bool) -> Result<()>
🔬This is a nightly-only experimental API. (unix_socket_ancillary_data
#76915)
pub fn set_passcred(&self, passcred: bool) -> Result<()>
unix_socket_ancillary_data
#76915)移动套接字以将 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 函数失败");
Ok(())
}
Runsourcepub fn passcred(&self) -> Result<bool>
🔬This is a nightly-only experimental API. (unix_socket_ancillary_data
#76915)
pub fn passcred(&self) -> Result<bool>
unix_socket_ancillary_data
#76915)获取用于在 SocketAncillary
中传递 unix 凭据的套接字的当前值。
可以通过 set_passcred
更改此值。
获取套接字选项 SO_PASSCRED
。
sourcepub fn set_mark(&self, mark: u32) -> Result<()>
🔬This is a nightly-only experimental API. (unix_set_mark
#96467)
pub fn set_mark(&self, mark: u32) -> Result<()>
unix_set_mark
#96467)设置套接字的 id 以进行网络过滤
#![feature(unix_set_mark)]
use std::os::unix::net::UnixDatagram;
fn main() -> std::io::Result<()> {
let sock = UnixDatagram::unbound()?;
sock.set_mark(32)?;
Ok(())
}
Runsourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
sourcepub fn peek(&self, buf: &mut [u8]) -> Result<usize>
🔬This is a nightly-only experimental API. (unix_socket_peek
#76923)
pub fn peek(&self, buf: &mut [u8]) -> Result<usize>
unix_socket_peek
#76923)从套接字所连接的远程地址接收套接字上的数据,而无需从队列中删除该数据。
成功时,返回偷看的字节数。
连续调用返回相同的数据。
这是通过将 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(())
}
Runsourcepub fn peek_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
🔬This is a nightly-only experimental API. (unix_socket_peek
#76923)
pub fn peek_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
unix_socket_peek
#76923)在套接字上接收单个数据报消息,而无需将其从队列中删除。 成功时,返回读取的字节数和源。
必须使用足够大的有效字节数组 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(())
}
RunTrait Implementations§
1.63.0 · source§impl AsFd for UnixDatagram
impl AsFd for UnixDatagram
source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
source§impl Debug for UnixDatagram
impl Debug for UnixDatagram
1.63.0 · source§impl From<OwnedFd> for UnixDatagram
impl From<OwnedFd> for UnixDatagram
1.63.0 · source§impl From<UnixDatagram> for OwnedFd
impl From<UnixDatagram> for OwnedFd
source§fn from(unix_datagram: UnixDatagram) -> OwnedFd
fn from(unix_datagram: UnixDatagram) -> OwnedFd
source§impl FromRawFd for UnixDatagram
impl FromRawFd for UnixDatagram
source§unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram
unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram
Self
的新实例。 Read more