Ktor network utilities - tvOS x64 target implementation of asynchronous TCP/UDP sockets and related networking functionality for the Ktor framework
npx @tessl/cli install tessl/maven-io-ktor--ktor-network-tvosx64@3.2.00
# Ktor Network - tvOS x64
1
2
Ktor Network provides asynchronous TCP and UDP socket operations for the tvOS x64 platform using Kotlin coroutines. This package enables developers to create network applications with socket-based communication, supporting both client and server scenarios. It includes features for connection management, socket lifecycle handling, and integration with Kotlin's coroutine-based concurrency model.
3
4
## Package Information
5
6
- **Package Name**: io.ktor:ktor-network-tvosx64
7
- **Package Type**: maven
8
- **Language**: Kotlin
9
- **Platform**: tvOS x64 (Apple TV 64-bit)
10
- **Installation**: Add to build.gradle.kts: `implementation("io.ktor:ktor-network-tvosx64:3.2.0")`
11
12
## Core Imports
13
14
```kotlin
15
import io.ktor.network.sockets.*
16
import io.ktor.network.selector.*
17
import kotlinx.io.*
18
```
19
20
## Basic Usage
21
22
```kotlin
23
import io.ktor.network.sockets.*
24
import io.ktor.network.selector.*
25
import kotlinx.coroutines.*
26
import kotlinx.io.*
27
28
// Create a selector manager for managing socket operations
29
val selectorManager = SelectorManager()
30
31
// TCP Client Example
32
val socket = aSocket(selectorManager).tcp().connect("localhost", 8080)
33
val connection = socket.connection()
34
35
// Send data
36
connection.output.writeStringUtf8("Hello, Server!")
37
connection.output.flush()
38
39
// Receive data
40
val response = connection.input.readUTF8Line()
41
println("Server response: $response")
42
43
// Close connection
44
socket.close()
45
46
// TCP Server Example
47
val serverSocket = aSocket(selectorManager).tcp().bind("0.0.0.0", 8080)
48
while (true) {
49
val clientSocket = serverSocket.accept()
50
launch {
51
val connection = clientSocket.connection()
52
val request = connection.input.readUTF8Line()
53
connection.output.writeStringUtf8("Echo: $request")
54
connection.output.flush()
55
clientSocket.close()
56
}
57
}
58
59
// UDP Example
60
val udpSocket = aSocket(selectorManager).udp().bind()
61
val packet = Buffer().apply { writeString("Hello UDP") }
62
val datagram = Datagram(packet, InetSocketAddress("localhost", 9090))
63
udpSocket.send(datagram)
64
val receivedDatagram = udpSocket.receive()
65
66
// Clean up
67
selectorManager.close()
68
```
69
70
## Architecture
71
72
Ktor Network is built around several key components:
73
74
- **Socket Interfaces**: Core abstractions (`ASocket`, `Socket`, `ServerSocket`) that define socket lifecycle and operations
75
- **Socket Builders**: Fluent API (`SocketBuilder`, `TcpSocketBuilder`, `UDPSocketBuilder`) for creating and configuring sockets
76
- **Selector Management**: Asynchronous I/O management (`SelectorManager`) for handling multiple socket operations concurrently
77
- **Address Types**: Socket address abstractions (`SocketAddress`, `InetSocketAddress`, `UnixSocketAddress`) for different addressing schemes
78
- **Configuration System**: Comprehensive socket options (`SocketOptions`) for tuning socket behavior
79
- **Channel Integration**: ByteChannel-based I/O for reading and writing data to sockets
80
81
## Capabilities
82
83
### Socket Creation and Management
84
85
Core socket creation functionality using builder pattern with configurable options. Supports both TCP and UDP sockets with comprehensive lifecycle management.
86
87
```kotlin { .api }
88
fun aSocket(selector: SelectorManager): SocketBuilder
89
90
class SocketBuilder {
91
fun tcp(): TcpSocketBuilder
92
fun udp(): UDPSocketBuilder
93
}
94
```
95
96
[Socket Creation](./socket-creation.md)
97
98
### TCP Socket Operations
99
100
TCP client and server socket operations with support for connection establishment, data transmission, and server socket binding.
101
102
```kotlin { .api }
103
class TcpSocketBuilder {
104
suspend fun connect(hostname: String, port: Int): Socket
105
suspend fun connect(remoteAddress: SocketAddress): Socket
106
suspend fun bind(hostname: String = "0.0.0.0", port: Int = 0): ServerSocket
107
suspend fun bind(localAddress: SocketAddress? = null): ServerSocket
108
}
109
110
interface Socket : ReadWriteSocket, ABoundSocket, AConnectedSocket, CoroutineScope
111
interface ServerSocket : ASocket, ABoundSocket, Acceptable<Socket>
112
```
113
114
[TCP Operations](./tcp-operations.md)
115
116
### UDP Socket Operations
117
118
UDP datagram socket operations supporting both bound and connected datagram sockets with packet-based communication.
119
120
```kotlin { .api }
121
class UDPSocketBuilder {
122
suspend fun bind(localAddress: SocketAddress? = null): BoundDatagramSocket
123
suspend fun connect(remoteAddress: SocketAddress, localAddress: SocketAddress? = null): ConnectedDatagramSocket
124
}
125
126
interface BoundDatagramSocket : ASocket, ABoundSocket, DatagramReadWriteChannel
127
interface ConnectedDatagramSocket : ASocket, ABoundSocket, AConnectedSocket, DatagramReadWriteChannel
128
```
129
130
[UDP Operations](./udp-operations.md)
131
132
### Socket Address Management
133
134
Socket address types and utilities for different addressing schemes including Internet addresses and Unix domain sockets.
135
136
```kotlin { .api }
137
sealed class SocketAddress
138
139
class InetSocketAddress(hostname: String, port: Int) : SocketAddress {
140
val hostname: String
141
val port: Int
142
fun resolveAddress(): ByteArray?
143
}
144
145
class UnixSocketAddress(path: String) : SocketAddress {
146
val path: String
147
companion object {
148
fun isSupported(): Boolean
149
}
150
}
151
```
152
153
[Socket Addresses](./socket-addresses.md)
154
155
### Socket Configuration
156
157
Comprehensive socket configuration options for tuning socket behavior including timeouts, buffer sizes, and protocol-specific options.
158
159
```kotlin { .api }
160
sealed class SocketOptions {
161
var typeOfService: TypeOfService
162
var reuseAddress: Boolean
163
var reusePort: Boolean
164
165
class TCPClientSocketOptions : PeerSocketOptions {
166
var noDelay: Boolean
167
var lingerSeconds: Int
168
var keepAlive: Boolean?
169
var socketTimeout: Long
170
}
171
172
class UDPSocketOptions : PeerSocketOptions {
173
var broadcast: Boolean
174
}
175
176
class AcceptorOptions : SocketOptions {
177
var backlogSize: Int
178
}
179
}
180
```
181
182
[Socket Configuration](./socket-configuration.md)
183
184
### I/O Operations
185
186
Socket I/O operations using ByteChannel integration for reading and writing data with support for both stream-based and packet-based communication.
187
188
```kotlin { .api }
189
interface ReadWriteSocket : ASocket, AReadable, AWritable
190
191
interface AReadable {
192
fun attachForReading(channel: ByteChannel): WriterJob
193
}
194
195
interface AWritable {
196
fun attachForWriting(channel: ByteChannel): ReaderJob
197
}
198
199
class Connection(
200
val socket: Socket,
201
val input: ByteReadChannel,
202
val output: ByteWriteChannel
203
)
204
```
205
206
[I/O Operations](./io-operations.md)
207
208
### Selector Management
209
210
Asynchronous I/O management for handling multiple socket operations concurrently using coroutine-based selection.
211
212
```kotlin { .api }
213
interface SelectorManager : CoroutineScope, Closeable {
214
fun notifyClosed(selectable: Selectable)
215
suspend fun select(selectable: Selectable, interest: SelectInterest)
216
}
217
218
fun SelectorManager(dispatcher: CoroutineContext = EmptyCoroutineContext): SelectorManager
219
220
enum class SelectInterest {
221
READ, WRITE, ACCEPT, CONNECT
222
}
223
```
224
225
[Selector Management](./selector-management.md)