端口扫描:
package main import ( "fmt" "net" "strconv" ) func main() { service := "localhost:" //从1号端口扫描到1000号 for port := 1; port < 1000; port++ { _, err := net.DialTimeout("tcp", service+strconv.Itoa(port), 1000000000) //连接时间为1秒超过1秒视为失败 if err == nil { fmt.Println(port) } } }
原文:http://my.oschina.net/u/1537881/blog/295519