Ktor network utilities for macOS ARM64 target - provides asynchronous networking components including sockets, selectors, and connection utilities for building Kotlin multiplatform network applications
npx @tessl/cli install tessl/maven-io-ktor--ktor-network-macosarm64@3.2.0Ktor Network provides comprehensive asynchronous networking capabilities for Kotlin Multiplatform applications targeting macOS ARM64. Built on Kotlin coroutines, it offers socket abstractions, connection management, and low-level I/O operations with platform-specific optimizations.
implementation("io.ktor:ktor-network-macosarm64:3.2.0")import io.ktor.network.sockets.*
import io.ktor.network.selector.*For UDP/Datagram operations:
import io.ktor.network.sockets.Datagram
import io.ktor.network.sockets.BoundDatagramSocket
import io.ktor.network.sockets.ConnectedDatagramSocketFor socket configuration:
import io.ktor.network.sockets.SocketOptions
import io.ktor.network.sockets.TypeOfServiceimport io.ktor.network.sockets.*
import io.ktor.network.selector.*
import kotlinx.coroutines.*
suspend fun connectTcp() {
val selectorManager = SelectorManager(Dispatchers.IO)
val socket = aSocket(selectorManager).tcp().connect(InetSocketAddress("example.com", 80))
// Use socket for communication
val input = socket.openReadChannel()
val output = socket.openWriteChannel(autoFlush = true)
// Clean up
socket.close()
selectorManager.close()
}suspend fun startTcpServer() {
val selectorManager = SelectorManager(Dispatchers.IO)
val serverSocket = aSocket(selectorManager).tcp().bind(InetSocketAddress("localhost", 8080))
while (true) {
val clientSocket = serverSocket.accept()
launch {
// Handle client connection
val input = clientSocket.openReadChannel()
val output = clientSocket.openWriteChannel(autoFlush = true)
// ... handle communication ...
clientSocket.close()
}
}
}suspend fun sendUdpMessage() {
val selectorManager = SelectorManager(Dispatchers.IO)
val socket = aSocket(selectorManager).udp().bind()
val message = "Hello UDP"
val datagram = Datagram(
packet = ByteReadPacket(message.toByteArray()),
address = InetSocketAddress("localhost", 9999)
)
socket.send(datagram)
socket.close()
selectorManager.close()
}Ktor Network is built around several core concepts:
aSocket(selector).tcp() or aSocket(selector).udp()Primary entry points and builder pattern for socket creation with TCP and UDP support.
fun aSocket(selector: SelectorManager): SocketBuilder
class SocketBuilder {
fun tcp(): TcpSocketBuilder
fun udp(): UDPSocketBuilder
}
class TcpSocketBuilder : Configurable<TcpSocketBuilder, SocketOptions.TCPClientSocketOptions> {
suspend fun connect(remoteAddress: SocketAddress): Socket
suspend fun bind(localAddress: SocketAddress? = null): ServerSocket
fun configure(block: SocketOptions.TCPClientSocketOptions.() -> Unit): TcpSocketBuilder
}
class UDPSocketBuilder : Configurable<UDPSocketBuilder, SocketOptions.UDPSocketOptions> {
suspend fun bind(localAddress: SocketAddress? = null): BoundDatagramSocket
suspend fun connect(remoteAddress: SocketAddress): ConnectedDatagramSocket
fun configure(block: SocketOptions.UDPSocketOptions.() -> Unit): UDPSocketBuilder
}Complete socket interface hierarchy with base types and capability interfaces for different socket behaviors.
interface ASocket : Closeable, DisposableHandle {
val socketContext: Job
val isClosed: Boolean
suspend fun awaitClosed()
}
interface ReadWriteSocket : ASocket, AReadable, AWritable
interface Socket : ReadWriteSocket, ABoundSocket, AConnectedSocket, CoroutineScope
interface ServerSocket : ASocket, ABoundSocket, Acceptable<Socket>
interface AReadable {
fun attachForReading(channel: ByteChannel): WriterJob
fun openReadChannel(): ByteReadChannel
}
interface AWritable {
fun attachForWriting(channel: ByteChannel): ReaderJob
fun openWriteChannel(autoFlush: Boolean = false): ByteWriteChannel
}Specialized UDP socket support with datagram packet handling and both bound and connected socket types.
class Datagram(val packet: Source, val address: SocketAddress)
interface BoundDatagramSocket : ASocket, ABoundSocket, DatagramReadWriteChannel
interface ConnectedDatagramSocket : ASocket, ABoundSocket, AConnectedSocket, DatagramReadWriteChannel
interface DatagramReadWriteChannel : DatagramReadChannel, DatagramWriteChannel {
suspend fun send(datagram: Datagram)
suspend fun receive(): Datagram
}Comprehensive addressing system supporting both Internet and Unix domain sockets.
sealed class SocketAddress
data class InetSocketAddress(val hostname: String, val port: Int) : SocketAddress
data class UnixSocketAddress(val path: String) : SocketAddress
fun SocketAddress.port(): Int // Extension for InetSocketAddress onlyExtensive configuration system for tuning socket behavior, performance, and network properties.
sealed class SocketOptions
class SocketOptions.TCPClientSocketOptions : SocketOptions.PeerSocketOptions() {
var keepAlive: Boolean?
var noDelay: Boolean?
var lingerSeconds: Int?
var socketTimeout: Long?
}
class SocketOptions.UDPSocketOptions : SocketOptions.PeerSocketOptions() {
var broadcast: Boolean?
}
class SocketOptions.AcceptorOptions : SocketOptions() {
var backlogSize: Int
var reuseAddress: Boolean
var reusePort: Boolean
}Platform-specific I/O event management system built on coroutines for efficient async operations.
interface SelectorManager : Closeable {
val coroutineContext: CoroutineContext
fun notifyClosed(selectable: Selectable)
suspend fun select(selectable: Selectable, interest: SelectInterest)
}
fun SelectorManager(dispatcher: CoroutineContext = Dispatchers.IO): SelectorManager
enum class SelectInterest { READ, WRITE, ACCEPT, CONNECT }Convenience functions, extensions, and helper utilities for common socket operations and type conversions.
fun Socket.connection(): Connection
val ServerSocket.port: Int
suspend fun ASocket.awaitClosed()
class Connection(val socket: Socket, val input: ByteReadChannel, val output: ByteWriteChannel) : Closeable
value class TypeOfService(val value: UByte) {
constructor(value: Int) : this(value.toUByte())
val intValue: Int get() = value.toInt()
companion object {
val UNDEFINED: TypeOfService
val IPTOS_LOWCOST: TypeOfService
val IPTOS_RELIABILITY: TypeOfService
val IPTOS_THROUGHPUT: TypeOfService
val IPTOS_LOWDELAY: TypeOfService
}
}class SocketTimeoutException(message: String) : Exception