SocketAddr in std::net - Rust (original) (raw)
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.0.0
An IPv4 socket address.
§1.0.0
An IPv6 socket address.
🔬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));
1.7.0 (const: 1.69.0)
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)
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)
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)
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)
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)
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)
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);1.0.0§
Returns a duplicate of the value. Read more
1.0.0§
Performs copy-assignment from source. Read more
1.0.0§
Formats the value using the given formatter. Read more
1.0.0§
Formats the value using the given formatter. Read more
1.0.0§
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
1.0.0§
1.0.0§
1.21.0§
Compares and returns the maximum of two values. Read more
1.21.0§
Compares and returns the minimum of two values. Read more
1.50.0§
Restrict a value to a certain interval. Read more
1.0.0§
Tests for self and other values to be equal, and is used by ==.
1.0.0§
Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
1.0.0§
This method returns an ordering between self and other values if one exists. Read more
1.0.0§
Tests less than (for self and other) and is used by the < operator. Read more
1.0.0§
Tests less than or equal to (for self and other) and is used by the<= operator. Read more
1.0.0§
Tests greater than (for self and other) and is used by the >operator. Read more
1.0.0§
Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Returned iterator over socket addresses which this type may correspond to.
1.0.0§
1.0.0§
1.0.0§
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dest. Read more
Returns the argument unchanged.
Calls U::from(self).
That is, this conversion is whatever the implementation of[From](../convert/trait.From.html "trait std::convert::From")<T> for U chooses to do.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.