-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCC65_20192_EB-space-ship.go
More file actions
60 lines (56 loc) · 1009 Bytes
/
CC65_20192_EB-space-ship.go
File metadata and controls
60 lines (56 loc) · 1009 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"net"
)
const (
cREPLY = iota
cREGISTER
cNOTIFY
cCHECK
)
type tMsg struct {
Code int
Addr string
Addrs []string
Num int
}
func main() {
var remoteAddr string
msg := tMsg{cCHECK, "not important", []string{}, 0}
for {
fmt.Print("Remote: ")
fmt.Scanln(&remoteAddr)
if remoteAddr == "." { break }
for {
fmt.Print("Num: ")
fmt.Scanf("%d\n", &msg.Num)
if msg.Num < 0 { break }
sendRec(remoteAddr, msg, func(c net.Conn) {
dec := json.NewDecoder(c)
var msg tMsg
dec.Decode(&msg)
if msg.Num < 0 {
fmt.Println("Unknown")
} else {
fmt.Println("Known!")
}
})
}
}
}
func sendRec(remoteAddr string, msg tMsg, resp func(c net.Conn)) {
if conn, err := net.Dial("tcp", remoteAddr); err != nil {
log.Println(err.Error())
} else {
defer conn.Close()
enc := json.NewEncoder(conn)
fmt.Println("Sending", msg, "to", remoteAddr)
enc.Encode(&msg)
if resp != nil {
resp(conn)
}
}
}