-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (65 loc) · 1.44 KB
/
main.go
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
73
74
75
76
77
78
79
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
var shouldListen bool
var port int
var isUDP bool
var isClient bool
flag.BoolVar(&isClient, "z", false, "Test Connection?")
flag.BoolVar(&shouldListen, "l", false, "Start Listener?")
flag.BoolVar(&isUDP, "u", false, "Use UDP")
flag.IntVar(&port, "port", 8080, "Port to listen on")
flag.IntVar(&port, "p", 8080, "alias for --port")
flag.Parse()
args := flag.Args() //get positional args
if isClient {
if len(args) < 2 {
fmt.Println("Format: ccnc -z <ip> <port>\nEg: ccnc -z localhost 8080")
os.Exit(1)
}
ip := args[0]
var start_port int
var end_port int
var err error
port_string := args[1]
if strings.Contains(port_string, "-") {
ports := strings.Split(port_string, "-")
start_port, err = strconv.Atoi(ports[0])
if err != nil {
fmt.Println("Invalid start_port number")
os.Exit(1)
}
end_port, err = strconv.Atoi(ports[1])
if err != nil {
fmt.Println("Invalid end_port number")
os.Exit(1)
}
if start_port > end_port {
fmt.Println("Start port cannot be greater than end port")
}
} else {
start_port, err = strconv.Atoi(port_string)
if err != nil {
fmt.Println("Invalid port number")
os.Exit(1)
}
end_port = start_port
}
TCPClient(ip, start_port, end_port)
return
}
if shouldListen {
if isUDP {
StartUDPServer(port)
} else {
StartTCPServer(port)
}
return
}
}