CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Pending
Overview
Eval results
Files

socket-builders.mddocs/

Socket Builders

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.

Core Builder Functions

Primary Entry Point

fun aSocket(selector: SelectorManager): SocketBuilder

Creates the main socket builder that provides access to TCP and UDP socket builders.

Parameters:

  • selector: SelectorManager - The selector manager for I/O event handling

Returns: SocketBuilder - Main builder for choosing socket type

Main Socket Builder

class SocketBuilder {
    fun tcp(): TcpSocketBuilder
    fun udp(): UDPSocketBuilder
}

The main builder class that branches into specific socket types.

TCP Socket Builder

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.

TCP Client Connection

suspend fun connect(remoteAddress: SocketAddress): Socket

Creates a TCP client socket and connects to the specified remote address.

Parameters:

  • remoteAddress: SocketAddress - The remote address to connect to

Returns: Socket - A connected TCP socket ready for I/O operations

TCP Server Binding

suspend fun bind(localAddress: SocketAddress? = null): ServerSocket

Creates a TCP server socket bound to the specified local address.

Parameters:

  • localAddress: SocketAddress? - Local address to bind to, or null for any available address

Returns: ServerSocket - A server socket ready to accept connections

UDP Socket Builder

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.

UDP Bound Socket

suspend fun bind(localAddress: SocketAddress? = null): BoundDatagramSocket

Creates 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 address

Returns: BoundDatagramSocket - A bound UDP socket for datagram communication

UDP Connected Socket

suspend fun connect(remoteAddress: SocketAddress): ConnectedDatagramSocket

Creates a UDP socket connected to a specific remote address for efficient point-to-point communication.

Parameters:

  • remoteAddress: SocketAddress - The remote address to connect to

Returns: ConnectedDatagramSocket - A connected UDP socket for efficient datagram exchange

Configuration Interface

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.

Usage Examples

Basic TCP Client

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

Configured TCP Server

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

UDP Socket Examples

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

Advanced TCP Configuration

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

Type Definitions

Required Import Statements

import io.ktor.network.sockets.*
import io.ktor.network.selector.SelectorManager
import io.ktor.utils.io.*
import kotlinx.coroutines.*

Related Types

  • SelectorManager - See Selectors
  • SocketAddress, InetSocketAddress - See Address Types
  • SocketOptions.* - See Socket Options
  • Socket, ServerSocket - See Socket Interfaces
  • BoundDatagramSocket, ConnectedDatagramSocket - See Datagram Sockets

Install with Tessl CLI

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

docs

address-types.md

datagram-sockets.md

index.md

selectors.md

socket-builders.md

socket-interfaces.md

socket-options.md

utilities.md

tile.json