Classes

class IPAddress

abstract sealed class IPAddress <: ToString & Equatable<IPAddress> & Hashable & BigEndianOrder<IPAddress>

Description: Indicates an Internet Protocol (IP) address. An IP address is a digital label, such as 192.0.2.1 or 2001:0db8:0000:0000:0000:ff00:0042:8329, assigned to a computer network device that uses the Internet protocol for communication. The IP address has two main functionalities: network interface identification and location addressing.

Parent types:

prop hostName

public prop hostName: ?String

Description: Returns the host name corresponding to the current IPAddress object. If the host name cannot be resolved, None is returned. This property is not implemented currently.

Throws:

Type: ?String

prop size

public prop size: Int64

Description: Obtains the byte length of an IP address object.

Type: Int64

static func parse(String)

public static func parse(s: String): IPAddress

Description: Converts the socket string of an IP protocol into an IPAddress object.

Parameters:

  • s: String: socket string of the IP protocol

Returns:

Throws:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let v4: IPAddress = IPAddress.parse("192.168.1.2")
    let v6: IPAddress = IPAddress.parse("2001:0250:1006:dff0:4913:2aa5:8075:7c10")
    @Assert(v4.toString(), "192.168.1.2")
    @Assert(v6.toString(), "2001:250:1006:dff0:4913:2aa5:8075:7c10")
}

static func readBigEndian(Array<Byte>)

public static func readBigEndian(buffer: Array<Byte>): IPAddress

Description: Reads an IPAddress object from a byte array in big-endian mode.

Parameters:

  • buffer: Array<Byte>: buffer used to store the data to be read

Throws:

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let bufferV4: Array<Byte> = [0xC0, 0xA8, 0x1, 0x2]
    let bufferV6: Array<Byte> = [0x20, 0x01, 0x02, 0x50, 0x10, 0x06, 0xdf, 0xf0, 0x49, 0x13, 0x2a, 0xa5, 0x80, 0x75, 0x7c, 0x10]
    let v4: IPAddress = IPAddress.readBigEndian(bufferV4)
    let v6: IPAddress = IPAddress.readBigEndian(bufferV6)
    @Assert(v4.toString(), "192.168.1.2")
    @Assert(v6.toString(), "2001:250:1006:dff0:4913:2aa5:8075:7c10")
}

static func resolve(AddressFamily, String)

public static func resolve(family: AddressFamily, domain: String): Array<IPAddress>

Description: Parses a domain name to obtain an IPAddress list.

Parameters:

Returns:

Examples:

import std.net.*

main () {
    let iplist: Array<IPAddress> = IPAddress.resolve(AddressFamily.UNSPEC, "localhost")
    println(iplist)
}
// may output: [127.0.0.1, ::1]

static func resolve(String)

public static func resolve(domain: String): Array<IPAddress>

Description: Parses a domain name to obtain an IPAddress list.

Parameters:

Returns:

Examples:

import std.net.*

main () {
    let iplist: Array<IPAddress> = IPAddress.resolve("localhost")
    println(iplist)
}
// may output: [127.0.0.1, ::1]

static func tryParse(String)

public static func tryParse(s: String): ?IPAddress

Description: Converts an IP address string into an IPAddress object. If the string is invalid, None is returned.

Parameters:

  • s: String: IP address string

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let v4: ?IPAddress = IPAddress.tryParse("192.168.1.2")
    let v6: ?IPAddress = IPAddress.tryParse("2001:0250:1006:dff0:4913:2aa5:8075:7c10")
    @Assert(v4.toString(), "Some(192.168.1.2)")
    @Assert(v6.toString(), "Some(2001:250:1006:dff0:4913:2aa5:8075:7c10)")
}

func getAddressBytes()

public func getAddressBytes(): Array<Byte>

Description: Returns the original IP address of an IPAddress object.

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let expectV4: Array<Byte> = [0xC0, 0xA8, 0x1, 0x2]
    let expectV6: Array<Byte> = [0x20, 0x01, 0x02, 0x50, 0x10, 0x06, 0xdf, 0xf0, 0x49, 0x13, 0x2a, 0xa5, 0x80, 0x75, 0x7c, 0x10]
    let v4: IPAddress = IPAddress.parse("192.168.1.2")
    let v6: IPAddress = IPAddress.parse("2001:0250:1006:dff0:4913:2aa5:8075:7c10")
    @Assert(v4.getAddressBytes(), expectV4)
    @Assert(v6.getAddressBytes(), expectV6)
}

func getPrefix(UInt8)

public open func getPrefix(prefixLen: UInt8): IPPrefix

Description: Creates a network prefix object for an IPAddress address object based on a specified network prefix length.

Parameters:

  • prefixLen: UInt8: network prefix length

Throws:

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let prefix: IPPrefix = IPAddress.parse("192.168.1.2").getPrefix(24)
    @Assert(prefix.toString(), "192.168.1.2/24")
}

func hashCode()

public func hashCode(): Int64

Description: Obtains the hashcode value.

Returns:

func isGlobalUnicast()

public open func isGlobalUnicast(): Bool

Description: Checks whether an IPAddress object is a global unicast address.

Returns:

  • Bool: If the object is a global unicast address, true is returned. Otherwise, false is returned.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    // 2000::/3
    @Assert(IPAddress.parse("2001:250:1006:dff0:4913:2aa5:8075:7c10").isGlobalUnicast(), true)
}

func isIPv4()

public func isIPv4(): Bool

Description: Checks whether an IPAddress object is an IPv4 address.

Returns:

  • Bool: If the object is an IPv4 address, true is returned. Otherwise, false is returned.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    @Assert(IPAddress.parse("192.168.1.2").isIPv4(), true)
}

func isIPv6()

public func isIPv6(): Bool

Description: Checks whether an IPAddress object is an IPv6 address.

Returns:

  • Bool: If the object is an IPv6 address, true is returned. Otherwise, false is returned.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    @Assert(IPAddress.parse("2001:250:1006:dff0:4913:2aa5:8075:7c10").isIPv6(), true)
}

func isLinkLocal()

public open func isLinkLocal(): Bool

Description: Checks whether an IPAddress object is a link-local address.

Returns:

  • Bool: If the object is a link-local address, true is returned. Otherwise, false is returned.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    // 169.254.0.0 ~ 169.254.255.255
    @Assert(IPAddress.parse("169.254.0.1").isLinkLocal(), true)
    // fe80::/10
    @Assert(IPAddress.parse("fe80::1").isLinkLocal(), true)
}

func isLoopback()

public open func isLoopback(): Bool

Description: Checks whether an IPAddress object is a loopback address.

Returns:

  • Bool: If the object is a loopback address, true is returned. Otherwise, false is returned.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    @Assert(IPAddress.parse("127.0.0.1").isLoopback(), true)
    @Assert(IPAddress.parse("::1").isLoopback(), true)
}

func isMulticast()

public open func isMulticast(): Bool

Description: Checks whether an IPAddress object is a multicast address.

Returns:

  • Bool: If the object is a multicast address, true is returned. Otherwise, false is returned.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    // 224.0.0.0 ~ 239.255.255.255
    @Assert(IPAddress.parse("224.0.0.1").isMulticast(), true)
    // ff00::/8
    @Assert(IPAddress.parse("ff00::1").isMulticast(), true)
}

func isPrivate()

public open func isPrivate(): Bool

Description: Checks whether an IPAddress object is a private address.

Returns:

  • Bool: If the object is a private address, true is returned. Otherwise, false is returned.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    // 10.0.0.0 ~ 10.255.255.255
    @Assert(IPAddress.parse("10.0.0.1").isPrivate(), true)
    // 172.16.0.0 ~ 172.31.255.255
    @Assert(IPAddress.parse("172.16.0.1").isPrivate(), true)
    // 192.168.0.0 ~192.168.255.255
    @Assert(IPAddress.parse("192.168.0.1").isPrivate(), true)
    // fc00::/7
    @Assert(IPAddress.parse("fc00::1").isPrivate(), true)
}

func isUnspecified()

public open func isUnspecified(): Bool

Description: Checks whether an IPAddress object is an unspecified IP address.

Returns:

  • Bool: If the object is an unspecified address, true is returned. Otherwise, false is returned.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    @Assert(IPAddress.parse("0.0.0.0").isUnspecified(), true)
    @Assert(IPAddress.parse("::").isUnspecified(), true)
}

func writeBigEndian(Array<Byte>)

public open func writeBigEndian(buffer: Array<Byte>): Int64

Description: Writes an IPAddress object to a byte array in big-endian mode.

Parameters:

  • buffer: Array<Byte>: buffer used to store the data to be written

Throws:

Returns:

  • Int64: number of bytes of the written data

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let buffer = Array<Byte>(128, repeat: 0)
    let expectV4: Array<Byte> = [0xC0, 0xA8, 0x1, 0x2]
    let expectV6: Array<Byte> = [0x20, 0x01, 0x02, 0x50, 0x10, 0x06, 0xdf, 0xf0, 0x49, 0x13, 0x2a, 0xa5, 0x80, 0x75, 0x7c, 0x10]
    let v4: IPAddress = IPAddress.parse("192.168.1.2")
    let v6: IPAddress = IPAddress.parse("2001:0250:1006:dff0:4913:2aa5:8075:7c10")
    var len = v4.writeBigEndian(buffer)
    @Assert(buffer[..len], expectV4)
    len = v6.writeBigEndian(buffer)
    @Assert(buffer[..len], expectV6)
}

operator func ==(IPAddress)

public operator func ==(rhs: IPAddress): Bool

Description: Checks whether two IPAddress objects are equal.

Parameters:

Returns:

  • Bool: If the two IPAddress objects are equal, true is returned. Otherwise, false is returned.

operator func !=(IPAddress)

public operator func !=(rhs: IPAddress): Bool

Description: Checks whether two IPAddress objects are not equal.

Parameters:

Returns:

  • Bool: If the two IPAddress objects are not equal, true is returned. Otherwise, false is returned.

class IPPrefix

abstract sealed class IPPrefix <: Equatable<IPPrefix> & Hashable & ToString

Description: Indicates an IP prefix (also called IP subnet), which is a consecutive IP address block with the boundary being a power of 2.

