-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathserver.go
More file actions
59 lines (48 loc) · 1.04 KB
/
server.go
File metadata and controls
59 lines (48 loc) · 1.04 KB
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
package main
import (
"fmt"
"net"
"os"
"strings"
)
type clientType map[string]bool
var clients = clientType{}
func (c clientType) keys(filter string) string {
output := []string{}
for key := range c {
if key != filter {
output = append(output, key)
}
}
return strings.Join(output, ",")
}
// Server --
func Server() {
localAddress := ":9595"
if len(os.Args) > 2 {
localAddress = os.Args[2]
}
addr, _ := net.ResolveUDPAddr("udp", localAddress)
conn, _ := net.ListenUDP("udp", addr)
for {
buffer := make([]byte, 1024)
bytesRead, remoteAddr, err := conn.ReadFromUDP(buffer)
if err != nil {
panic(err)
}
incoming := string(buffer[0:bytesRead])
fmt.Println("[INCOMING]", incoming)
if incoming != "register" {
continue
}
clients[remoteAddr.String()] = true
for client := range clients {
resp := clients.keys(client)
if len(resp) > 0 {
r, _ := net.ResolveUDPAddr("udp", client)
conn.WriteTo([]byte(resp), r)
fmt.Printf("[INFO] Responded to %s with %s\n", client, string(resp))
}
}
}
}