Ktor network utilities for macOS ARM64 target - provides asynchronous networking components including sockets, selectors, and connection utilities for building Kotlin multiplatform network applications
—
Socket builders provide a fluent API for creating and configuring sockets in ktor-network. The builder pattern allows for clean, readable socket creation with optional configuration.
fun aSocket(selector: SelectorManager): SocketBuilderCreates the main socket builder that provides access to TCP and UDP socket builders.
Parameters:
selector: SelectorManager - The selector manager for I/O event handlingReturns: SocketBuilder - Main builder for choosing socket type
class SocketBuilder {
fun tcp(): TcpSocketBuilder
fun udp(): UDPSocketBuilder
}The main builder class that branches into specific socket types.
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
}Builder for creating TCP sockets with client and server capabilities.
suspend fun connect(remoteAddress: SocketAddress): SocketCreates a TCP client socket and connects to the specified remote address.
Parameters:
remoteAddress: SocketAddress - The remote address to connect toReturns: Socket - A connected TCP socket ready for I/O operations
suspend fun bind(localAddress: SocketAddress? = null): ServerSocketCreates a TCP server socket bound to the specified local address.
Parameters:
localAddress: SocketAddress? - Local address to bind to, or null for any available addressReturns: ServerSocket - A server socket ready to accept connections
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
}Builder for creating UDP sockets with bound and connected variants.
suspend fun bind(localAddress: SocketAddress? = null): BoundDatagramSocketCreates a UDP socket bound to a local address for receiving datagrams from any source.
Parameters:
localAddress: SocketAddress? - Local address to bind to, or null for any available addressReturns: BoundDatagramSocket - A bound UDP socket for datagram communication
suspend fun connect(remoteAddress: SocketAddress): ConnectedDatagramSocketCreates a UDP socket connected to a specific remote address for efficient point-to-point communication.
Parameters:
remoteAddress: SocketAddress - The remote address to connect toReturns: ConnectedDatagramSocket - A connected UDP socket for efficient datagram exchange
interface Configurable<out T : Configurable<T, Options>, Options : SocketOptions> {
fun configure(block: Options.() -> Unit): T
}Interface enabling fluent configuration of socket builders with type-safe options.
import io.ktor.network.sockets.*
import io.ktor.network.selector.*
suspend fun createTcpClient() {
val selectorManager = SelectorManager()
val socket = aSocket(selectorManager)
.tcp()
.connect(InetSocketAddress("example.com", 80))
// Use socket...
socket.close()
}suspend fun createConfiguredTcpServer() {
val selectorManager = SelectorManager()
val serverSocket = aSocket(selectorManager)
.tcp()
.configure {
reuseAddress = true
reusePort = true
backlogSize = 100
}
.bind(InetSocketAddress("localhost", 8080))
// Accept connections...
serverSocket.close()
}// Bound UDP socket for receiving from any source
suspend fun createUdpReceiver() {
val selectorManager = SelectorManager()
val socket = aSocket(selectorManager)
.udp()
.bind(InetSocketAddress("localhost", 9999))
val datagram = socket.receive()
socket.close()
}
// Connected UDP socket for efficient point-to-point
suspend fun createUdpSender() {
val selectorManager = SelectorManager()
val socket = aSocket(selectorManager)
.udp()
.configure {
broadcast = false
}
.connect(InetSocketAddress("target.com", 8888))
// Send datagrams efficiently...
socket.close()
}suspend fun createOptimizedTcpConnection() {
val selectorManager = SelectorManager()
val socket = aSocket(selectorManager)
.tcp()
.configure {
noDelay = true // Disable Nagle's algorithm
keepAlive = true // Enable TCP keepalive
socketTimeout = 30000 // 30 second timeout
reuseAddress = true // Allow address reuse
sendBufferSize = 64 * 1024 // 64KB send buffer
receiveBufferSize = 64 * 1024 // 64KB receive buffer
}
.connect(InetSocketAddress("api.example.com", 443))
// High-performance connection ready
socket.close()
}import io.ktor.network.sockets.*
import io.ktor.network.selector.SelectorManager
import io.ktor.utils.io.*
import kotlinx.coroutines.*SelectorManager - See SelectorsSocketAddress, InetSocketAddress - See Address TypesSocketOptions.* - See Socket OptionsSocket, ServerSocket - See Socket InterfacesBoundDatagramSocket, ConnectedDatagramSocket - See Datagram SocketsInstall with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-network-macosarm64