SocketAddr in core::net - Rust (original) (raw)

Enum SocketAddr

1.77.0 · Source

pub enum SocketAddr {
    V4(SocketAddrV4),
    V6(SocketAddrV6),
}

Expand description

An internet socket address, either IPv4 or IPv6.

Internet socket addresses consist of an IP address, a 16-bit port number, as well as possibly some version-dependent additional information. See SocketAddrV4’s andSocketAddrV6’s respective documentation for more details.

§Portability

SocketAddr is intended to be a portable representation of socket addresses and is likely not the same as the internal socket address type used by the target operating system’s API. Like allrepr(Rust) structs, however, its exact layout remains undefined and should not be relied upon between builds.

§Examples

use std:🥅:{IpAddr, Ipv4Addr, SocketAddr};

let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

assert_eq!("127.0.0.1:8080".parse(), Ok(socket));
assert_eq!(socket.port(), 8080);
assert_eq!(socket.is_ipv4(), true);

§1.77.0

An IPv4 socket address.

§1.77.0

An IPv6 socket address.

Source§

Source

🔬This is a nightly-only experimental API. (addr_parse_ascii #101035)

Parse a socket address from a slice of bytes.

#![feature(addr_parse_ascii)]

use std:🥅:{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

let socket_v4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
let socket_v6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8080);

assert_eq!(SocketAddr::parse_ascii(b"127.0.0.1:8080"), Ok(socket_v4));
assert_eq!(SocketAddr::parse_ascii(b"[::1]:8080"), Ok(socket_v6));

Source§

1.7.0 (const: 1.69.0) · Source

Creates a new socket address from an IP address and a port number.

§Examples
use std:🥅:{IpAddr, Ipv4Addr, SocketAddr};

let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
assert_eq!(socket.port(), 8080);

1.7.0 (const: 1.69.0) · Source

Returns the IP address associated with this socket address.

§Examples
use std:🥅:{IpAddr, Ipv4Addr, SocketAddr};

let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));

1.9.0 (const: 1.87.0) · Source

Changes the IP address associated with this socket address.

§Examples
use std:🥅:{IpAddr, Ipv4Addr, SocketAddr};

let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
socket.set_ip(IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));

1.0.0 (const: 1.69.0) · Source

Returns the port number associated with this socket address.

§Examples
use std:🥅:{IpAddr, Ipv4Addr, SocketAddr};

let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
assert_eq!(socket.port(), 8080);

1.9.0 (const: 1.87.0) · Source

Changes the port number associated with this socket address.

§Examples
use std:🥅:{IpAddr, Ipv4Addr, SocketAddr};

let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
socket.set_port(1025);
assert_eq!(socket.port(), 1025);

1.16.0 (const: 1.69.0) · Source

Returns true if the IP address in this SocketAddr is anIPv4 address, and false otherwise.

§Examples
use std:🥅:{IpAddr, Ipv4Addr, SocketAddr};

let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
assert_eq!(socket.is_ipv4(), true);
assert_eq!(socket.is_ipv6(), false);

1.16.0 (const: 1.69.0) · Source

Returns true if the IP address in this SocketAddr is anIPv6 address, and false otherwise.

§Examples
use std:🥅:{IpAddr, Ipv6Addr, SocketAddr};

let socket = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 0, 1)), 8080);
assert_eq!(socket.is_ipv4(), false);
assert_eq!(socket.is_ipv6(), true);

§

§

§

§

§

§