pub struct UnixStream(_);
Available on Unix only.
Expand description
Unix 流套接字。
Examples
use std::os::unix::net::UnixStream;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let mut stream = UnixStream::connect("/path/to/my/socket")?;
stream.write_all(b"hello world")?;
let mut response = String::new();
stream.read_to_string(&mut response)?;
println!("{response}");
Ok(())
}
RunImplementations§
source§impl UnixStream
impl UnixStream
1.70.0 · sourcepub fn connect_addr(socket_addr: &SocketAddr) -> Result<UnixStream>
pub fn connect_addr(socket_addr: &SocketAddr) -> Result<UnixStream>
连接到 address
指定的套接字。
Examples
use std::os::unix::net::{UnixListener, UnixStream};
fn main() -> std::io::Result<()> {
let listener = UnixListener::bind("/path/to/the/socket")?;
let addr = listener.local_addr()?;
let sock = match UnixStream::connect_addr(&addr) {
Ok(sock) => sock,
Err(e) => {
println!("Couldn't connect: {e:?}");
return Err(e)
}
};
Ok(())
}
Runsourcepub fn pair() -> Result<(UnixStream, UnixStream)>
pub fn pair() -> Result<(UnixStream, UnixStream)>
sourcepub fn try_clone(&self) -> Result<UnixStream>
pub fn try_clone(&self) -> Result<UnixStream>
为底层套接字创建一个新的独立的拥有所有权的句柄。
返回的 UnixStream
是与此对象引用相同的流的引用。
两个句柄将读取和写入相同的数据流,并且在一个流上设置的选项将传播到另一流。
Examples
use std::os::unix::net::UnixStream;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
let sock_copy = socket.try_clone().expect("Couldn't clone socket");
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 peer_cred(&self) -> Result<UCred>
🔬This is a nightly-only experimental API. (peer_credentials_unix_socket
#42839)
pub fn peer_cred(&self) -> Result<UCred>
peer_credentials_unix_socket
#42839)sourcepub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
设置套接字的读取超时。
如果提供的值为 None
,则 read
调用将无限期阻塞。
如果将零 Duration
传递给此方法,则返回 Err
。
Examples
use std::os::unix::net::UnixStream;
use std::time::Duration;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
Ok(())
}
Runuse std::io;
use std::os::unix::net::UnixStream;
use std::time::Duration;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
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
,则 write
调用将无限期阻塞。
如果将零 Duration
传递给此方法,则返回 Err
。
Examples
use std::os::unix::net::UnixStream;
use std::time::Duration;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
socket.set_write_timeout(Some(Duration::new(1, 0)))
.expect("Couldn't set write timeout");
Ok(())
}
Runuse std::io;
use std::net::UdpSocket;
use std::time::Duration;
fn main() -> std::io::Result<()> {
let socket = UdpSocket::bind("127.0.0.1:34254")?;
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::UnixStream;
use std::time::Duration;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
assert_eq!(socket.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::UnixStream;
use std::time::Duration;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
socket.set_write_timeout(Some(Duration::new(1, 0)))
.expect("Couldn't set write timeout");
assert_eq!(socket.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::UnixStream;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
socket.set_passcred (true).expect ("无法设置密码");
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::UnixStream;
fn main() -> std::io::Result<()> {
let sock = UnixStream::connect("/tmp/sock")?;
sock.set_mark(32)?;
Ok(())
}
Runsourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
sourcepub fn shutdown(&self, how: Shutdown) -> Result<()>
pub fn shutdown(&self, how: Shutdown) -> Result<()>
关闭此连接的读取,写入或两半。
此函数将导致对指定部分的所有未决和 future I/O 调用立即返回适当的值 (请参见 Shutdown
的文档)。
Examples
use std::os::unix::net::UnixStream;
use std::net::Shutdown;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
socket.shutdown(Shutdown::Both).expect("shutdown function failed");
Ok(())
}
Runsourcepub 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::UnixStream;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
let mut buf = [0; 10];
let len = socket.peek(&mut buf).expect("peek failed");
Ok(())
}
Runsourcepub fn recv_vectored_with_ancillary(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<usize>
🔬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>
unix_socket_ancillary_data
#76915)从套接字接收数据和辅助数据。
成功时,返回读取的字节数。
Examples
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
use std::io::IoSliceMut;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
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 = socket.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(())
}
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::{UnixStream, SocketAncillary};
use std::io::IoSlice;
fn main() -> std::io::Result<()> {
let socket = UnixStream::connect("/tmp/sock")?;
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[..]);
socket.send_vectored_with_ancillary(bufs, &mut ancillary)
.expect("send_vectored_with_ancillary function failed");
Ok(())
}
RunTrait Implementations§
1.63.0 · source§impl AsFd for UnixStream
impl AsFd for UnixStream
source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
借用文件描述符。 Read more
source§impl Debug for UnixStream
impl Debug for UnixStream
1.63.0 · source§impl From<UnixStream> for OwnedFd
impl From<UnixStream> for OwnedFd
source§fn from(unix_stream: UnixStream) -> OwnedFd
fn from(unix_stream: UnixStream) -> OwnedFd
从输入类型转换为此类型。
source§impl FromRawFd for UnixStream
impl FromRawFd for UnixStream
source§unsafe fn from_raw_fd(fd: RawFd) -> UnixStream ⓘ
unsafe fn from_raw_fd(fd: RawFd) -> UnixStream ⓘ
根据给定的原始文件描述符构造
Self
的新实例。 Read moresource§impl IntoRawFd for UnixStream
impl IntoRawFd for UnixStream
source§fn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
消费这个对象,返回原始的底层文件描述符。 Read more
source§impl<'a> Read for &'a UnixStream
impl<'a> Read for &'a UnixStream
source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
与
read
相似,不同之处在于它读入缓冲区的一部分。 Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
读取所有字节,直到此源中的 EOF 为止,然后将它们放入
buf
。 Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
读取这个源中的所有字节,直到 EOF 为止,然后将它们追加到
buf
。 Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()>
🔬This is a nightly-only experimental API. (
read_buf
#78485)从此源中提取一些字节到指定的缓冲区中。 Read more
source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
🔬This is a nightly-only experimental API. (
read_buf
#78485)读取填充
cursor
所需的确切字节数。 Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
为这个
Read
实例创建一个 “by reference” 适配器。 Read moresource§impl Read for UnixStream
impl Read for UnixStream
source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
与
read
相似,不同之处在于它读入缓冲区的一部分。 Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
读取所有字节,直到此源中的 EOF 为止,然后将它们放入
buf
。 Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
读取这个源中的所有字节,直到 EOF 为止,然后将它们追加到
buf
。 Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()>
🔬This is a nightly-only experimental API. (
read_buf
#78485)从此源中提取一些字节到指定的缓冲区中。 Read more
source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
🔬This is a nightly-only experimental API. (
read_buf
#78485)读取填充
cursor
所需的确切字节数。 Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
为这个
Read
实例创建一个 “by reference” 适配器。 Read moresource§impl<'a> Write for &'a UnixStream
impl<'a> Write for &'a UnixStream
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
🔬This is a nightly-only experimental API. (
write_all_vectored
#70436)尝试将多个缓冲区写入此 writer。 Read more
source§impl Write for UnixStream
impl Write for UnixStream
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
🔬This is a nightly-only experimental API. (
write_all_vectored
#70436)尝试将多个缓冲区写入此 writer。 Read more