An IP prefix is specified by the following information:

  • Start IP address (IPv4 or IPv6). This is the first IP address of the prefix.
  • Prefix length. This specifies the length of the prefix by specifying the number of bits in the IP address, starting from the most significant bit in the network byte order, which is constant for all addresses in the prefix.

For example, the prefix 192.0.2.0/24 covers 256 IPv4 addresses from 192.0.2.0 to 192.0.2.255 (included), and the prefix 2001:db8:1:2 covers 2^64 IPv6 addresses from 2001:db8:1:2:: to 2001:db8:1:2:ffff:ffff:ffff:ffff (included). Objects of this class are immutable.

Parent types:

prop address

public prop address: IPAddress

Description: Obtains an IPAddress address used to construct the current IPPrefix object.

Type: IPAddress

prop prefixLength

public prop prefixLength: UInt8

Description: Obtains the prefix length.

Type: UInt8

static func parse(String)

public static func parse(s: String): IPPrefix

Description: Converts the socket string of an IP protocol into an IPPrefix object.

Parameters:

  • s: String: socket string of the IP protocol

Throws:

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let v4: IPPrefix = IPPrefix.parse("192.168.1.2/24")
    let v6: IPPrefix = IPPrefix.parse("2001:0250:1006:dff0:4913:2aa5:8075:7c10/32")
    @Assert(v4.toString(), "192.168.1.2/24")
    @Assert(v6.toString(), "2001:250:1006:dff0:4913:2aa5:8075:7c10/32")
}

static func tryParse(String)

public static func tryParse(s: String): ?IPPrefix

Description: Converts the socket string of an IP protocol into an IPPrefix object. If the string is invalid, None is returned.

Parameters:

  • s: String: socket string of the IP protocol

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let v4: ?IPPrefix = IPPrefix.tryParse("192.168.1.2/24")
    let v6: ?IPPrefix = IPPrefix.tryParse("2001:0250:1006:dff0:4913:2aa5:8075:7c10/32")
    @Assert(v4.toString(), "Some(192.168.1.2/24)")
    @Assert(v6.toString(), "Some(2001:250:1006:dff0:4913:2aa5:8075:7c10/32)")
}

func broadcast()

public open func broadcast(): IPAddress

Description: Returns the broadcast address of an IPPrefix address.

Returns:

func contains(IPAddress)

public func contains(rhs: IPAddress): Bool

Description: Checks whether an IPPrefix address contains a specified IPAddress address.

Parameters:

Returns:

  • Bool: If the specified IPAddress address is contained, true is returned. Otherwise, false is returned.

func contains(IPPrefix)

public func contains(rhs: IPPrefix): Bool

Description: Checks whether an IPPrefix address contains a specified IPPrefix address.

Parameters:

Returns:

  • Bool: If the specified IPPrefix address is contained, true is returned. Otherwise, false is returned.

func hostmask()

public open func hostmask(): IPAddress

Description: Returns the host network mask address of an IPPrefix address.

Returns:

func masked()

public open func masked(): IPPrefix

Description: Returns an IPPrefix address that is masked based on the prefix length. For example, 192.168.0.0/16 is returned for 192.168.12.34/16; fc00::/16 is returned for fc00::1:2:3:4/16.

Returns:

func netmask()

public open func netmask(): IPAddress

Description: Returns the network mask address of an IPPrefix address.

Returns:

func network()

public open func network(): IPAddress

Description: Returns the network address of an IPPrefix address.

Returns:

func overlaps(IPPrefix)

public func overlaps(rhs: IPPrefix): Bool

Description: Checks whether an IPPrefix address overlaps with a specified IPPrefix address.

Parameters:

Returns:

  • Bool: If the address overlaps with the specified IPPrefix address, true is returned. Otherwise, false is returned.

func toString()

public func toString(): String

Description: Returns the text representation string of the current IPPrefix address.

Returns:

  • String: text representation string of the current IPPrefix address, for example, 192.168.0.0/16 or fc00::/16

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let v4: IPPrefix = IPAddress.parse("192.168.1.2").getPrefix(24)
    let v6: IPPrefix = IPAddress.parse("2001:0250:1006:dff0:4913:2aa5:8075:7c10").getPrefix(32)
    @Assert(v4.toString(), "192.168.1.2/24")
    @Assert(v6.toString(), "2001:250:1006:dff0:4913:2aa5:8075:7c10/32")
}

operator func ==(IPPrefix)

public operator func ==(rhs: IPPrefix): Bool

Description: Checks whether two IPPrefix objects are equal.

Parameters:

Returns:

  • Bool: If the two IPPrefix objects are equal, true is returned. Otherwise, false is returned.

operator func !=(IPPrefix)

public operator func !=(rhs: IPPrefix): Bool

Description: Checks whether two IPPrefix objects are not equal.

Parameters:

Returns:

  • Bool: If the two IPPrefix objects are not equal, true is returned. Otherwise, false is returned.

class IPSocketAddress

public class IPSocketAddress <: SocketAddress & Equatable<IPSocketAddress>{
    public init(address: Array<Byte>, port: UInt16)
    public init(address: String, port: UInt16)
    public init(address: IPAddress, port: UInt16)
}

Description: Implements an IP protocol socket address (IP address+port number). It provides an immutable object for socket binding or connection, or as a return value.

Parent types:

prop address

public prop address: IPAddress

Description: Obtains the IP address of the current IPSocketAddress object.

Type: IPAddress

prop family

public prop family: AddressFamily

Description: Obtains the address family of the current IPSocketAddress object.

Type: AddressFamily

prop port

public prop port: UInt16

Description: Obtains the port of the current IPSocketAddress object.

Type: UInt16

prop size

public prop size: Int64

Description: Obtains the original byte length of the current IPSocketAddress object.

Type: Int64

init(Array<Byte>, UInt16)

public init(address: Array<Byte>, port: UInt16)

Description: Constructs an IPSocketAddress address based on the IP address represented by the big-endian Array<Byte> and a host-endian UInt16 port.

Parameters:

  • address: Array<Byte>: big-endian IP address
  • port: UInt16: host-endian port

Throws:

init(String, UInt16)

public init(address: String, port: UInt16)

Description: Constructs an IPSocketAddress address based on the IP address represented by a string and a host-endian UInt16 port.

Parameters:

  • address: String: IP address string
  • port: UInt16: host-endian port

Throws:

init(IPAddress, UInt16)

public init(address: IPAddress, port: UInt16)

Description: Constructs an IPSocketAddress address based on an IPAddress object and a host-endian UInt16 port.

Parameters:

static func parse(String)

public static func parse(s: String): IPSocketAddress

Description: Converts the socket string of an IP protocol into an IPSocketAddress object.

Parameters:

  • s: String: socket string of the IP protocol

Returns:

Throws:

  • IllegalFormatException: The input parameter must be a valid socket address, for example, 192.168.0.0:80 or [fc00::1]:8080. Otherwise, this exception is thrown.

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let v4: IPSocketAddress = IPSocketAddress.parse("192.168.1.2:8080")
    let v6: IPSocketAddress = IPSocketAddress.parse("[2001:0250:1006:dff0:4913:2aa5:8075:7c10]:8080")
    @Assert(v4.address.toString(), "192.168.1.2")
    @Assert(v4.port, 8080u16)
    @Assert(v6.address.toString(), "2001:250:1006:dff0:4913:2aa5:8075:7c10")
    @Assert(v6.port, 8080u16)
    @Assert(v4.toString(), "192.168.1.2:8080")
    @Assert(v6.toString(), "[2001:250:1006:dff0:4913:2aa5:8075:7c10]:8080")
}

static func tryParse(String)

public static func tryParse(s: String): ?IPSocketAddress

Description: Converts the socket string of an IP protocol into an IPSocketAddress object. If the string is invalid, None is returned.

Parameters:

  • s: String: socket string of the IP protocol

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let v4: ?IPSocketAddress = IPSocketAddress.tryParse("192.168.1.2:8080")
    let v6: ?IPSocketAddress = IPSocketAddress.tryParse("[2001:0250:1006:dff0:4913:2aa5:8075:7c10]:8080")
    @Assert(v4.toString(), "Some(192.168.1.2:8080)")
    @Assert(v6.toString(), "Some([2001:250:1006:dff0:4913:2aa5:8075:7c10]:8080)")
}

func getAddressBytes()

public func getAddressBytes(): Array<Byte>

Description: Returns the Array<Byte> representation of the original address of an IPSocketAddress object. The content layout is the same as that of sockaddr_in or sockaddr_in6.

Returns:

func hashCode()

public func hashCode(): Int64

Description: Obtains the hashcode value.

Returns:

func isIPv4()

public func isIPv4(): Bool

Description: Checks whether an IPSocketAddress object is an IPv4 socket address.

Returns:

  • Bool: If the object is an IPv4 address, true is returned. Otherwise, false is returned.

func isIPv6()

public func isIPv6(): Bool

Description: Checks whether an IPSocketAddress object is an IPv6 socket address.

Returns:

  • Bool: If the object is an IPv6 address, true is returned. Otherwise, false is returned.

func toString()

public func toString(): String

Description: Returns the text representation string of the current IPSocketAddress.

Returns:

  • String: text representation string of the current IPSocketAddress, for example, 192.168.1.2:8080 or [fc00::/16]:8080

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let v4: IPSocketAddress = IPSocketAddress.parse("192.168.1.2:8080")
    let v6: IPSocketAddress = IPSocketAddress.parse("[2001:0250:1006:dff0:4913:2aa5:8075:7c10]:8080")
    @Assert(v4.toString(), "192.168.1.2:8080")
    @Assert(v6.toString(), "[2001:250:1006:dff0:4913:2aa5:8075:7c10]:8080")
}

operator func ==(IPSocketAddress)

public operator func ==(rhs: IPSocketAddress): Bool

Description: Checks whether two IPSocketAddress objects are equal.

Parameters:

Returns:

  • Bool: If the two IPSocketAddress objects are equal, true is returned. Otherwise, false is returned.

operator func !=(IPSocketAddress)

public operator func !=(rhs: IPSocketAddress): Bool

Description: Checks whether two IPSocketAddress objects are not equal.

Parameters:

Returns:

  • Bool: If the two IPSocketAddress objects are not equal, true is returned. Otherwise, false is returned.

class IPv4Address

