-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.odin
72 lines (53 loc) · 1.46 KB
/
main.odin
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package otis
import "core:bytes"
import "core:fmt"
import "core:net"
import "core:strings"
PORT :: 6379
main :: proc() {
endpoint := net.Endpoint {
address = net.IP4_Loopback,
port = PORT,
}
listen_socket, listen_err := net.listen_tcp(endpoint)
if listen_err != nil {
fmt.printf("Listen error: %s\n", listen_err)
return
}
fmt.printf("Server listening on port: %d\n", endpoint.address)
client_socket, client_addr, client_accept_error := net.accept_tcp(listen_socket)
if client_accept_error != nil {
fmt.printf("Accept error: %s\n", client_accept_error)
}
fmt.printf("New client connected from: %v\n", client_addr)
handleClient(client_socket)
}
handleClient :: proc(client_socket: net.TCP_Socket) {
defer net.close(client_socket)
for {
data_in_bytes := make([]byte, 1024)
defer delete(data_in_bytes)
bytes_received, err := net.recv_tcp(client_socket, data_in_bytes[:])
if err != nil {
fmt.printf("Error while receiving data: %s\n", err)
break
}
if bytes_received == 0 {
fmt.println("Connection closed by client")
break
}
received_str := string(data_in_bytes[:bytes_received])
if strings.has_prefix(received_str, "exit") {
fmt.println("Connection ended")
break
}
str := "PONG"
// Echo back to client
_, send_err := net.send_tcp(client_socket, transmute([]byte)str)
if send_err != nil {
fmt.printf("Error sending response: %s\n", send_err)
break
}
fmt.printf("Client said: %s", received_str)
}
}