G路由/通道-未退出ctlr-c 请考虑下面的GO代码,我写的是从一个给定的文件名(OS.ARG(1))扫描一个TCP端口(OS.ARG(2))的主机名。它读取每个主机名,尝试连接。如果连接失败,它会将失败的主机名附加到outfile package main import( "fmt" "os" "log" "bufio" "time" "net" ) func main(){ argc := len(os.Args) if argc < 3 { fmt.Printf("Invalid usage") log.Fatal("Invalid usage") } stamp := time.Now().UnixNano() outfile := fmt.Sprintf("%s%d.txt", "/tmp/port_check", stamp) filename := os.Args[1] file, err := os.Open(filename) checkerr(err) f, err := os.OpenFile(outfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) checkerr(err) defer f.Close() port := os.Args[2] channel := make(chan string,17) fscanner := bufio.NewScanner(file) for fscanner.Scan(){ _line := fscanner.Text() go check_sender(_line,port,f,channel) } _count := 0 for out := range channel{ fmt.Println("_count is:", _count, out) _count += 1 } close(channel) } func checkerr(err error){ if err != nil { fmt.Println(err) log.Fatal(err) } } func check_sender(sender string, port string, f *os.File, channel chan string){ address_string := fmt.Sprintf("%s:%s", sender, port) _, err := net.DialTimeout("tcp", address_string,4 * time.Second) if err != nil { write_this := fmt.Sprintf("%s\n", sender) f.WriteString(write_this) } channel <- sender }

G路由/通道-未退出ctlr-c 请考虑下面的GO代码,我写的是从一个给定的文件名(OS.ARG(1))扫描一个TCP端口(OS.ARG(2))的主机名。它读取每个主机名,尝试连接。如果连接失败,它会将失败的主机名附加到outfile package main import( "fmt" "os" "log" "bufio" "time" "net" ) func main(){ argc := len(os.Args) if argc < 3 { fmt.Printf("Invalid usage") log.Fatal("Invalid usage") } stamp := time.Now().UnixNano() outfile := fmt.Sprintf("%s%d.txt", "/tmp/port_check", stamp) filename := os.Args[1] file, err := os.Open(filename) checkerr(err) f, err := os.OpenFile(outfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) checkerr(err) defer f.Close() port := os.Args[2] channel := make(chan string,17) fscanner := bufio.NewScanner(file) for fscanner.Scan(){ _line := fscanner.Text() go check_sender(_line,port,f,channel) } _count := 0 for out := range channel{ fmt.Println("_count is:", _count, out) _count += 1 } close(channel) } func checkerr(err error){ if err != nil { fmt.Println(err) log.Fatal(err) } } func check_sender(sender string, port string, f *os.File, channel chan string){ address_string := fmt.Sprintf("%s:%s", sender, port) _, err := net.DialTimeout("tcp", address_string,4 * time.Second) if err != nil { write_this := fmt.Sprintf("%s\n", sender) f.WriteString(write_this) } channel <- sender },go,Go,当以以下方式运行时: $ go run port_scan.go /tmp/meh.txt 80 _count is: 0 someblog50063432.blogspot.com _count is: 1 someblog922816893.blogspot.com _count is: 2 someblog622823698.blogspot.com _count is: 3 someblog1074223783.blogspot.com _count is: 4 someblog1876

当以以下方式运行时:

$ go run port_scan.go /tmp/meh.txt 80
_count is: 0 someblog50063432.blogspot.com
_count is: 1 someblog922816893.blogspot.com
_count is: 2 someblog622823698.blogspot.com
_count is: 3 someblog1074223783.blogspot.com
_count is: 4 someblog1876411881.blogspot.com
^Csignal: interrupt < ----------------------- this 

谢谢请参阅上面的更新1-计算您分叉的线程数,在我们获得相同的计数后,
close(channel)
似乎可以工作。但问题是,所有这些都是手工完成的,还是有一种自动的方法来处理这样的情况?@flughting_learner:很难给出推荐,因为您最初的设计意图并不真正为人所知。具体来说,您可以创建一个大小为17的通道。我猜你想用这个来限制并行连接尝试的次数。但实际上您并没有这样做,因为您首先连接了一些东西,然后将其推入通道,也就是说,您所限制的只是并行返回的结果数量,而不是并行执行的连接数量。@Steffen,然后如何限制并行连接尝试的次数?这是我的意图,我以为我在这么做。@trapping\u学习者:例如。
$ go run port_scan.go /tmp/meh.txt 80
_count is: 0 someblog50063432.blogspot.com
_count is: 1 someblog922816893.blogspot.com
_count is: 2 someblog622823698.blogspot.com
_count is: 3 someblog1074223783.blogspot.com
_count is: 4 someblog1876411881.blogspot.com
^Csignal: interrupt < ----------------------- this 
    channel := make(chan string,17)
    fscanner := bufio.NewScanner(file)
+   spin := 0
    for fscanner.Scan(){
        _line := fscanner.Text()
        go check_sender(_line,port,f,channel)
+       spin += 1
    }

        _count := 0
        for out := range channel{
            fmt.Println("_count is:", _count, out)
            _count += 1
+           if _count == spin {
+               close(channel)
+           }
        }

-       close(channel)
 }