comparison samples/echo.tp @ 371:625b0aa9c204

Add a simple echo server sample to demonstrate socket listening
author Michael Pavone <pavone@retrodev.com>
date Wed, 12 Aug 2015 19:13:52 -0700
parents
children
comparison
equal deleted inserted replaced
370:57d78a0af132 371:625b0aa9c204
1 #{
2 echo <- :sock {
3 print: "New connection\n"
4 data <- sock recv: 4096
5 while: { (data length) > 0 } do: {
6 sock send: data
7 data <- sock recv: 4096
8 }
9 print: "Connection closed\n"
10 }
11
12 main <- :args {
13 port <- "2323"
14 if: (args length) > 1 {
15 port <- args get: 1
16 }
17 (socket listenOnPort: port) value: :lsock {
18 print: "Listening on port " . port . "\n"
19 continue? <- true
20 while: { continue? } do: {
21 (lsock accept) value: :csock {
22 echo: csock
23 } none: {
24 print: "Failed to accept new connection\n"
25 continue? <- false
26 }
27 }
28 } none: {
29 print: "Failed to listen on port " . port . "\n"
30 }
31
32 }
33 }