Available on crate feature
net only.Expand description
Socket address type for network programming.
This module provides a type-safe abstraction for socket addresses, combining a host (IP address, domain name, or hostname) with a port number.
§Interoperability with std::net
When std feature is enabled, SocketAddr provides TryFrom implementations
for converting to/from std::net::SocketAddr. Note that:
- Only IP addresses can be converted:
std::net::SocketAddronly supports IP addresses - Domain names and hostnames cannot be converted: They require DNS resolution (which is outside of scope of this library)
- Use
TryFrom: Conversions may fail if the host is not an IP address
§Examples
use bare_types::net::{SocketAddr, Host, Port};
// Create from IP address and port
let host = Host::parse_str("192.168.1.1").unwrap();
let port = Port::new(8080).unwrap();
let addr = SocketAddr::new(host, port);
// Parse from string
let addr: SocketAddr = "192.168.1.1:8080".parse().unwrap();
// Access components
assert!(addr.as_host().is_ipaddr());
assert_eq!(addr.as_port().as_u16(), 8080);Structs§
- Socket
Addr - A socket address combining a host and a port.
Enums§
- Socket
Addr Error - Error type for socket address parsing.
- StdConversion
Error - Error type for
std::net::SocketAddrconversion.