improved tcpserver.lyng sample
This commit is contained in:
parent
840cd32574
commit
9b90fe370b
@ -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 ) {
|
||||
@ -20,14 +22,6 @@ fun handleClient(client: TcpSocket) {
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
try {
|
||||
for( i in 0..<clientCount ) {
|
||||
val client = server.accept() as TcpSocket
|
||||
launch {
|
||||
handleClient(client)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
server.close()
|
||||
@ -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")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user