/
client.ooc
39 lines (29 loc) · 1.16 KB
/
client.ooc
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
use zeromq
import zeromq, structs/ArrayList
main: func (args: ArrayList<String>) {
// The only application thread is the main thread.
// One I/O thread in the thread pool will do.
// This client is a requester
// Connect to the server
port := 5555
addr := "tcp://%s:%d" format(match {
case args size > 1 => args[1]
case => "localhost"
}, port)
"Speaking with server %s" format(addr toCString()) println()
s := Socket new(Context new(1), SocketType req). connect(addr)
while (true) {
"> " print()
stdout flush()
message := stdin readLine()
if(stdin eof?()) break;
// zeromq messages are binary strings, they don't care about encoding and stuff
// we just use C-style zero-terminated strings here.
s send(Message new(message toCString(), message length() + 1, null, null))
// recv is a blocking call
reply := s recv()
// Let's hope the server isn't sending some carefully crafted byte sequence
// that might make our client execute code as root. Oh noes!
"< %s" printfln(reply data())
}
}