Ktor network utilities - tvOS x64 target implementation of asynchronous TCP/UDP sockets and related networking functionality for the Ktor framework
—
Socket address types and utilities for different addressing schemes including Internet addresses and Unix domain sockets.
Base sealed class for all socket address types providing common functionality.
/**
* Represents a socket address abstraction.
* This sealed class serves as the base type for different kinds of socket addresses,
* such as Internet-specific or other platform-dependent address types.
*/
sealed class SocketAddress
/**
* Retrieves the port number associated with this socket address.
* If the SocketAddress instance is of type InetSocketAddress, the associated port is returned.
* Otherwise, an UnsupportedOperationException is thrown.
* @returns the port number of the socket address if available
* @throws UnsupportedOperationException if the socket address type does not support a port
*/
fun SocketAddress.port(): IntUsage Example:
import io.ktor.network.sockets.*
val inetAddress = InetSocketAddress("localhost", 8080)
val unixAddress = UnixSocketAddress("/tmp/socket")
// Get port from inet address
val port = inetAddress.port() // Returns 8080
// This would throw UnsupportedOperationException
try {
val unixPort = unixAddress.port()
} catch (e: UnsupportedOperationException) {
println("Unix sockets don't have ports")
}Socket address for Internet Protocol (IP) based connections supporting both IPv4 and IPv6.
/**
* Internet socket address for IP-based connections
* @param hostname The hostname or IP address
* @param port The port number
*/
class InetSocketAddress(
hostname: String,
port: Int
) : SocketAddress {
/**
* The hostname of the socket address.
* Note that this may trigger a name service reverse lookup.
*/
val hostname: String
/**
* The port number of the socket address.
*/
val port: Int
/**
* Returns the raw IP address bytes of this socket address.
* The returned array is 4-bytes for IPv4 addresses and 16-bytes for IPv6 addresses.
* Returns null if the address cannot be resolved or is not a valid IP address.
* Always returns null for Kotlin/JS and Kotlin/Wasm targets.
*/
fun resolveAddress(): ByteArray?
/**
* The hostname of the socket address.
* Note that this may trigger a name service reverse lookup.
*/
operator fun component1(): String
/**
* The port number of the socket address.
*/
operator fun component2(): Int
/**
* Create a copy of InetSocketAddress.
* Note that this may trigger a name service reverse lookup.
*/
fun copy(hostname: String = this.hostname, port: Int = this.port): InetSocketAddress
}Usage Examples:
import io.ktor.network.sockets.*
// Create inet socket addresses
val localhostAddress = InetSocketAddress("localhost", 8080)
val ipAddress = InetSocketAddress("192.168.1.1", 80)
val ipv6Address = InetSocketAddress("::1", 8080)
// Access properties
println("Hostname: ${localhostAddress.hostname}")
println("Port: ${localhostAddress.port}")
// Destructuring declaration
val (hostname, port) = localhostAddress
println("Connecting to $hostname:$port")
// Copy with modifications
val sslAddress = localhostAddress.copy(port = 8443)
println("SSL address: $sslAddress")
// Resolve IP address
val ipBytes = localhostAddress.resolveAddress()
if (ipBytes != null) {
println("IP bytes: ${ipBytes.joinToString(".")}")
} else {
println("Could not resolve address")
}
// Use in socket operations
val selectorManager = SelectorManager()
val socket = aSocket(selectorManager).tcp().connect(localhostAddress)Socket address for Unix domain sockets supporting local inter-process communication.
/**
* Unix domain socket address for local IPC
* @param path The filesystem path for the socket
*/
class UnixSocketAddress(
path: String
) : SocketAddress {
/**
* The path of the socket address.
*/
val path: String
/**
* The path of the socket address.
*/
operator fun component1(): String
/**
* Create a copy of UnixSocketAddress.
*/
fun copy(path: String = this.path): UnixSocketAddress
companion object {
/**
* Checks if Unix domain sockets are supported on the current platform.
* @returns true if Unix domain sockets are supported, false otherwise
*/
fun isSupported(): Boolean
}
}Usage Examples:
import io.ktor.network.sockets.*
// Check platform support
if (UnixSocketAddress.isSupported()) {
// Create Unix socket addresses
val unixAddress = UnixSocketAddress("/tmp/my-socket")
val userSocket = UnixSocketAddress("/home/user/.app/socket")
// Access properties
println("Socket path: ${unixAddress.path}")
// Destructuring declaration
val (path) = unixAddress
println("Socket at: $path")
// Copy with modifications
val backupSocket = unixAddress.copy(path = "/tmp/my-socket.backup")
// Use in socket operations (if supported by platform)
val selectorManager = SelectorManager()
try {
val socket = aSocket(selectorManager).tcp().connect(unixAddress)
// Use socket for local IPC
} catch (e: Exception) {
println("Unix sockets not supported or connection failed: ${e.message}")
}
} else {
println("Unix domain sockets are not supported on this platform")
}Utility functions for working with socket addresses.
/**
* Extension function to get port from any SocketAddress
* @returns port number for InetSocketAddress
* @throws UnsupportedOperationException for address types that don't support ports
*/
fun SocketAddress.port(): IntComplete Example:
import io.ktor.network.sockets.*
import io.ktor.network.selector.*
suspend fun addressExamples() {
val selectorManager = SelectorManager()
// Different address types
val addresses = listOf(
InetSocketAddress("localhost", 8080),
InetSocketAddress("127.0.0.1", 8080),
InetSocketAddress("::1", 8080), // IPv6 localhost
InetSocketAddress("google.com", 80)
)
for (address in addresses) {
println("Address: $address")
println("Hostname: ${address.hostname}")
println("Port: ${address.port}")
// Try to resolve IP
val ipBytes = address.resolveAddress()
if (ipBytes != null) {
if (ipBytes.size == 4) {
println("IPv4: ${ipBytes.joinToString(".")}")
} else if (ipBytes.size == 16) {
println("IPv6: ${ipBytes.joinToString(":") { "%02x".format(it) }}")
}
}
// Use address for connection
try {
val socket = aSocket(selectorManager).tcp().connect(address)
println("Connected successfully to $address")
socket.close()
} catch (e: Exception) {
println("Connection failed: ${e.message}")
}
println("---")
}
// Unix socket example (if supported)
if (UnixSocketAddress.isSupported()) {
val unixAddr = UnixSocketAddress("/tmp/test-socket")
println("Unix socket: ${unixAddr.path}")
// Note: Unix sockets don't have ports
try {
unixAddr.port()
} catch (e: UnsupportedOperationException) {
println("Unix sockets don't support port(): ${e.message}")
}
}
selectorManager.close()
}Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-network-tvosx64