Enum SocketAddr

pub enum SocketAddr

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 and SocketAddrV6'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 all repr(Rust) structs, however, its exact layout remains undefined and should not be relied upon between builds.

Examples

use std::net::{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);

Variants

V4(SocketAddrV4)

An IPv4 socket address.

V6(SocketAddrV6)

An IPv6 socket address.

Implementations

impl SocketAddr

fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError>

Parse a socket address from a slice of bytes.

#![feature(addr_parse_ascii)]

use std::net::{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));

impl SocketAddr

const fn new(ip: IpAddr, port: u16) -> SocketAddr

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

Examples

use std::net::{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);
const fn ip(&self) -> IpAddr

Returns the IP address associated with this socket address.

Examples

use std::net::{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)));
const fn set_ip(&mut self, new_ip: IpAddr)

Changes the IP address associated with this socket address.

Examples

use std::net::{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)));
const fn port(&self) -> u16

Returns the port number associated with this socket address.

Examples

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

let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
assert_eq!(socket.port(), 8080);
const fn set_port(&mut self, new_port: u16)

Changes the port number associated with this socket address.

Examples

use std::net::{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);
const fn is_ipv4(&self) -> bool

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

Examples

use std::net::{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);
const fn is_ipv6(&self) -> bool

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

Examples

use std::net::{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);
const fn unspecified_from(this: Self) -> Self

Returns the unspecified socket address for the same IP version.

Returns 0.0.0.0:0 for IPv4 and [::]:0 for IPv6. For IPv6, this method preserves the flow information and scope ID.

Use this method when you must bind a socket to an unspecified local address that uses the same IP version as a remote address.

Examples

#![feature(addr_unspecified_from)]
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6, Ipv4Addr, Ipv6Addr};

let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 3000));
assert_eq!(
    SocketAddr::unspecified_from(addr),
    SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0))
);

let addr = SocketAddr::V6(SocketAddrV6::new(
    Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
    443,
    0x1234,
    5,
));

let unspecified = SocketAddr::unspecified_from(addr);
assert_eq!(unspecified.ip(), Ipv6Addr::UNSPECIFIED);
if let SocketAddr::V6(v6) = unspecified {
    assert_eq!(v6.flowinfo(), 0x1234);
    assert_eq!(v6.scope_id(), 5);
}

Trait Implementations

impl Clone for SocketAddr

fn clone(&self) -> SocketAddr

impl Copy for SocketAddr

impl Debug for SocketAddr

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

impl Display for SocketAddr

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Eq for SocketAddr

fn assert_fields_are_eq(&self)

impl From<SocketAddrV4> for SocketAddr

fn from(sock4: SocketAddrV4) -> SocketAddr

Converts a SocketAddrV4 into a SocketAddr::V4.

impl From<SocketAddrV6> for SocketAddr

fn from(sock6: SocketAddrV6) -> SocketAddr

Converts a SocketAddrV6 into a SocketAddr::V6.

impl FromStr for SocketAddr

type Err = AddrParseError;
fn from_str(s: &str) -> Result<SocketAddr, AddrParseError>

impl Hash for SocketAddr

fn hash<__H: Hasher>(&self, state: &mut __H)

impl Ord for SocketAddr

fn cmp(&self, other: &SocketAddr) -> Ordering

impl PartialEq for SocketAddr

fn eq(&self, other: &SocketAddr) -> bool

impl PartialOrd for SocketAddr

fn partial_cmp(&self, other: &SocketAddr) -> Option<Ordering>

impl StructuralPartialEq for SocketAddr

impl TrivialClone for SocketAddr

impl<I: ~const Into<IpAddr>> From<(I, u16)> for SocketAddr

fn from(pieces: (I, u16)) -> SocketAddr

Converts a tuple struct (Into<IpAddr>, u16) into a SocketAddr.

This conversion creates a SocketAddr::V4 for an IpAddr::V4 and creates a SocketAddr::V6 for an IpAddr::V6.

u16 is treated as port of the newly created SocketAddr.

Auto Trait Implementations

impl Freeze for SocketAddr

impl RefUnwindSafe for SocketAddr

impl Send for SocketAddr

impl Sync for SocketAddr

impl Unpin for SocketAddr

impl UnsafeUnpin for SocketAddr

impl UnwindSafe for SocketAddr

Blanket Implementations

impl<T> Any for SocketAddr where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for SocketAddr where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for SocketAddr where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for SocketAddr where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for SocketAddr

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Printable for SocketAddr where T: Copy + Debug,

impl<T> SizeHint for SocketAddr where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for SocketAddr

impl<T, U> Into<U> for SocketAddr where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for SocketAddr where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for SocketAddr where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>