or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.mdio-operations.mdselector-management.mdsocket-addresses.mdsocket-configuration.mdsocket-creation.mdtcp-operations.mdudp-operations.md

socket-creation.mddocs/

0

# Socket Creation

1

2

Core socket creation functionality using builder pattern with configurable options. Supports both TCP and UDP sockets with comprehensive lifecycle management.

3

4

## Capabilities

5

6

### Socket Builder Factory

7

8

Creates a socket builder instance that provides access to TCP and UDP socket builders.

9

10

```kotlin { .api }

11

/**

12

* Start building a socket

13

* @param selector SelectorManager for managing socket operations

14

* @returns SocketBuilder instance for creating TCP or UDP sockets

15

*/

16

fun aSocket(selector: SelectorManager): SocketBuilder

17

```

18

19

**Usage Example:**

20

21

```kotlin

22

import io.ktor.network.sockets.*

23

import io.ktor.network.selector.*

24

25

val selectorManager = SelectorManager()

26

val socketBuilder = aSocket(selectorManager)

27

28

// Create TCP socket builder

29

val tcpBuilder = socketBuilder.tcp()

30

31

// Create UDP socket builder

32

val udpBuilder = socketBuilder.udp()

33

```

34

35

### Socket Builder

36

37

Main socket builder providing access to protocol-specific builders.

38

39

```kotlin { .api }

40

/**

41

* Socket builder for creating TCP and UDP sockets

42

*/

43

class SocketBuilder : Configurable<SocketBuilder, SocketOptions> {

44

/** Build TCP socket builder */

45

fun tcp(): TcpSocketBuilder

46

47

/** Build UDP socket builder */

48

fun udp(): UDPSocketBuilder

49

50

/** Current socket options */

51

var options: SocketOptions

52

}

53

```

54

55

### Configurable Interface

56

57

Interface for socket configuration management used by all socket builders.

58

59

```kotlin { .api }

60

/**

61

* Represents a configurable socket with options

62

*/

63

interface Configurable<out T : Configurable<T, Options>, Options : SocketOptions> {

64

/** Current socket options */

65

var options: Options

66

67

/** Configure socket options in block function */

68

fun configure(block: Options.() -> Unit): T

69

}

70

```

71

72

**Usage Example:**

73

74

```kotlin

75

import io.ktor.network.sockets.*

76

import io.ktor.network.selector.*

77

78

val selectorManager = SelectorManager()

79

80

// Configure socket with options

81

val socket = aSocket(selectorManager)

82

.configure {

83

reuseAddress = true

84

reusePort = true

85

typeOfService = TypeOfService.IPTOS_LOWDELAY

86

}

87

.tcp()

88

.connect("localhost", 8080)

89

```

90

91

### Base Socket Interface

92

93

Base interface for all socket types providing lifecycle management.

94

95

```kotlin { .api }

96

/**

97

* Base type for all async sockets

98

*/

99

interface ASocket : Closeable, DisposableHandle {

100

/** Represents a socket lifetime, completes at socket closure */

101

val socketContext: Job

102

}

103

104

/** Check if the socket is closed */

105

val ASocket.isClosed: Boolean

106

107

/** Await until socket close */

108

suspend fun ASocket.awaitClosed()

109

```

110

111

### Socket Address Interfaces

112

113

Interfaces for socket address information.

114

115

```kotlin { .api }

116

/**

117

* Represents a bound socket

118

*/

119

interface ABoundSocket {

120

/** Local socket address. Could throw an exception if no address bound yet. */

121

val localAddress: SocketAddress

122

}

123

124

/**

125

* Represents a connected socket

126

*/

127

interface AConnectedSocket {

128

/** Remote socket address. Could throw an exception if the peer is not yet connected or already disconnected. */

129

val remoteAddress: SocketAddress

130

}

131

```

132

133

**Usage Example:**

134

135

```kotlin

136

import io.ktor.network.sockets.*

137

import io.ktor.network.selector.*

138

139

val selectorManager = SelectorManager()

140

141

// Create and connect a socket

142

val socket = aSocket(selectorManager).tcp().connect("localhost", 8080)

143

144

// Check socket status

145

println("Socket closed: ${socket.isClosed}")

146

println("Local address: ${socket.localAddress}")

147

println("Remote address: ${socket.remoteAddress}")

148

149

// Wait for socket to close

150

socket.awaitClosed()

151

152

// Clean up

153

selectorManager.close()

154

```

155

156

## Types

157

158

```kotlin { .api }

159

class SocketTimeoutException(message: String) : IOException

160

```