1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Simple server example using a non-blocking `TCPServer`
16 module socket_simple_server
21 print
"Usage : socket_simple_server <port>"
25 var port
= args
[0].to_i
27 # Open the listening socket
28 var socket
= new TCPServer(port
)
30 socket
.blocking
= false
32 print
"Listening on port {socket.port}"
34 # Loop until a single client connects
35 var client
: nullable TCPStream = null
36 while client
== null do
38 sys
.nanosleep
(0, 200000)
40 client
= socket
.accept
45 print client
.read_line
46 client
.write
"Hello client!\n"
47 print client
.read_line
48 client
.write
"Bye client!\n"