Ktor network utilities - tvOS x64 target implementation of asynchronous TCP/UDP sockets and related networking functionality for the Ktor framework
—
UDP datagram socket operations supporting both bound and connected datagram sockets with packet-based communication.
Builder for creating UDP sockets supporting both bound and connected datagram sockets.
/**
* UDP socket builder
*/
class UDPSocketBuilder : Configurable<UDPSocketBuilder, SocketOptions.UDPSocketOptions> {
/**
* Bind server socket to listen to local address
* @param localAddress Local socket address to bind to (null for any address)
* @param configure Configuration block for UDP options
* @returns BoundDatagramSocket instance for sending/receiving datagrams
*/
suspend fun bind(
localAddress: SocketAddress? = null,
configure: SocketOptions.UDPSocketOptions.() -> Unit = {}
): BoundDatagramSocket
/**
* Bind server socket at port to listen to hostname
* @param hostname Local hostname to bind to (default: "0.0.0.0")
* @param port Local port to bind to (default: 0 for auto-assignment)
* @param configure Configuration block for UDP options
* @returns BoundDatagramSocket instance for sending/receiving datagrams
*/
suspend fun bind(
hostname: String = "0.0.0.0",
port: Int = 0,
configure: SocketOptions.UDPSocketOptions.() -> Unit = {}
): BoundDatagramSocket
/**
* Create a datagram socket to listen datagrams at local address and set to remote address
* @param remoteAddress Remote socket address to connect to
* @param localAddress Local socket address to bind to (null for any address)
* @param configure Configuration block for UDP options
* @returns ConnectedDatagramSocket instance for sending/receiving datagrams to/from specific remote address
*/
suspend fun connect(
remoteAddress: SocketAddress,
localAddress: SocketAddress? = null,
configure: SocketOptions.UDPSocketOptions.() -> Unit = {}
): ConnectedDatagramSocket
}Usage Examples:
import io.ktor.network.sockets.*
import io.ktor.network.selector.*
import kotlinx.io.*
val selectorManager = SelectorManager()
// UDP Server - Bind to receive datagrams from any source
val udpServer = aSocket(selectorManager).udp().bind("0.0.0.0", 8080) {
broadcast = true
reuseAddress = true
}
// UDP Client - Connect to specific remote address
val udpClient = aSocket(selectorManager).udp().connect(
remoteAddress = InetSocketAddress("localhost", 8080)
) {
broadcast = false
}
// Send datagram from bound socket
val buffer = Buffer().apply { writeString("Hello UDP Server!") }
val datagram = Datagram(buffer, InetSocketAddress("localhost", 8080))
udpServer.send(datagram)
// Receive datagram
val receivedDatagram = udpServer.receive()
println("Received from ${receivedDatagram.address}: ${receivedDatagram.packet.readText()}")Data structure representing a UDP datagram with packet content and target address.
/**
* UDP datagram with packet content targeted to address
* @property packet content as Source
* @property address to send to
*/
class Datagram(
val packet: Source,
val address: SocketAddress
)Usage Example:
import io.ktor.network.sockets.*
import kotlinx.io.*
// Create datagram with string content
val content = Buffer().apply { writeString("Hello, World!") }
val targetAddress = InetSocketAddress("192.168.1.100", 8080)
val datagram = Datagram(content, targetAddress)
// Read packet content
val receivedText = datagram.packet.readText()
println("Content: $receivedText")
println("Target: ${datagram.address}")Interface for bound UDP sockets that can send and receive datagrams to/from any address.
/**
* Represents a bound datagram socket
*/
interface BoundDatagramSocket : ASocket, ABoundSocket, DatagramReadWriteChannelInterface for connected UDP sockets that send and receive datagrams to/from a specific remote address.
/**
* Represents a connected datagram socket
*/
interface ConnectedDatagramSocket : ASocket, ABoundSocket, AConnectedSocket, DatagramReadWriteChannelInterfaces for sending and receiving datagrams using channel-based operations.
/**
* A channel for receiving datagrams
*/
interface DatagramReadChannel {
/** Incoming datagrams channel */
val incoming: ReceiveChannel<Datagram>
/** Receive a datagram */
suspend fun receive(): Datagram
}
/**
* A channel for sending datagrams
*/
interface DatagramWriteChannel {
/** Datagram outgoing channel */
val outgoing: SendChannel<Datagram>
/** Send datagram */
suspend fun send(datagram: Datagram)
}
/**
* A channel for sending and receiving datagrams
*/
interface DatagramReadWriteChannel : DatagramReadChannel, DatagramWriteChannelUsage Examples:
import io.ktor.network.sockets.*
import io.ktor.network.selector.*
import kotlinx.coroutines.*
import kotlinx.io.*
val selectorManager = SelectorManager()
// UDP Echo Server Example
suspend fun udpEchoServer() {
val server = aSocket(selectorManager).udp().bind("0.0.0.0", 8080)
println("UDP server listening on port 8080")
while (true) {
val receivedDatagram = server.receive()
println("Received from ${receivedDatagram.address}")
// Echo back the message
val echoContent = Buffer().apply {
writeString("Echo: ${receivedDatagram.packet.readText()}")
}
val echoDatagram = Datagram(echoContent, receivedDatagram.address)
server.send(echoDatagram)
}
}
// UDP Client Example
suspend fun udpClient() {
val client = aSocket(selectorManager).udp().connect(
InetSocketAddress("localhost", 8080)
)
// Send message
val message = Buffer().apply { writeString("Hello Server!") }
val datagram = Datagram(message, client.remoteAddress)
client.send(datagram)
// Receive response
val response = client.receive()
println("Server response: ${response.packet.readText()}")
client.close()
}
// Using channel-based operations
suspend fun channelBasedUdp() {
val socket = aSocket(selectorManager).udp().bind()
// Send using outgoing channel
launch {
val message = Buffer().apply { writeString("Channel message") }
val datagram = Datagram(message, InetSocketAddress("localhost", 8080))
socket.outgoing.send(datagram)
}
// Receive using incoming channel
launch {
for (datagram in socket.incoming) {
println("Received: ${datagram.packet.readText()}")
}
}
}/** Maximum UDP datagram size */
internal const val MAX_DATAGRAM_SIZE = 65535Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-network-tvosx64