Struct std::os::unix::net::UnixStream 1.10.0[−][src]
pub struct UnixStream(_);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
连接到 address 指定的套接字。
Examples
#![feature(unix_socket_abstract)]
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(())
}Run为底层套接字创建一个新的独立的拥有所有权的句柄。
返回的 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(())
}Run🔬 This is a nightly-only experimental API. (peer_credentials_unix_socket #42839)
unstable
🔬 This is a nightly-only experimental API. (peer_credentials_unix_socket #42839)
unstable
设置套接字的读取超时。
如果提供的值为 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(())
}Run设置套接字的写超时。
如果提供的值为 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(())
}Run返回此套接字的读取超时。
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(())
}Run返回此套接字的写入超时。
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(())
}Run移动套接字以将 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("Couldn't set passcred");
Ok(())
}Run获取用于在 SocketAncillary 中传递 unix 凭据的套接字的当前值。
可以通过 set_passcred 更改此值。
获取套接字选项 SO_PASSCRED。
关闭此连接的读取,写入或两半。
此函数将导致对指定部分的所有未决和 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(())
}Run从套接字所连接的远程地址接收套接字上的数据,而无需从队列中删除该数据。
成功时,返回偷看的字节数。
连续调用返回相同的数据。
这是通过将 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(())
}Runpub fn recv_vectored_with_ancillary(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<usize>
pub fn recv_vectored_with_ancillary(
&self,
bufs: &mut [IoSliceMut<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<usize>
从套接字接收数据和辅助数据。
成功时,返回读取的字节数。
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(())
}Runpub fn send_vectored_with_ancillary(
&self,
bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<usize>
pub fn send_vectored_with_ancillary(
&self,
bufs: &[IoSlice<'_>],
ancillary: &mut SocketAncillary<'_>
) -> Result<usize>
在套接字上发送数据和辅助数据。
成功时,返回写入的字节数。
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
执行转换。
unsafe fn from_raw_fd(fd: RawFd) -> UnixStreamⓘNotable traits for UnixStreamimpl Read for UnixStreamimpl<'a> Read for &'a UnixStreamimpl Write for UnixStreamimpl<'a> Write for &'a UnixStream
unsafe fn from_raw_fd(fd: RawFd) -> UnixStreamⓘNotable traits for UnixStreamimpl Read for UnixStreamimpl<'a> Read for &'a UnixStreamimpl Write for UnixStreamimpl<'a> Write for &'a UnixStream
impl Read for UnixStreamimpl<'a> Read for &'a UnixStreamimpl Write for UnixStreamimpl<'a> Write for &'a UnixStream根据给定的原始文件描述符构造 Self 的新实例。 Read more
消费这个对象,返回原始的底层文件描述符。 Read more
将格式化的字符串写入此 writer,返回遇到的任何错误。 Read more
将格式化的字符串写入此 writer,返回遇到的任何错误。 Read more