improved tcpserver.lyng sample

This commit is contained in:
Sergey Chernov 2026-04-09 17:11:30 +03:00
parent 840cd32574
commit 9b90fe370b

View File

@ -5,28 +5,22 @@ val clientCount = 1000
val server = Net.tcpListen(0, host, clientCount, true) as TcpServer val server = Net.tcpListen(0, host, clientCount, true) as TcpServer
val port = server.localAddress().port val port = server.localAddress().port
fun payloadFor(index: Int): String { fun payloadFor(index: Int) = "$index:${Random.nextInt()}:${Random.nextInt()}"
"$index:${Random.nextInt()}:${Random.nextInt()}"
}
fun handleClient(client: TcpSocket) {
try {
val source = client.readLine()
if( source != null ) {
client.writeUtf8("pong: $source\n")
client.flush()
}
} finally {
client.close()
}
}
launch { launch {
try { try {
for( i in 0..<clientCount ) { while(true) {
val client = server.accept() as TcpSocket val client = server.accept() as TcpSocket
launch { launch {
handleClient(client) try {
val source = client.readLine()
if( source != null ) {
client.writeUtf8("pong: $source\n")
client.flush()
}
} finally {
client.close()
}
} }
} }
} finally { } finally {
@ -51,7 +45,4 @@ val replies = (0..<clientCount).map { index ->
}.joinAll() }.joinAll()
assertEquals(clientCount, replies.size) assertEquals(clientCount, replies.size)
println("OK: $clientCount concurrent tcp clients")
val summary = "OK: $clientCount concurrent tcp clients"
println(summary)
summary