-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
46 lines (44 loc) · 774 Bytes
/
test.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
package main
import (
"fmt"
"net"
"sort"
"time"
)
func worker(ports, results chan int) {
for p := range ports {
address := fmt.Sprintf("www.xiaodi8.com:%d", p)
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
if err != nil {
results <- 0
continue
}
conn.Close()
results <- p
}
}
func main() {
ports := make(chan int, 100)
results := make(chan int)
var openports []int
for i := 0; i < cap(ports); i++ {
go worker(ports, results)
}
go func() {
for i := 1; i <= 500; i++ {
ports <- i
}
}()
for i := 0; i < 500; i++ {
port := <-results
if port != 0 {
openports = append(openports, port)
}
}
close(ports)
close(results)
sort.Ints(openports)
for _, port := range openports {
fmt.Printf("%d open\n", port)
}
}