public class IPv4Address <: IPAddress & ToString & Equatable<IPv4Address> & LessOrEqual<IPv4Address> {
    public static let broadcast = IPv4Address(0xFF, 0xFF, 0xFF, 0xFF)
    public static let localhost = IPv4Address(0x7F, 0, 0, 0x01)
    public static let unspecified = IPv4Address(0, 0, 0, 0)
    public init(bits: UInt32)
    public init(a: Byte, b: Byte, c: Byte, d: Byte)
}

Description: Indicates an Internet protocol version 4 (IPv4) address. It is defined by RFC 790, RFC 1918, and RFC 2365.

Parent types:

static let broadcast

public static let broadcast = IPv4Address(0xFF, 0xFF, 0xFF, 0xFF)

Description: Returns the broadcast address of IPv4Address: 255.255.255.255.

Type: IPv4Address

static let localhost

public static let localhost = IPv4Address(0x7F, 0, 0, 0x01)

Description: Returns the localhost address of IPv4Address: 127.0.0.1.

Type: IPv4Address

static let unspecified

public static let unspecified = IPv4Address(0, 0, 0, 0)

Description: Returns the unspecified IPv4Address address: 0.0.0.0, which corresponds to the constant INADDR_ANY in other languages.

Type: IPv4Address

init(UInt32)

public init(bits: UInt32)

Description: Constructs an IPv4Address address based on a host-endian UInt32 value.

Parameters:

init(Byte, Byte, Byte, Byte)

public init(a: Byte, b: Byte, c: Byte, d: Byte)

Description: Constructs an IPv4Address address object based on four 8-bit bytes. The text is represented as a.b.c.d.

Parameters:

  • a: Byte: 8-bit byte
  • b: Byte: 8-bit byte
  • c: Byte: 8-bit byte
  • d: Byte: 8-bit byte

static func readBigEndian(Array<Byte>)

public static func readBigEndian(buffer: Array<Byte>): IPv4Address

Description: Reads an IPv4Address object from a byte array in big-endian mode.

Parameters:

  • buffer: Array<Byte>: buffer used to store the data to be read

Throws:

Returns:

func getPrefix(UInt8)

public func getPrefix(prefixLen: UInt8): IPPrefix

Description: Creates a network prefix object for an IPv4Address address based on a specified network prefix length.

Parameters:

  • prefixLen: UInt8: network prefix length. The value must be greater than or equal to 0 and less than or equal to 32.

Throws:

Returns:

func isBroadcast()

public func isBroadcast(): Bool

Description: Checks whether an IPv4Address object is a broadcast address.

Returns:

  • Bool: If the object is a broadcast address, true is returned. Otherwise, false is returned.

func isGlobalUnicast()

public func isGlobalUnicast(): Bool

Description: Checks whether an IPv4Address object is a global unicast address.

Returns:

  • Bool: If the object is a global unicast address, true is returned. Otherwise, false is returned.

func isLinkLocal()

public func isLinkLocal(): Bool

Description: Checks whether an IPv4Address object is a link-local address.

Returns:

  • Bool: If the object is a link-local address, true is returned. Otherwise, false is returned.

func isLoopback()

public func isLoopback(): Bool

Description: Checks whether an IPv4Address object is a loopback address.

Returns:

  • Bool: If the object is a loopback address, true is returned. Otherwise, false is returned.

func isMulticast()

public func isMulticast(): Bool

Description: Checks whether an IPv4Address object is a multicast address.

Returns:

  • Bool: If the object is a multicast address, true is returned. Otherwise, false is returned.

func isPrivate()

public func isPrivate(): Bool

Description: Checks whether an IPv4Address object is a private address.

Returns:

  • Bool: If the object is a private address, true is returned. Otherwise, false is returned.

func isUnspecified()

public func isUnspecified(): Bool

Description: Checks whether an IPv4Address object is an unspecified IP address.

Returns:

  • Bool: If the object is an unspecified address, true is returned. Otherwise, false is returned.

func toBits()

public func toBits(): UInt32

Description: Converts an IPv4Address address into a host-endian UInt32 value.

Returns:

func toIPv6Compatible()

public func toIPv6Compatible(): IPv6Address

Description: Converts an IPv4Address address into an IPv4-compatible IPv6Address address. a.b.c.d is converted into ::a.b.c.d.

Returns:

func toIPv6Mapped()

public func toIPv6Mapped(): IPv6Address

Description: Converts an IPv4Address address into an IPv4-mapped IPv6Address address. a.b.c.d is converted into ::ffff:a.b.c.d.

Returns:

func writeBigEndian(Array<Byte>)

public func writeBigEndian(buffer: Array<Byte>): Int64

Description: Writes an IPv4Address object to a byte array in big-endian mode.

Parameters:

  • buffer: Array<Byte>: buffer used to store the data to be written

Throws:

Returns:

  • Int64: number of bytes of the written data

func toString()

public func toString(): String

Description: Returns the text representation string of the current IPv4Address.

Returns:

  • String: text representation string of the current IPv4Address, for example, a.b.c.d

operator func <=(IPv4Address)

public operator func <=(rhs: IPv4Address): Bool

Description: Checks whether an IPv4Address object is less than or equal to an IPv4Address object to be compared with.

Parameters:

Returns:

  • Bool: If the IPv4Address object is less than or equal to the IPv4Address object to be compared with, true is returned. Otherwise, false is returned.

operator func ==(IPv4Address)

public operator func ==(rhs: IPv4Address): Bool

Description: Checks whether two IPv4Address objects are equal.

Parameters:

Returns:

  • Bool: If the two IPv4Address objects are equal, true is returned. Otherwise, false is returned.

operator func !=(IPv4Address)

public operator func !=(rhs: IPv4Address): Bool

Description: Checks whether two IPv4Address objects are not equal.

Parameters:

Returns:

  • Bool: If the two IPv4Address objects are not equal, true is returned. Otherwise, false is returned.

class IPv6Address

public class IPv6Address <: IPAddress & ToString & Equatable<IPv6Address> & LessOrEqual<IPv6Address> {
    public static let localhost = IPv6Address(0u16, 0, 0, 0, 0, 0, 0, 1)
    public static let unspecified = IPv6Address(0u16, 0, 0, 0, 0, 0, 0, 0)
    public init(octets: Array<Byte>, scopeId!: ?UInt32 = None)
    public init(a: UInt16, b: UInt16, c: UInt16, d: UInt16, e: UInt16, f: UInt16, g: UInt16, h: UInt16, scopeId!: ?UInt32 = None)
}

Description: Indicates an Internet protocol version 6 (IPv6) address. It is defined by RFC4291, RFC5952, and RFC4007.

Parent types:

static let localhost

public static let localhost = IPv6Address(0u16, 0, 0, 0, 0, 0, 0, 1)

Description: Returns the localhost address of IPv6Address: ::1.

Type: IPv6Address

static let unspecified

public static let unspecified = IPv6Address(0u16, 0, 0, 0, 0, 0, 0, 0)

Description: Returns the unspecified IPv6Address address: ::, which corresponds to the constant INADDR_ANY in other languages.

Type: IPv6Address

prop scopeId

public prop scopeId: ?UInt32

Description: Obtains the default scope ID.

Type: Option<UInt32>

init(Array<Byte>, ?UInt32)

public init(octets: Array<Byte>, scopeId!: ?UInt32 = None)

Description: Constructs an IPv6Address address based on the big-endian Array<Byte>.

Throws:

Parameters:

init(UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, ?UInt32)

public init(a: UInt16, b: UInt16, c: UInt16, d: UInt16, e: UInt16, f: UInt16, g: UInt16, h: UInt16, scopeId!: ?UInt32 = None)

Description: Constructs an IPv6Address address object based on eight 16-bit segments. The text is represented as a:b:c:d:e:f:g:h%scopeId.

Parameters:

static func readBigEndian(Array<Byte>)

public static func readBigEndian(buffer: Array<Byte>): IPv6Address

Description: Reads an IPv6Address object from a byte array in big-endian mode.

Parameters:

  • buffer: Array<Byte>: buffer used to store the data to be read

Throws:

Returns:

func getPrefix(UInt8)

public func getPrefix(prefixLen: UInt8): IPPrefix

Description: Creates a network prefix object for an IPv6Address address object based on a specified network prefix length.

Parameters:

  • prefixLen: UInt8: network prefix length. The value must be greater than or equal to 0 and less than or equal to 128.

Throws:

Returns:

func isGlobalUnicast()

public func isGlobalUnicast(): Bool

Description: Checks whether an IPv6Address object is a global unicast address.

Returns:

  • Bool: If the object is a global unicast address, true is returned. Otherwise, false is returned.

func isIPv4Mapped()

public func isIPv4Mapped(): Bool

Description: Checks whether an IPv6Address object is an IPv4-mapped address.

Returns:

  • Bool: If the object is an IPv4-mapped address, true is returned. Otherwise, false is returned.

func isLinkLocal()

public func isLinkLocal(): Bool

Description: Checks whether an IPv6Address object is a link-local address.

Returns:

  • Bool: If the object is a link-local address, true is returned. Otherwise, false is returned.

func isLoopback()

public func isLoopback(): Bool

Description: Checks whether an IPv6Address object is a loopback address.

Returns:

  • Bool: If the object is a loopback address, true is returned. Otherwise, false is returned.

func isMulticast()

public func isMulticast(): Bool

Description: Checks whether an IPv6Address object is a multicast address.

Returns:

  • Bool: If the object is a multicast address, true is returned. Otherwise, false is returned.

func isPrivate()

public func isPrivate(): Bool

Description: Checks whether an IPv6Address object is a private address.

Returns:

  • Bool: If the object is a private address, true is returned. Otherwise, false is returned.

func isTeredo()

public func isTeredo(): Bool

Description: Checks whether an IPv6Address object is a Teredo address. The prefix of Teredo is 2001::/32.

Returns:

  • Bool: If the object is a Teredo address, true is returned. Otherwise, false is returned.

func isUnspecified()

public func isUnspecified(): Bool

Description: Checks whether an IPv6Address object is an unspecified IP address.

Returns:

  • Bool: If the object is an unspecified address, true is returned. Otherwise, false is returned.

func scope(?UInt32)

public func scope(scopeId: ?UInt32): IPv6Address

Description: Converts the address value of an IPv6Address object and a specified scope ID into a new IPv6Address object. If the specified scope ID is None, the existing scope ID is removed.

