- 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://www.codesnippet.cn/detail/3107201410079.html
来源: http://www.codesnippet.cn/detail/3107201410079.html