or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

address-types.mddatagram-sockets.mdindex.mdselectors.mdsocket-builders.mdsocket-interfaces.mdsocket-options.mdutilities.md
tile.json

tessl/maven-io-ktor--ktor-network-macosarm64

Ktor network utilities for macOS ARM64 target - provides asynchronous networking components including sockets, selectors, and connection utilities for building Kotlin multiplatform network applications

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/io.ktor/ktor-network-macosarm64@3.2.x

To install, run

npx @tessl/cli install tessl/maven-io-ktor--ktor-network-macosarm64@3.2.0

index.mddocs/

Ktor Network

Ktor 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.

Package Information

  • Package Name: io.ktor:ktor-network-macosarm64
  • Package Type: Maven/Gradle
  • Language: Kotlin (Multiplatform)
  • Platform Target: macOS ARM64
  • Installation: implementation("io.ktor:ktor-network-macosarm64:3.2.0")

Core Imports

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.ConnectedDatagramSocket

For socket configuration:

import io.ktor.network.sockets.SocketOptions
import io.ktor.network.sockets.TypeOfService

Basic Usage

TCP Client Socket

import 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()
}

TCP Server Socket

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()
        }
    }
}

UDP Socket

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()
}

Architecture

Ktor Network is built around several core concepts:

  • Socket Builders: Fluent API for creating sockets with aSocket(selector).tcp() or aSocket(selector).udp()
  • Socket Types: Different interfaces for different socket capabilities (readable, writable, bound, connected)
  • Selector Manager: Platform-specific I/O event management using coroutines
  • Address System: Unified addressing for Internet and Unix domain sockets
  • Options System: Comprehensive socket configuration for performance and behavior tuning

Capabilities

Socket Creation and Builders

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
}

Socket Builders

Core Socket Interfaces and Types

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
}

Socket Interfaces

UDP/Datagram Socket Functionality

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
}

Datagram Sockets

Socket Address Types and Utilities

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 only

Address Types

Socket Configuration and Options

Extensive 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
}

Socket Options

Selector and Event Management

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 }

Selectors

Utility Functions and Extensions

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
    }
}

Utilities

Exception Types

class SocketTimeoutException(message: String) : Exception