struct SF::IpAddress
- SF::IpAddress
- Struct
- Value
- Object
Overview
Encapsulate an IPv4 network address
SF::IpAddress
is a utility class for manipulating network
addresses. It provides a set a implicit constructors and
conversion functions to easily build or transform an IP
address from/to various representations.
Usage example:
a0 = SF::IpAddress.new # an invalid address
a1 = SF::IpAddress::None # an invalid address (same as a0)
a2 = SF::IpAddress.new("127.0.0.1") # the local host address
a3 = SF::IpAddress::Broadcast # the broadcast address
a4 = SF::IpAddress.new(192, 168, 1, 56) # a local address
a5 = SF::IpAddress.new("my_computer") # a local address created from a network name
a6 = SF::IpAddress.new("89.54.1.169") # a distant address
a7 = SF::IpAddress.new("www.google.com") # a distant address created from a network name
a8 = SF::IpAddress.local_address # my address on the local network
a9 = SF::IpAddress.get_public_address # my address on the internet
Note that SF::IpAddress
currently doesn't support IPv6
nor other types of network addresses.
Defined in:
network/network.crnetwork/obj.cr
Constant Summary
-
Any =
new(0, 0, 0, 0)
-
Value representing any address (0.0.0.0)
-
Broadcast =
new(255, 255, 255, 255)
-
The "broadcast" address (for sending UDP messages to everyone on a local network)
-
LocalHost =
new(127, 0, 0, 1)
-
The "localhost" address (for connecting a computer to itself locally)
-
None =
new
-
Value representing an empty/invalid address
Constructors
-
.get_public_address(timeout : Time = Time::Zero) : IpAddress
Get the computer's public address
-
.local_address : IpAddress
Get the computer's local address
-
.new(byte0 : Int, byte1 : Int, byte2 : Int, byte3 : Int)
Construct the address from 4 bytes
-
.new(address : String)
Construct the address from a string
-
.new(address : Int)
Construct the address from a 32-bits integer
-
.new
Default constructor
Instance Method Summary
-
#!=(right : IpAddress) : Bool
Overload of != operator to compare two IP addresses
-
#<(right : IpAddress) : Bool
Overload of < operator to compare two IP addresses
-
#<=(right : IpAddress) : Bool
Overload of <= operator to compare two IP addresses
-
#==(right : IpAddress) : Bool
Overload of == operator to compare two IP addresses
-
#>(right : IpAddress) : Bool
Overload of > operator to compare two IP addresses
-
#>=(right : IpAddress) : Bool
Overload of >= operator to compare two IP addresses
-
#dup : IpAddress
Returns a shallow copy of this object.
- #inspect(io)
-
#to_integer : UInt32
Get an integer representation of the address
-
#to_s : String
Get a string representation of the address
Constructor Detail
Get the computer's public address
The public address is the address of the computer from the internet point of view, i.e. something like 89.54.1.169. It is necessary for communications over the world wide web. The only way to get a public address is to ask it to a distant website; as a consequence, this function depends on both your network connection and the server, and may be very slow. You should use it as few as possible. Because this function depends on the network connection and on a distant server, you may use a time limit if you don't want your program to be possibly stuck waiting in case there is a problem; this limit is deactivated by default.
- timeout - Maximum time to wait
Returns: Public IP address of the computer
See also: .local_address
Get the computer's local address
The local address is the address of the computer from the LAN point of view, i.e. something like 192.168.1.56. It is meaningful only for communications over the local network. Unlike public_address, this function is fast and may be used safely anywhere.
Returns: Local IP address of the computer
See also: public_address
Construct the address from 4 bytes
Calling IpAddress(a, b, c, d) is equivalent to calling IpAddress("a.b.c.d"), but safer as it doesn't have to parse a string to get the address components.
- byte0 - First byte of the address
- byte1 - Second byte of the address
- byte2 - Third byte of the address
- byte3 - Fourth byte of the address
Construct the address from a string
Here address can be either a decimal address (ex: "192.168.1.56") or a network name (ex: "localhost"). This is equivalent to the constructor taking a std::string parameter, it is defined for convenience so that the implicit conversions from literal strings to IpAddress work.
- address - IP address or network name
Construct the address from a 32-bits integer
This constructor uses the internal representation of the address directly. It should be used for optimization purposes, and only if you got that representation from IpAddress.to_integer().
- address - 4 bytes of the address packed into a 32-bits integer
See also: #to_integer
Instance Method Detail
Overload of != operator to compare two IP addresses
- left - Left operand (a IP address)
- right - Right operand (a IP address)
Returns: True if both addresses are different
Overload of < operator to compare two IP addresses
- left - Left operand (a IP address)
- right - Right operand (a IP address)
Returns: True if left is lesser than right
Overload of <= operator to compare two IP addresses
- left - Left operand (a IP address)
- right - Right operand (a IP address)
Returns: True if left is lesser or equal than right
Overload of == operator to compare two IP addresses
- left - Left operand (a IP address)
- right - Right operand (a IP address)
Returns: True if both addresses are equal
Overload of > operator to compare two IP addresses
- left - Left operand (a IP address)
- right - Right operand (a IP address)
Returns: True if left is greater than right
Overload of >= operator to compare two IP addresses
- left - Left operand (a IP address)
- right - Right operand (a IP address)
Returns: True if left is greater or equal than right
Returns a shallow copy of this object.
Because Value
is a value type, this method returns self
,
which already involves a shallow copy of this object because
value types are passed by value.
Get an integer representation of the address
The returned number is the internal representation of the
address, and should be used for optimization purposes only
(like sending the address through a socket).
The integer produced by this function can then be converted
back to a SF::IpAddress
with the proper constructor.
Returns: 32-bits unsigned integer representation of the address
See also: #to_s
Get a string representation of the address
The returned string is the decimal representation of the IP address (like "192.168.1.56"), even if it was constructed from a host name.
Returns: String representation of the address
See also: #to_integer