Parameters:

Returns:

func toIPv4()

public func toIPv4(): ?IPv4Address

Description: Converts an IPv6Address address into an IPv4-compatible IPv4Address address. For example, ::a.b.c.d and ::ffff:a.b.c.d are converted into a.b.c.d; ::1 is converted into 0.0.0.1. None is returned for all addresses that do not start with all zeros or ::ffff.

Returns:

func toIPv4Mapped()

public func toIPv4Mapped(): ?IPv4Address

Description: Converts an IPv6Address address into an IPv4-mapped IPv4Address address. For example, ::ffff:a.b.c.d is converted into a.b.c.d. None is returned for all addresses that do not start with ::ffff.

Returns:

func writeBigEndian(Array<Byte>)

public func writeBigEndian(buffer: Array<Byte>): Int64

Description: Writes an IPv6Address object to a byte array in big-endian mode.

Parameters:

  • buffer: Array<Byte>: buffer used to store the data to be written

Throws:

Returns:

  • Int64: number of bytes of the written data

func toString()

public func toString(): String

Description: Returns the text representation string of the current IPv6Address.

Returns:

  • String: text representation string of the current IPv6Address, for example, 2001:db8:1:2:ffff:ffff:ffff:ffff

operator func <=(IPv6Address)

public operator func <=(rhs: IPv6Address): Bool

Description: Checks whether an IPv6Address object is less than or equal to an IPv6Address object to be compared with.

Parameters:

Returns:

  • Bool: If the IPv6Address object is less than or equal to the IPv6Address object to be compared with, true is returned. Otherwise, false is returned.

operator func ==(IPv6Address)

public operator func ==(rhs: IPv6Address): Bool

Description: Checks whether two IPv6Address objects are equal.

Parameters:

Returns:

  • Bool: If the two IPv6Address objects are equal, true is returned. Otherwise, false is returned.

operator func !=(IPv6Address)

public operator func !=(rhs: IPv6Address): Bool

Description: Checks whether two IPv6Address objects are not equal.

Parameters:

Returns:

  • Bool: If the two IPv6Address objects are not equal, true is returned. Otherwise, false is returned.

class RawSocket

public class RawSocket {
    public init(domain: SocketDomain, `type`: SocketType, protocol: ProtocolType)
}

Description: Provides basic functionalities of a socket.

This class can be used to access a socket combining specific communication domain, type, and protocol. The socket packet provides support for common network protocols including TCP and UDP. Therefore, this class is applicable to network programming requirements of other classes.

NOTE

  • Currently, verified functionalities of RawSocket include TCP, UDP, UDS, and ICMP sockets. For other classes, unexpected usage problems may occur.
  • In addition, due to the API openness, using connect followed by listen is allowed. In some scenarios, unexpected problems may occur. It is recommended that developers comply with normal calling logic to prevent problems.

prop localAddr (deprecated)

public prop localAddr: RawAddress

Description: Obtains the local address of the current RawSocket instance.

NOTE

This property will be deprecated in future releases and localAddress will be used instead.

Type: RawAddress

Throws:

  • SocketException: If the current RawSocket instance has been closed or the local address cannot be obtained, this exception is thrown.

prop localAddress

public prop localAddress: RawAddress

Description: Obtains the local address of the current RawSocket instance.

Type: RawAddress

Throws:

  • SocketException: If the current RawSocket instance has been closed or the local address cannot be obtained, this exception is thrown.

prop readTimeout

public mut prop readTimeout: ?Duration

Description: Obtains or sets the read timeout of the current RawSocket instance.

Type: ?Duration

Throws:

prop remoteAddr (deprecated)

public prop remoteAddr: RawAddress

Description: Obtains the remote address of the current RawSocket instance.

NOTE

This property will be deprecated in future releases and remoteAddress will be used instead.

Type: RawAddress

Throws:

  • SocketException: If the current RawSocket instance has been closed or the remote address cannot be obtained, this exception is thrown.

prop remoteAddress

public prop remoteAddress: RawAddress

Description: Obtains the remote address of the current RawSocket instance.

Type: RawAddress

Throws:

  • SocketException: If the current RawSocket instance has been closed or the remote address cannot be obtained, this exception is thrown.

prop writeTimeout

public mut prop writeTimeout: ?Duration

Description: Obtains or sets the write timeout of the current RawSocket instance.

Type: ?Duration

Throws:

init(SocketDomain, SocketType, ProtocolType)

public init(domain: SocketDomain, `type`: SocketType, protocol: ProtocolType)

Description: Creates a socket combining specific communication domain, type, and protocol.

Parameters:

Throws:

  • SocketException: If the socket combining the specific communication domain, type, and protocol cannot be created, this exception is thrown.

func accept(?Duration)

public func accept(timeout!: ?Duration = None): RawSocket

Description: Accepts the first connection request from the suspended connection queue when the current RawSocket instance is listening, and returns a RawSocket for communication.

Parameters:

  • timeout!: ?Duration: maximum duration for waiting for the connection request. The default value None indicates always waiting.

Returns:

Throws:

func bind(RawAddress)

public func bind(addr: RawAddress): Unit

Description: Binds the current RawSocket instance to a specified socket address.

Parameters:

Throws:

func close()

public func close(): Unit

Description: Closes the current RawSocket instance.

func connect(RawAddress, ?Duration)

public func connect(addr: RawAddress, timeout!: ?Duration = None): Unit

Description: Sends a connection request to a destination address.

Parameters:

  • addr: RawAddress: destination address to which the connection request is sent
  • timeout!: ?Duration: maximum duration for waiting for the connection request. The default value None indicates always waiting.

Throws:

func getSocketOption(Int32, Int32, CPointer<Byte>, CPointer<Int32>)

public unsafe func getSocketOption(level: Int32, option: Int32, value: CPointer<Byte>, len: CPointer<Int32>): Unit

Description: Obtains the value of a socket option.

Parameters:

Throws:

  • SocketException: If the current RawSocket instance has been closed or the socket option fails to be obtained, this exception is thrown.

func listen(Int32)

public func listen(backlog: Int32): Unit

Description: Listens to the address bound to the current RawSocket instance.

Parameters:

  • backlog: Int32: maximum length to which the waiting queue grows

Throws:

func receive(Array<Byte>, Int32)

public func receive(buffer: Array<Byte>, flags: Int32): Int64

Description: Receives data from the peer end in a connection.

Parameters:

  • buffer: Array<Byte>: array for storing received data
  • flags: Int32: flag specifying a function behavior

Returns:

Throws:

func receiveFrom(Array<Byte>, Int32)

public func receiveFrom(buffer: Array<Byte>, flags: Int32): (RawAddress, Int64)

Description: Receives data from another RawSocket instance.

Parameters:

  • buffer: Array<Byte>: array for storing received data
  • flags: Int32: flag specifying a function behavior

Returns:

Throws:

func send(Array<Byte>, Int32)

public func send(buffer: Array<Byte>, flags: Int32): Unit

Description: Sends data to the peer end in a connection.

Parameters:

  • buffer: Array<Byte>: data
  • flags: Int32: flag specifying a function behavior

Throws:

func sendTo(RawAddress, Array<Byte>, Int32)

public func sendTo(addr: RawAddress, buffer: Array<Byte>, flags: Int32): Unit

Description: Sends data to a destination address. If the type of RawSocket is DATAGRAM, a data packet to be sent can contain a maximum of 65,507 bytes.

Parameters:

  • addr: RawAddress: destination address to which data is sent
  • buffer: Array<Byte>: data
  • flags: Int32: flag specifying a function behavior

Throws:

  • SocketException: If the current RawSocket instance has been closed, the data fails to be sent, or connect is called before sendTo is called on macOS, this exception is thrown.
  • SocketTimeoutException: If the specified write timeout is exceeded, this exception is thrown.

func setSocketOption(Int32, Int32, CPointer<Byte>, Int32)

public unsafe func setSocketOption(level: Int32, option: Int32, value: CPointer<Byte>, len: Int32): Unit

Description: Sets a socket option.

Parameters:

  • level: Int32: socket option level
  • option: Int32: socket option name
  • value: CPointer<Byte>: socket option value
  • len: Int32: socket option value length

Throws:

  • SocketException: If the current RawSocket instance has been closed or the socket option fails to be set, this exception is thrown.

class SocketAddress

abstract sealed class SocketAddress <: ToString & Equatable<SocketAddress> & Hashable

Description: Indicates a protocol-independent socket address. It provides an immutable object for socket binding or connection, or as a return value.

Parent types:

prop size

public prop size: Int64

Description: Obtains the original byte length of the current SocketAddress object.

Type: Int64

prop family

public prop family: AddressFamily

Description: Obtains the address family of the current SocketAddress object.

Type: AddressFamily

func getAddressBytes()

public func getAddressBytes(): Array<Byte>

Description: Returns the original IP address of a SocketAddress object.

Returns:

operator func ==(SocketAddress)

public operator func ==(rhs: SocketAddress): Bool

Description: Checks whether two SocketAddress objects are equal.

Parameters:

Returns:

  • Bool: If the two SocketAddress objects are equal, true is returned. Otherwise, false is returned.

operator func !=(SocketAddress)

public operator func !=(rhs: SocketAddress): Bool

Description: Checks whether two SocketAddress objects are not equal.

Parameters:

Returns:

  • Bool: If the two SocketAddress objects are not equal, true is returned. Otherwise, false is returned.

class TcpServerSocket

public class TcpServerSocket <: ServerSocket {
    public init(bindAt!: SocketAddress)
    public init(bindAt!: UInt16)
}

Description: Specifies a server that listens for TCP connections.

After a socket is created, its properties can be configured through properties and setSocketOptionXX functions. To start listening, bind() must be called to bind the socket to a local port. The accept() function accepts the TCP connection and waits for a connection. If a connection already exists in the queue, a response is returned immediately. The socket can be explicitly closed using close.

Parent types:

prop backlogSize

public mut prop backlogSize: Int64

Description: Sets and reads the size of backlog.

If the property is called after bind is called, an exception is thrown. Whether a variable takes effect depends on system behaviors.

Type: Int64

Throws:

  • SocketException: If the property is called after bind is called, this exception is thrown.

prop bindToDevice

public mut prop bindToDevice: ?String

Description: Sets and reads a bound NIC.

