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 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) { launch {
try {
while(true) {
val client = server.accept() as TcpSocket
launch {
try { try {
val source = client.readLine() val source = client.readLine()
if( source != null ) { if( source != null ) {
@ -19,14 +21,6 @@ fun handleClient(client: TcpSocket) {
} finally { } finally {
client.close() client.close()
} }
}
launch {
try {
for( i in 0..<clientCount ) {
val client = server.accept() as TcpSocket
launch {
handleClient(client)
} }
} }
} 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