-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtcp_server.hk
More file actions
35 lines (27 loc) · 689 Bytes
/
tcp_server.hk
File metadata and controls
35 lines (27 loc) · 689 Bytes
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
//
// tcp_server.hk
//
import socket;
let { AF_INET, SOCK_STREAM } = socket;
let { SOL_SOCKET, SO_REUSEADDR } = socket;
let { set_option, bind, listen, accept, readln, writeln, close } = socket;
let port = 9000;
let server = socket.new(AF_INET, SOCK_STREAM, 0);
set_option(server, SOL_SOCKET, SO_REUSEADDR, 1);
bind(server, "0.0.0.0", port);
listen(server, 10);
let client = accept(server);
if (!client) {
panic("accept failed");
}
println("Client connected");
loop {
let received = readln(client);
if (is_empty(received))
break;
println("Received: " + received);
writeln(client, "Reply: " + received);
}
close(client);
close(server);
println("Client disconnected");