-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathreference-server.factor
44 lines (36 loc) · 1.18 KB
/
reference-server.factor
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
USING: accessors alien.c-types alien.data byte-arrays
classes.struct io io.encodings.binary io.encodings.string
io.encodings.utf8 io.servers kernel literals namespaces
sequences unix.ffi unix.types ;
IN: reference-server
:: reference-server ( -- )
1024 <byte-array> :> buffer
AF_INET SOCK_STREAM 0 socket :> server
sockaddr-in malloc-struct
AF_INET >>family
0 >>addr
15000 htons >>port :> address
server address sockaddr-in heap-size bind drop
[
server 10 listen drop
server address 0 socklen_t <ref> accept :> client
client buffer 1024 0 recv
buffer swap head-slice utf8 decode print flush
client $[ "hello world\n" >byte-array ]
dup length unix.ffi:write drop
client close drop
t
] loop
server close drop ;
: reference-server2 ( -- )
binary <threaded-server>
"reference-server" >>name
15000 >>insecure
[
1024 read-partial [
[ utf8 decode print flush ] with-global
$[ "hello world\n" >byte-array ] io:write flush
] when*
] >>handler
start-server wait-for-server ;
MAIN: reference-server2