Type: ?String

prop localAddress

public override prop localAddress: SocketAddress

Description: Reads the local address of Socket that is to be or has been bound.

Type: SocketAddress

Throws:

prop receiveBufferSize

public mut prop receiveBufferSize: Int64

Description: Sets and reads the SO_RCVBUF property.

Type: Int64

Throws:

prop reuseAddress

public mut prop reuseAddress: Bool

Description: Sets and reads the SO_REUSEADDR property. The default value is true.

Behaviors after the property takes effect depend on the system. Before usage, refer to description documents about the SO_REUSEADDR/SOCK_REUSEADDR property in different systems.

Type: Bool

prop reusePort

public mut prop reusePort: Bool

Description: Sets and reads the SO_REUSEPORT property.

This property can be modified only before binding. On Windows, the SO_REUSEADDR property can be used because it does not exist, and an exception is thrown. Behaviors after the default property takes effect depend on the system. Before usage, refer to description documents about the SO_REUSEPORT property in different systems. If both SO_REUSEADDR/SO_REUSEPORT are enabled, unpredictable system errors occur. Exercise caution during configuration.

Type: Bool

Throws:

  • SocketException: On Windows, this type is not supported, and this exception is thrown.

prop sendBufferSize

public mut prop sendBufferSize: Int64

Description: Sets and reads the SO_SNDBUF property.

Type: Int64

Throws:

init(SocketAddress)

public init(bindAt!: SocketAddress)

Description: Creates a TcpServerSocket instance, which cannot be connected by clients before binding is complete.

Parameters:

  • bindAt!: SocketAddress: local address to be bound. If the port number is set to 0, a random idle local address is bound.

init(UInt16)

public init(bindAt!: UInt16)

Description: Creates a TcpServerSocket instance, which cannot be connected by clients before binding is complete.

Parameters:

  • bindAt!: UInt16: local port to be bound. The value 0 indicates that a random idle local port is bound.

func accept()

public override func accept(): TcpSocket

Description: Listens for or accepts client connections and waits for connections.

Returns:

Throws:

  • SocketException: If the listening fails due to a system error, this exception is thrown.

func accept(?Duration)

public override func accept(timeout!: ?Duration): TcpSocket

Description: Listens for or accepts client connections.

Parameters:

Returns:

Throws:

func bind()

public override func bind(): Unit

Description: If a local port fails to be bound, the socket needs to be closed using close. Retrying for many times is not supported.

Throws:

  • SocketException: If the binding fails due to a system error, this exception is thrown.

func close()

public override func close(): Unit

Description: Closes the socket. This function can be called more than once.

func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)

public func getSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: CPointer<UIntNative>
): Unit

Description: Obtains a specified socket parameter.

Parameters:

Throws:

  • SocketException: If getsockopt fails to be returned, this exception is thrown.

func getSocketOptionBool(Int32, Int32)

public func getSocketOptionBool(
    level: Int32,
    option: Int32
): Bool

Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • Bool: specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func getSocketOptionIntNative(Int32, Int32)

public func getSocketOptionIntNative(
    level: Int32,
    option: Int32
): IntNative

Description: Obtains a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • IntNative: value of the socket parameter obtained

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func isClosed()

public override func isClosed(): Bool

Description: Checks whether a socket has been closed.

Returns:

  • Bool: If the socket has been closed, true is returned. Otherwise, false is returned.

func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)

public func setSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: UIntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: CPointer<Unit>: parameter value
  • valueLength: UIntNative: parameter value length

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionBool(Int32, Int32, Bool)

public func setSocketOptionBool(
    level: Int32,
    option: Int32,
    value: Bool
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: Bool: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionIntNative(Int32, Int32, IntNative)

public func setSocketOptionIntNative(
    level: Int32,
    option: Int32,
    value: IntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: IntNative: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func toString()

public override func toString(): String

Description: Returns status information about the current TcpServerSocket.

Returns:

class TcpSocket

public class TcpSocket <: StreamingSocket & Equatable<TcpSocket> & Hashable {
    public init(address: String, port: UInt16)
    public init(address: SocketAddress)
    public init(address: SocketAddress, localAddress!: ?SocketAddress)
}

Description: Specifies a client that requests a TCP connection.

After an instance object is created, a connection can be created using the connect function and explicitly closed using close. This class is inherited from StreamingSocket. For more information, see StreamingSocket.

Parent types:

prop bindToDevice

public mut prop bindToDevice: ?String

Description: Sets and reads a bound NIC.

Type: ?String

prop keepAlive

public mut prop keepAlive: ?SocketKeepAliveConfig

Description: Sets and reads the keepalive property. None indicates that keepalive is disabled.

If there is no user setting, the system uses the default configuration. Setting this configuration may be delayed or ignored by the system, which depends on the processing capability of the system.

Type: ?SocketKeepAliveConfig

prop linger

public mut prop linger: ?Duration

Description: Sets and reads the SO_LINGER property. The default value depends on the system. None indicates that this option is disabled.

NOTE

  • If SO_LINGER is set to Some(v), when the socket is closed, waiting lasts for v (time) before the connection is closed if there are still byte streams in waiting status. If these byte streams are still not sent after the period, the connection is terminated by an exception (closed through RST packets).
  • If SO_LINGER is set to None, when the socket is closed, the connection is closed immediately. If there are characters waiting to be sent, FIN-ACK is used to close the connection. If there are still characters waiting to be sent, RST is used to close the connection.

Type: ?Duration

Throws:

prop localAddress

public override prop localAddress: SocketAddress

Description: Reads the local address of Socket that is to be or has been bound.

Type: SocketAddress

Throws:

  • SocketException: If Socket has been closed or no local address is available (no local address is configured and the socket is not connected), this exception is thrown.

prop noDelay

public mut prop noDelay: Bool

Description: Sets and reads the TCP_NODELAY property. The default value is true.

This option disables the Nagel algorithm, and all written bytes are forwarded without delay. When the property is set to false, the Nagel algorithm introduces a delay time before packet sending.

Type: Bool

prop quickAcknowledge

public mut prop quickAcknowledge: Bool

Description: Sets and reads the TCP_QUICKACK property. The default value is false.

This option is similar to noDelay but affects only TCP ACK and the first response. It is not supported on Windows or macOS.

Type: Bool

prop readTimeout

public override mut prop readTimeout: ?Duration

Description: Sets and reads the read operation timeout duration.

If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout duration (263 – 1 nanoseconds) is used. The default value is None.

Type: ?Duration

Throws:

prop receiveBufferSize

public mut prop receiveBufferSize: Int64

Description: Sets and reads the SO_RCVBUF property, and provides a method to specify the size for buffering received packets. Whether the option takes effect depends on the system.

Type: Int64

Throws:

prop remoteAddress

public override prop remoteAddress: SocketAddress

Description: Reads the remote address to which Socket has been or is to be connected.

Type: SocketAddress

Throws:

prop sendBufferSize

public mut prop sendBufferSize: Int64

Description: Sets and reads the SO_SNDBUF property, and provides a method to specify the size for buffering packets to be sent. Whether the option takes effect depends on the system.

Type: Int64

Throws:

prop writeTimeout

public override mut prop writeTimeout: ?Duration

Description: Sets and reads the write operation timeout duration.

If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout duration (263 – 1 nanoseconds) is used. The default value is None.

Type: ?Duration

Throws:

init(SocketAddress)

public init(address: SocketAddress)

Description: Creates a socket that is not connected.

Parameters:

Throws:

  • SocketException: If the address parameter is invalid or the address on Windows is an all-zero address, this exception is thrown.

init(SocketAddress, ?SocketAddress)

public init(address: SocketAddress, localAddress!: ?SocketAddress)

Description: Creates a socket that is not connected and binds it to a specified local address. If localAddress is None, an address is randomly selected for the binding.

If localAddress is not None, SO_REUSEADDR is set to true by default. Otherwise, the error "address already in use" may occur. To change this configuration, call setSocketOptionBool(SocketOptions.SOL_SOCKET, SocketOptions.SO_REUSEADDR, false). In addition, the local and remote addresses must be IPv4 addresses.

Parameters:

Throws:

  • SocketException: If the address parameter is invalid or the address on Windows is an all-zero address, this exception is thrown.

init(String, UInt16)

public init(address: String, port: UInt16)

Description: Creates a socket that is not connected.

Parameters:

  • address: String: address to be connected to
  • port: UInt16: port to be connected to

Throws:

  • SocketException: If the address parameter is invalid or the address on Windows is an all-zero address, this exception is thrown.

func close()

public func close(): Unit

Description: Closes a socket. All operations except close/isClosed are not allowed to be called again. This function can be called more than once.

func connect(?Duration)

public func connect(timeout!: ?Duration = None): Unit

Description: Connects to a remote socket, with the local address automatically bound. Therefore, no additional binding operation is required.

Parameters:

  • timeout!: ?Duration: connection timeout duration. None indicates no timeout duration and no connection retry. If the server rejects the connection, connection failure is returned. In addition, this operation includes the binding operation. Therefore, repeatedly calling the bind function is not required.

Throws:

  • IllegalArgumentException: If the remote address is invalid, the connection timeout duration is less than 0, or the timeout duration is less than 0, this exception is thrown.
  • SocketException: If the connection cannot be established due to a system issue (for example, socket closed, no access permission, or system error), this exception is thrown. The connection may be successful after the function is called again.
  • SocketTimeoutException: If the connection times out, this exception is thrown.

func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)

public func getSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: CPointer<UIntNative>
): Unit

Description: Reads a specified socket parameter.

Parameters:

Throws:

  • SocketException: If getsockopt fails to be returned, this exception is thrown.

func getSocketOptionBool(Int32, Int32)

public func getSocketOptionBool(
    level: Int32,
    option: Int32
): Bool

Description: Reads a specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • Bool: parameter value read

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func getSocketOptionIntNative(Int32, Int32)

public func getSocketOptionIntNative(
    level: Int32,
    option: Int32
): IntNative

Description: Reads a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func hashCode()

public override func hashCode(): Int64

Description: Obtains the hash value of the current TcpSocket instance.

Returns:

func isClosed()

public func isClosed(): Bool

Description: Checks whether a socket has been explicitly closed by calling close.

Returns:

  • Bool: whether the socket has been explicitly closed by calling close. If so, true is returned. Otherwise, false is returned.

