-
Notifications
You must be signed in to change notification settings - Fork 8
/
Server.nim
52 lines (42 loc) · 1017 Bytes
/
Server.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#[
Server
]#
import net, nativesockets, os, threadpool
proc isSocketClosed(sock: Socket): bool =
try:
if sock.getSocketError() == 10054.OSErrorCode:
return true
except:
return false
proc recvMsg(sock: Socket) {.thread.} =
while true:
if sock.isSocketClosed():
return
let cmds: string = sock.recv(1)
stdout.write cmds
proc sendMsg(sock: Socket) {.thread.} =
var
o: TaintedString
while true:
if sock.isSocketClosed():
return
try:
o = stdin.readLine()
sock.send(o & "\r\L")
except:
return
when isMainModule:
let
socket = newSocket()
port = 4444
socket.bindAddr(Port(port))
socket.listen()
var
client: Socket
address = ""
socket.acceptAddr(client, address)
echo "Got connection from: " & address
spawn recvMsg(client)
spawn sendMsg(client)
sync()
echo "Closed"