Skip to main content

Module socketaddr

Module socketaddr 

Source
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::SocketAddr only 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§

SocketAddr
A socket address combining a host and a port.

Enums§

SocketAddrError
Error type for socket address parsing.
StdConversionError
Error type for std::net::SocketAddr conversion.