func read(Array<Byte>)

public override func read(buffer: Array<Byte>): Int64

Description: Reads packets. Timeout is determined by readTimeout. For details, see readTimeout.

NOTE

  • Due to the differences among underlying APIs of the system, if the connection is closed by the peer end, behaviors of the read and write functions are also different.
  • On Windows, after the peer end closes the connection, if write is called at the local end, content in the buffer is cleared. In this case, if read is called, a connection close exception is thrown.
  • On Linux or macOS, after the peer end closes the connection, if read is called after write is called, content in the buffer is still read.

Parameters:

  • buffer: Array<Byte>: buffer for storing the read data

Returns:

  • Int64: length of the read data

Throws:

  • SocketException: If the size of buffer is 0 or the read operation fails due to a system error, this exception is thrown.

func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)

public func setSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: UIntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: CPointer<Unit>: parameter value
  • valueLength: UIntNative: parameter value length

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionBool(Int32, Int32, Bool)

public func setSocketOptionBool(
    level: Int32,
    option: Int32,
    value: Bool
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: Bool: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionIntNative(Int32, Int32, IntNative)

public func setSocketOptionIntNative(
    level: Int32,
    option: Int32,
    value: IntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: IntNative: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func toString()

public override func toString(): String

Description: Returns status information about the current TcpSocket.

Returns:

func write(Array<Byte>)

public override func write(payload: Array<Byte>): Unit

Description: Writes packets. Timeout is determined by writeTimeout. For details, see writeTimeout.

Parameters:

  • payload: Array<Byte>: buffer for storing the written data

Throws:

  • SocketException: If the size of buffer is 0 or the write operation fails due to a system error, this exception is thrown.

operator func !=(TcpSocket)

public override operator func !=(other: TcpSocket): Bool

Description: Checks whether two TcpSocket instances are not equal.

Parameters:

Returns:

  • Bool: If the two TcpSocket instances are not equal, true is returned. Otherwise, false is returned.

operator func ==(TcpSocket)

public override operator func ==(other: TcpSocket): Bool

Description: Checks whether two TcpSocket instances are equal.

Parameters:

Returns:

  • Bool: If the two TcpSocket instances are equal, true is returned. Otherwise, false is returned.

class UdpSocket

public class UdpSocket <: DatagramSocket {
    public init(bindAt!: SocketAddress)
    public init(bindAt!: UInt16)
}

Description: Provides UDP packet communication.

After a UdpSocket instance is created, bind() needs to be called for binding. Packets can be received without connection to the remote end. In addition, connection can be established for UdpSocket through the connect()/disconnect() function. Packets to be transmitted cannot exceed 64 KB, which is required by the UDP protocol. UdpSocket needs to be explicitly closed using close(). For more information, see DatagramSocket.

Parent types:

prop localAddress

public override prop localAddress: SocketAddress

Description: Reads the local address of Socket that is to be or has been bound.

Type: SocketAddress

Throws:

  • SocketException: If Socket has been closed or no local address is available (no local address is configured and the socket is not connected), this exception is thrown.

prop receiveBufferSize

public mut prop receiveBufferSize: Int64

Description: Sets and reads the SO_RCVBUF property.

Type: Int64

Throws:

prop receiveTimeout

public override mut prop receiveTimeout: ?Duration

Description: Sets and reads the receive/receiveFrom operation timeout duration.

If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout duration (263 – 1 nanoseconds) is used. The default value is None.

Type: ?Duration

Throws:

prop remoteAddress

public override prop remoteAddress: ?SocketAddress

Description: Reads the remote address connected to Socket. If Socket is not connected, None is returned.

Type: ?SocketAddress

Throws:

prop reuseAddress

public mut prop reuseAddress: Bool

Description: Sets and reads the SO_REUSEADDR property.

Behaviors after the default property takes effect depend on the system. Before usage, refer to description documents about the SO_REUSEADDR/SOCK_REUSEADDR property in different systems.

Type: Bool

prop reusePort

public mut prop reusePort: Bool

Description: Sets and reads the SO_REUSEPORT property.

On Windows, SO_REUSEADDR can be used but the SO_REUSEPORT property is excluded. Therefore, an exception is thrown. Behaviors after the default property takes effect depend on the system. Before usage, refer to description documents about the SO_REUSEPORT property in different systems.

Type: Bool

Throws:

  • SocketException: On Windows, this type is not supported, and this exception is thrown.

prop sendBufferSize

public mut prop sendBufferSize: Int64

Description: Sets and reads the SO_SNDBUF property.

Type: Int64

Throws:

prop sendTimeout

public override mut prop sendTimeout: ?Duration

Description: Sets and reads the send/sendTo operation timeout duration.

If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout duration (263 – 1 nanoseconds) is used. The default value is None.

Type: ?Duration

init(SocketAddress)

public init(bindAt!: SocketAddress)

Description: Creates an unbound UdpSocket instance.

Parameters:

Throws:

init(UInt16)

public init(bindAt!: UInt16)

Description: Creates an unbound UdpSocket instance.

Parameters:

  • bindAt!: UInt16: port to be bound

func bind()

public func bind(): Unit

Description: If a local port fails to be bound, the socket needs to be closed using close. Retrying for many times is not supported.

Throws:

  • SocketException: If the binding fails due to a system error, this exception is thrown.

func close()

public override func close(): Unit

Description: Closes a socket. All operations except close/isClosed are not allowed to be called again. This function can be called more than once.

func connect(SocketAddress)

public func connect(remote: SocketAddress): Unit

Description: Connects to a specified remote address. The configuration can be canceled using disconnect.

Only packets from the remote address are received. This operation must be performed after bind is called. After this operation is performed, the port starts to receive ICMP packets. If abnormal packets are received, send/sendTo may fail to be executed.

Parameters:

Throws:

  • IllegalArgumentException: If the remote address is invalid, this exception is thrown.
  • SocketException: If the port is not bound, the connection cannot be established due to a system issue, or the remote address is an all-zero address on Windows, this exception is thrown.

func disconnect()

public func disconnect(): Unit

Description: Stops the connection. The configuration of receiving only packets from a specified peer end is canceled. This function can be called before connect more than once.

func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)

public func getSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: CPointer<UIntNative>
): Unit

Description: Obtains a specified socket parameter.

Parameters:

Throws:

  • SocketException: If getsockopt fails to be returned, this exception is thrown.

func getSocketOptionBool(Int32, Int32)

public func getSocketOptionBool(
    level: Int32,
    option: Int32
): Bool

Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • Bool: specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func getSocketOptionIntNative(Int32, Int32)

public func getSocketOptionIntNative(
    level: Int32,
    option: Int32
): IntNative

Description: Obtains a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET

  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • IntNative: value of the specified socket parameter

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func isClosed()

public override func isClosed(): Bool

Description: Checks whether a socket has been explicitly closed by calling close.

Returns:

  • Bool: If the socket has been explicitly closed by calling close, true is returned. Otherwise, false is returned.

func receive(Array<Byte>)

public func receive(buffer: Array<Byte>): Int64

Description: Receives packets from an address connected to using connect.

Parameters:

  • buffer: Array<Byte>: address for storing the received packets

Returns:

  • Int64: size of the received packets

func receiveFrom(Array<Byte>)

public override func receiveFrom(buffer: Array<Byte>): (SocketAddress, Int64)

Description: Receives packets.

Parameters:

  • buffer: Array<Byte>: buffer address for storing the received packets

Returns:

  • (SocketAddress, Int64): address from which the received packets are sent, and size of the received packets, which may be 0 or greater than the size of buffer

Throws:

func send(Array<Byte>)

public func send(payload: Array<Byte>): Unit

Description: Sends packets to the address connected to using connect.

Parameters:

  • payload: Array<Byte>: content of the sent packets

Throws:

  • SocketException: If the size of payload exceeds the system limit or the system fails to send the packets (for example, when connect is called and abnormal ICMP packets are received, the sending fails), this exception is thrown.

func sendTo(SocketAddress, Array<Byte>)

public override func sendTo(recipient: SocketAddress, payload: Array<Byte>): Unit

Description: Sends packets. Insufficient buffer addresses may cause blocking.

Parameters:

  • recipient: SocketAddress: remote address to which the packets are sent
  • payload: Array<Byte>: content of the sent packets

Throws:

  • SocketException: If the size of payload exceeds the system limit, the system fails to send the packets (for example, when connect is called and abnormal ICMP packets are received, the sending fails), the remote address is an all-zero address on Windows, or connect is called before sendTo is called on macOS, this exception is thrown.

func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)

public func setSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: UIntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: CPointer<Unit>: parameter value
  • valueLength: UIntNative: parameter value length

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionBool(Int32, Int32, Bool)

