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