-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflood.go
90 lines (75 loc) · 1.86 KB
/
flood.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
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"log"
"net"
"runtime"
"time"
)
func main() {
start := time.Now()
numClient, numThread := loopInput()
runtime.GOMAXPROCS(numThread)
//ping server repeatedly with each routine
pingChan := make(chan string, numClient)
for i := 0; i < numClient; i++ {
go Ping("tcp", "127.0.0.1:4040", pingChan)
}
var m string
for i := 0; i < numClient; i++ {
m = <-pingChan
log.Println(i+1, m)
}
log.Println(time.Since(start))
log.Println("flood closed")
}
//create a ping to the specified server
func Ping(proto, addr string, out chan<- string) {
c, err := net.Dial(proto, addr)
if err != nil {
log.Fatal(err)
}
defer c.Close()
msg := []byte("0101010101010101010101001010101001010101")
_, err = c.Write(msg)
if err != nil {
log.Fatal(err)
}
buf := make([]byte, 1024)
_, err = c.Read(buf)
if err != nil {
log.Fatal(err)
}
out <- string(buf)
}
func loopInput() (int, int) {
needInput := true
input := []int{0, 0}
for needInput {
clientsNum, procsNum, updateNeedInput := askInput()
input[0] = clientsNum
input[1] = procsNum
needInput = updateNeedInput
}
return input[0], input[1]
}
func askInput() (int, int, bool) {
fmt.Println("This program will provide clients to flood a server")
fmt.Println("Input the amount of clients to send to the server")
var numClient int
_, err1 := fmt.Scanln(&numClient)
if err1 != nil {
fmt.Println("not a valid input, requires an integer ")
return 0, 0, true
}
fmt.Println("Input the amount of processors to use")
var numProcs int
_, err2 := fmt.Scanln(&numProcs)
if err2 != nil {
fmt.Println("Invalid number of threads. Try again with an integer")
return 0, 0, true
}
fmt.Println("Starting the flood with ", numClient, " clients using a GOMAXPROCS number of ", numProcs, " .")
fmt.Println("----------------------------------------------")
return numClient, numProcs, false
}