public func setSocketOptionBool(
    level: Int32,
    option: Int32,
    value: Bool
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: Bool: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionIntNative(Int32, Int32, IntNative)

public func setSocketOptionIntNative(
    level: Int32,
    option: Int32,
    value: IntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: IntNative: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func toString()

public override func toString(): String

Description: Returns status information about the current UdpSocket.

Returns:

class UnixDatagramSocket

public class UnixDatagramSocket <: DatagramSocket {
    public init(bindAt!: SocketAddress)
    public init(bindAt!: String)
}

Description: Provides the host communication capability based on data packets.

After the UnixDatagramSocket instance is created, the bind() function should be explicitly called for binding. For Unix data packets, the socket does not require a connection or many handshakes with the remote end. However, users can also use the connect/disconnect function to connect to or disconnect from the remote end. Different from UDP, UDS has no limit on the data packet size. The limit comes from the operating system and API implementation. Socket resources need to be explicitly reclaimed using the close function. For more information, see DatagramSocket.

NOTE

  • This class is not supported on Windows.

Parent types:

prop localAddress

public override prop localAddress: SocketAddress

Description: Reads the local address of socket that is to be or has been bound.

Type: SocketAddress

Throws:

prop receiveBufferSize

public mut prop receiveBufferSize: Int64

Description: Sets and reads the SO_RCVBUF property, and provides a method to specify the size for buffering packets to be sent. Whether the option takes effect depends on the system.

Type: Int64

Throws:

prop receiveTimeout

public override mut prop receiveTimeout: ?Duration

Description: Sets and reads the receive/receiveFrom operation timeout duration.

If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout duration (263 – 1 nanoseconds) is used. The default value is None.

Type: ?Duration

Throws:

prop remoteAddress

public override prop remoteAddress: ?SocketAddress

Description: Reads the remote address connected to Socket. If Socket is not connected, None is returned.

Type: ?SocketAddress

Throws:

prop sendBufferSize

public mut prop sendBufferSize: Int64

Description: Sets and reads the SO_SNDBUF property, and provides a method to specify the size for buffering packets to be sent. Whether the option takes effect depends on the system.

Type: Int64

Throws:

prop sendTimeout

public override mut prop sendTimeout: ?Duration

Description: Sets and reads the send/sendTo operation timeout duration.

If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout duration (263 – 1 nanoseconds) is used. The default value is None.

Type: ?Duration

Throws:

init(SocketAddress)

public init(bindAt!: SocketAddress)

Description: Creates a UnixDatagramSocket instance that is not connected.

isSock() can be used to check whether the file type exists, and the unlink() function can be used to delete the file type.

Parameters:

  • bindAt!: SocketAddress: socket address to be connected to. The address does not exist and will be created upon binding using bind.

Throws:

  • SocketException: If the path is empty or already exists, this exception is thrown.

init(String)

public init(bindAt!: String)

Description: Creates a UnixDatagramSocket instance that is not connected.

isSock() can be used to check whether the file type exists, and the unlink() function can be used to delete the file type.

Parameters:

  • bindAt!: String: file address to be connected to. The file address does not exist and will be created upon binding using bind.

Throws:

func bind()

public func bind(): Unit

Description: Binds a Unix datagram socket and creates a listening queue.

This function automatically creates a socket file in the local address. If the file already exists, the binding fails. isSock can be used to check whether the file type exists, and the unlink() function can be used to delete the file type. Upon failure, the socket needs to be closed using close. Retrying for many times is not supported.

Throws:

  • SocketException: If the file address already exists or the file fails to be created, this exception is thrown.

func close()

public override func close(): Unit

Description: Closes a socket. All operations except close/isClosed are not allowed to be called again. This function can be called more than once.

func connect(SocketAddress)

public func connect(remote: SocketAddress): Unit

Description: Connects to a specified remote address. The configuration can be canceled using disconnect.

Only packets from the remote address are received. bind is executed by default without the need of being called. After this operation is performed, the port starts to receive ICMP packets. If abnormal packets are received, send/sendTo may fail to be executed.

Parameters:

Throws:

func connect(String)

public func connect(remotePath: String): Unit

Description: Connects to a specified remote address. The configuration can be canceled using disconnect.

Only packets from the remote address are received. This function must be called after bind. After this operation is performed, the port starts to receive ICMP packets. If abnormal packets are received, send/sendTo may fail to be executed.

Parameters:

  • remotePath: String: remote file address

Throws:

func disconnect()

public func disconnect(): Unit

Description: Stops the connection. The configuration of receiving only packets from a specified peer end is canceled. This function can be called before connect more than once.

Throws:

func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)

public func getSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: CPointer<UIntNative>
): Unit

Description: Obtains a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: CPointer<Unit>: parameter value
  • valueLength: CPointer<UIntNative>: parameter value length

Throws:

  • SocketException: If getsockopt fails to be returned, this exception is thrown.

func getSocketOptionIntNative(Int32, Int32)

public func getSocketOptionIntNative(
    level: Int32,
    option: Int32
): IntNative

Description: Obtains a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • IntNative: value of the specified socket parameter

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func isClosed()

public override func isClosed(): Bool

Description: Checks whether a socket has been explicitly closed by calling close.

Returns:

  • Bool: whether the socket has been explicitly closed by calling close. If so, true is returned. Otherwise, false is returned.

func receive(Array<Byte>)

public func receive(buffer: Array<Byte>): Int64

Description: Receives packets from an address connected to using connect.

Parameters:

  • buffer: Array<Byte>: address for storing the received packets

Returns:

  • Int64: size of the received packets

func receiveFrom(Array<Byte>)

public override func receiveFrom(buffer: Array<Byte>): (SocketAddress, Int64)

Description: Receives packets.

Parameters:

  • buffer: Array<Byte>: address for storing the received packets

Returns:

  • (SocketAddress, Int64): address from which the received packets are sent, and size of the received packets, which may be 0 or greater than the size of buffer

Throws:

func send(Array<Byte>)

public func send(payload: Array<Byte>): Unit

Description: Sends packets to the address connected to using connect.

Parameters:

  • payload: Array<Byte>: content of the sent packets

Throws:

  • SocketException: If the size of payload exceeds the system limit or the system fails to send the packets, this exception is thrown.

func sendTo(SocketAddress, Array<Byte>)

public override func sendTo(recipient: SocketAddress, payload: Array<Byte>): Unit

Description: Sends packets. Insufficient buffer addresses may cause blocking.

Parameters:

  • recipient: SocketAddress: remote address to which the packets are sent
  • payload: Array<Byte>: content of the sent packets

Throws:

  • SocketException: If the size of payload exceeds the system limit, the system fails to send the packets (for example, when connect is called and abnormal ICMP packets are received, the sending fails), or connect is called before sendTo is called on macOS, this exception is thrown.

func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)

public func setSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: UIntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: CPointer<Unit>: parameter value
  • valueLength: UIntNative: parameter value length

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionBool(Int32, Int32, Bool)

public func setSocketOptionBool(
    level: Int32,
    option: Int32,
    value: Bool
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: Bool: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionIntNative(Int32, Int32, IntNative)

public func setSocketOptionIntNative(
    level: Int32,
    option: Int32,
    value: IntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: IntNative: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func getSocketOptionBool(Int32, Int32)

public func getSocketOptionBool(
    level: Int32,
    option: Int32
): Bool

Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • Bool: value of the specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Throws:

  • SocketException: If getsockopt fails to be returned, this exception is thrown.

func toString()

public override func toString(): String

Description: Returns status information about the current UDS.

Returns:

  • String: string containing status information about the current UDS

class UnixServerSocket

public class UnixServerSocket <: ServerSocket {
    public init(bindAt!: String)
    public init(bindAt!: SocketAddress)
}

Description: Provides a host communication server based on the duplex stream.

UnixServerSocket listens to connections. After this class is created, its property values can be configured through properties and setSocketOptionXX functions. The bind() function needs to be called for binding a local address to start listening for connections. A connection can be accepted through the accept() function.

NOTE

  • This class is not supported on Windows.

Parent types:

prop backlogSize

public mut prop backlogSize: Int64

Description: Sets and reads the size of backlog. If the property is called after bind is called, an exception is thrown. Whether a variable takes effect depends on system behaviors.

Type: Int64

Throws:

  • SocketException: If the property is called after bind is called, this exception is thrown.

prop localAddress

public override prop localAddress: SocketAddress

Description: Reads the local address of Socket that is to be or has been bound.

Type: SocketAddress

Throws:

prop receiveBufferSize

public mut prop receiveBufferSize: Int64

Description: Sets and reads the SO_RCVBUF property.

Type: Int64

Throws:

prop sendBufferSize

public mut prop sendBufferSize: Int64

Description: Sets and reads the SO_SNDBUF property.

Type: Int64

Throws:

init(SocketAddress)

public init(bindAt!: SocketAddress)

Description: Creates a UnixServerSocket instance that is not connected.

Parameters:

init(String)

public init(bindAt!: String)

Description: Creates a UnixServerSocket instance that is not connected.

isSock can be used to check whether the file type exists, and the unlink() function can be used to delete the file type.

Parameters:

  • bindAt!: String: file address to be connected to

Throws:

func accept()

public override func accept(): UnixSocket

Description: Waits to accept a connection from a client or reads a connection from the queue.

Returns:

  • UnixSocket: socket of the client to be connected to

func accept(?Duration)

public override func accept(timeout!: ?Duration): UnixSocket

Description: Waits to accept a connection from a client or reads a connection from the queue.

Parameters:

Returns:

  • UnixSocket: socket of the client to be connected to

Throws:

func bind()

public override func bind(): Unit

Description: Binds a Unix domain socket and creates a listening queue.

This function automatically creates a socket file in the local address. If the file already exists, the binding fails. The isSock function can be used to check whether the file type exists, and the unlink() function can be used to delete the file type. Upon failure, the socket needs to be closed using close. Retrying for many times is not supported.

Throws:

  • SocketException: If the binding fails due to a system error, this exception is thrown.

func close()

public override func close(): Unit

Description: Closes a socket. All operations on the socket except close/isClosed are not allowed to be called again. This function can be called more than once.

func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)

public func getSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: CPointer<UIntNative>
): Unit

Description: Obtains a specified socket parameter.

Parameters:

Throws:

  • SocketException: If getsockopt fails to be returned, this exception is thrown.

func getSocketOptionBool(Int32, Int32)

public func getSocketOptionBool(
    level: Int32,
    option: Int32
): Bool

Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • Bool: specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func getSocketOptionIntNative(Int32, Int32)

public func getSocketOptionIntNative(
    level: Int32,
    option: Int32
): IntNative

Description: Obtains a socket parameter whose return value is an integer.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • IntNative: value of the specified socket parameter

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func isClosed()

public override func isClosed(): Bool

Description: Checks whether a socket has been explicitly closed by calling close.

Returns:

  • Bool: If the socket has been explicitly closed by calling close, true is returned. Otherwise, false is returned.

func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)

public func setSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: UIntNative
): Unit

Description: Sets a socket parameter whose return value is an integer.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: CPointer<Unit>: parameter value
  • valueLength: UIntNative: parameter value length

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionBool(Int32, Int32, Bool)

