Struct core::net::SocketAddrV4
1.0.0 · source · pub struct SocketAddrV4 { /* private fields */ }
Expand description
IPv4 套接字地址。
IPv4 套接字地址由 IPv4
地址 和一个 16 位端口号组成,如 IETF RFC 793 中所述。
有关同时包含 IPv4 和 IPv6 套接字地址的类型,请参见 SocketAddr
。
SocketAddrV4
结构体的大小可能会因目标操作系统而异。
不要假设此类型与底层系统表示具有相同的内存布局。
Examples
use std::net::{Ipv4Addr, SocketAddrV4};
let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
assert_eq!("127.0.0.1:8080".parse(), Ok(socket));
assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
assert_eq!(socket.port(), 8080);
RunImplementations§
source§impl SocketAddrV4
impl SocketAddrV4
sourcepub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError>
🔬This is a nightly-only experimental API. (addr_parse_ascii
#101035)
pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError>
addr_parse_ascii
#101035)从字节片中解析 IPv4 套接字地址。
#![feature(addr_parse_ascii)]
use std::net::{Ipv4Addr, SocketAddrV4};
let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
assert_eq!(SocketAddrV4::parse_ascii(b"127.0.0.1:8080"), Ok(socket));
Runsource§impl SocketAddrV4
impl SocketAddrV4
const: 1.69.0 · sourcepub const fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4
pub const fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4
从 IPv4
address 和端口号创建一个新的套接字地址。
Examples
use std::net::{SocketAddrV4, Ipv4Addr};
let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
RunTrait Implementations§
source§impl Clone for SocketAddrV4
impl Clone for SocketAddrV4
source§fn clone(&self) -> SocketAddrV4
fn clone(&self) -> SocketAddrV4
返回值的副本。 Read more
source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
从
source
执行复制分配。 Read moresource§impl Debug for SocketAddrV4
impl Debug for SocketAddrV4
source§impl Display for SocketAddrV4
impl Display for SocketAddrV4
1.16.0 · source§impl From<SocketAddrV4> for SocketAddr
impl From<SocketAddrV4> for SocketAddr
source§fn from(sock4: SocketAddrV4) -> SocketAddr
fn from(sock4: SocketAddrV4) -> SocketAddr
将 SocketAddrV4
转换为 SocketAddr::V4
。
1.5.0 · source§impl FromStr for SocketAddrV4
impl FromStr for SocketAddrV4
§type Err = AddrParseError
type Err = AddrParseError
可以从解析中返回的相关错误。
source§fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError>
fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError>
解析字符串
s
以返回此类型的值。 Read more