public func setSocketOptionBool(
    level: Int32,
    option: Int32,
    value: Bool
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: Bool: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionIntNative(Int32, Int32, IntNative)

public func setSocketOptionIntNative(
    level: Int32,
    option: Int32,
    value: IntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: IntNative: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func toString()

public override func toString(): String

Description: Returns status information about the current UnixServerSocket.

Returns:

class UnixSocket

public class UnixSocket <: StreamingSocket {
    public init(address: SocketAddress, localAddress!: ?SocketAddress = None)
    public init(path: String, localPath!: ?String = None)
}

Description: Provides a host communication client based on the duplex stream.

After the UnixSocket instance is created, the connect() function needs to be called to create a connection, and close() needs to be explicitly called to reclaim resources when the connection ends. For more information, see StreamingSocket.

NOTE

  • This class is not supported on Windows.

Parent types:

prop localAddress

public override prop localAddress: SocketAddress

Description: Reads the local address of Socket that is to be or has been bound.

Type: SocketAddress

Throws:

  • SocketException: If Socket has been closed or no local address is available (no local address is configured and the socket is not connected), this exception is thrown.

prop readTimeout

public override mut prop readTimeout: ?Duration

Description: Sets and reads the read operation timeout duration.

If the value is too small, the minimum clock cycle value is used. If the value is too large, None is used. The default value is None.

Type: ?Duration

Throws:

prop receiveBufferSize

public mut prop receiveBufferSize: Int64

Description: Sets and reads the SO_RCVBUF property.

Type: Int64

Throws:

prop remoteAddress

public override prop remoteAddress: SocketAddress

Description: Reads the remote address to which Socket has been or is to be connected.

Type: SocketAddress

Throws:

prop sendBufferSize

public mut prop sendBufferSize: Int64

Description: Sets and reads the SO_SNDBUF property.

Type: Int64

Throws:

prop writeTimeout

public override mut prop writeTimeout: ?Duration

Description: Sets and reads the write operation timeout duration.

If the value is too small, the minimum clock cycle value is used. If the value is too large, the maximum timeout duration (263 – 1 nanoseconds) is used. The default value is None.

Type: ?Duration

Throws:

init(SocketAddress, ?SocketAddress)

public init(address: SocketAddress, localAddress!: ?SocketAddress = None)

Description: Creates a UnixSocket instance that is not connected.

Parameters:

  • address: SocketAddress: socket address to be connected to
  • localAddress!: ?SocketAddress: local socket address that needs to be bound. The default value is None.

init(String, ?String)

public init(path: String, localPath!: ?String = None)

Description: Creates a UnixSocket instance that is not connected.

isSock can be used to check whether the file type exists, and the unlink() function can be used to delete the file type.

Parameters:

  • path: String: file address to be connected to
  • localPath!: ?String: local socket address path that needs to be bound. The default value is None.

Throws:

func close()

public func close(): Unit

Description: Closes a socket. All operations except close/isClosed are not allowed to be called again. This function can be called more than once.

func connect(?Duration)

public func connect(timeout!: ?Duration = None): Unit

Description: Establishes a connection with the peer end. If the peer end rejects the connection, the connection fails and a local address is automatically bound. Therefore, no additional binding operation is required.

Parameters:

  • timeout!: ?Duration: timeout duration. None indicates no timeout. Different from TCP, when Unix is used, if the queue is full, an error is returned immediately when the function is called, without retrying waiting.

Throws:

  • IllegalArgumentException: If the remote address is invalid or the timeout duration is less than 0, this exception is thrown.
  • SocketException: If the connection cannot be established due to a system error, this exception is thrown.
  • SocketTimeoutException: If the connection times out, this exception is thrown.

func getSocketOption(Int32, Int32, CPointer<Unit>, CPointer<UIntNative>)

public func getSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: CPointer<UIntNative>
): Unit

Description: Obtains a specified socket parameter.

Parameters:

Throws:

  • SocketException: If getsockopt fails to be returned, this exception is thrown.

func getSocketOptionBool(Int32, Int32)

public func getSocketOptionBool(
    level: Int32,
    option: Int32
): Bool

Description: Obtains a specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • Bool: value of the specified socket parameter. It is forcibly converted from IntNative. 0 is converted into false, and a non-zero value is converted into true.

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func getSocketOptionIntNative(Int32, Int32)

public func getSocketOptionIntNative(
    level: Int32,
    option: Int32
): IntNative

Description: Obtains a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE

Returns:

  • IntNative: value of the specified socket parameter

Throws:

  • SocketException: If getsockopt fails to be returned or the parameter value exceeds the threshold of IntNative, this exception is thrown.

func isClosed()

public func isClosed(): Bool

Description: Checks whether a socket has been explicitly closed by calling close.

Returns:

  • Bool: whether the socket has been explicitly closed by calling close. If so, true is returned. Otherwise, false is returned.

func read(Array<Byte>)

public override func read(buffer: Array<Byte>): Int64

Description: Reads packets. Timeout is determined by readTimeout. For details, see readTimeout.

Parameters:

  • buffer: Array<Byte>: variable for storing the read data

Returns:

  • Int64: length of the read data

Throws:

  • SocketException: If the size of buffer is 0 or the read operation fails due to a system error, this exception is thrown.

func setSocketOption(Int32, Int32, CPointer<Unit>, UIntNative)

public func setSocketOption(
    level: Int32,
    option: Int32,
    value: CPointer<Unit>,
    valueLength: UIntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: CPointer<Unit>: parameter value
  • valueLength: UIntNative: parameter value length

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionBool(Int32, Int32, Bool)

public func setSocketOptionBool(
    level: Int32,
    option: Int32,
    value: Bool
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: Bool: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func setSocketOptionIntNative(Int32, Int32, IntNative)

public func setSocketOptionIntNative(
    level: Int32,
    option: Int32,
    value: IntNative
): Unit

Description: Sets a specified socket parameter.

Parameters:

  • level: Int32: level, for example, SOL_SOCKET
  • option: Int32: parameter, for example, SO_KEEPALIVE
  • value: IntNative: parameter value

Throws:

  • SocketException: If setsockopt fails to be returned, this exception is thrown.

func toString()

public override func toString(): String

Description: Returns status information about the current UnixSocket.

Returns:

func write(Array<Byte>)

public override func write(buffer: Array<Byte>): Unit

Description: Reads and writes data. Timeout is determined by writeTimeout. For details, see writeTimeout.

Parameters:

  • buffer: Array<Byte>: variable for storing the written data

Throws:

  • SocketException: If the size of buffer is 0 or the write operation fails due to a system error, this exception is thrown.

class UnixSocketAddress

public class UnixSocketAddress <: SocketAddress & Equatable<UnixSocketAddress> {
    public init(path: Array<Byte>)
    public init(path: String)
}

Description: Implements a Unix domain socket address, which encapsulates the file system path bound or connected to the Unix domain socket. The path length cannot exceed 108.

If the path is an empty string, it is an unnamed address. If the path starts with \0, it is an abstract address. The path cannot contain \0.

Parent types:

prop family

public prop family: AddressFamily

Description: Obtains the address family of the current UnixSocketAddress object, which is always AddressFamily.UNIX.

Type: AddressFamily

prop size

public prop size: Int64

Description: Obtains the original byte length of the current UnixSocketAddress object.

Type: Int64

init(Array<Byte>)

public init(path: Array<Byte>)

Description: Constructs a UnixSocketAddress address based on the file system path represented by Array<Byte>.

Parameters:

  • path: Array<Byte>: byte array of the file system path

Throws:

init(String)

public init(path: String)

Description: Constructs a UnixSocketAddress address based on the file system path represented by a string.

Parameters:

  • path: String: file system path string

Throws:

func getAddressBytes()

public func getAddressBytes(): Array<Byte>

Description: Returns the original IP address of a UnixSocketAddress object. The content layout is the same as that of sockaddr_un.

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let udsa1_1: UnixSocketAddress = UnixSocketAddress("/tmp/server1.sock")
    @Assert(udsa1_1.getAddressBytes(), "\u{1}\u{0}/tmp/server1.sock".toArray())
}

func hashCode()

public func hashCode(): Int64

Description: Obtains the hashcode value.

Returns:

func toString()

public func toString(): String

Description: Returns the text representation string of the current UnixSocketAddress.

Returns:

Examples:

import std.net.*
import std.unittest.*
import std.unittest.testmacro.*

main () {
    let expect1 = "/tmp/server1.sock"
    let expect2 = "\u{0}/tmp/server1.sock"
    let udsa1_1: UnixSocketAddress = UnixSocketAddress("/tmp/server1.sock")
    let udsa2_1: UnixSocketAddress = UnixSocketAddress("/tmp/server1.sock".toArray())
    let udsa2_2: UnixSocketAddress = UnixSocketAddress("/tmp/server1.sock\u{0}\u{0}".toArray())
    let udsa3_1: UnixSocketAddress = UnixSocketAddress("\u{0}/tmp/server1.sock")
    let udsa4_1: UnixSocketAddress = UnixSocketAddress("\u{0}/tmp/server1.sock".toArray())
    let udsa4_2: UnixSocketAddress = UnixSocketAddress("\u{0}/tmp/server1.sock\u{0}\u{0}".toArray())
    @Assert(udsa1_1.toString(), expect1)
    @Assert(udsa2_1.toString(), expect1)
    @Assert(udsa2_2.toString(), expect1)
    @Assert(udsa3_1.toString(), expect2)
    @Assert(udsa1_1, udsa2_1)
    @Assert(udsa1_1, udsa2_2)
    @Assert(udsa3_1, udsa4_1)
    @Assert(udsa3_1, udsa4_2)
    @Assert(udsa4_1.toString(), expect2)
    @Assert(udsa4_2.toString(), expect2)

    try {
        UnixSocketAddress("/tmp/server1\u{0}.sock")
    } catch (e: IllegalArgumentException) {
        @Assert(true)
    }

    try {
        UnixSocketAddress("/tmp/server1.sock\u{0}\u{0}")
    } catch (e: IllegalArgumentException) {
        @Assert(true)
    }
    try {
        UnixSocketAddress("\u{0}/tmp/server1.sock\u{0}\u{0}")
    } catch (e: IllegalArgumentException) {
        @Assert(true)
    }
    try {
        UnixSocketAddress("/tmp/server1\u{0}.sock".toArray())
    } catch (e: IllegalArgumentException) {
        @Assert(true)
    }
    return
}

operator func ==(UnixSocketAddress)

public operator func ==(rhs: UnixSocketAddress): Bool

Description: Checks whether two UnixSocketAddress objects are equal.

Parameters:

Returns:

operator func !=(UnixSocketAddress)

public operator func !=(rhs: UnixSocketAddress): Bool

Description: Checks whether two UnixSocketAddress objects are not equal.

Parameters:

Returns:

  • Bool: If the two UnixSocketAddress objects are not equal, true is returned. Otherwise, false is returned.