[TOC] ## net.Listen ``` func main() { conn=make(map[string]net.Conn) l, e := net.Listen("tcp", ":9010") if e != nil { panic(e) } defer l.Close() for{ accept, e := l.Accept() if e != nil { log.Println(e) } go handleCon(accept) } } func handleCon(accept net.Conn) { defer accept.Close() byt :=make([]byte,2048) for { n, err := accept.Read(byt) if err != nil { continue } fmt.Println(string(byt[:n])) } } ``` ## SetReadDeadline ``` l, e := net.Listen("tcp", ":9010") if e != nil { panic(e) } defer l.Close() for{ accept, e := l.Accept() if e != nil { log.Println(e) } accept.SetReadDeadline(time.Now().Add(9*time.Second)) conn[accept.RemoteAddr().String()]=accept go handleCon(accept) } ``` ## net.Dial ``` c, e := net.Dial("tcp", ":9010") if e != nil { panic(e) } c.Write([]byte("hello word")) ``` ## net.ParseIP ``` ip := net.ParseIP("127.0.0.1") fmt.Println(ip.String()) // 127.0.0.1 ip = net.ParseIP("123") fmt.Println(ip) // <nil> ``` ## net.LookupHost 根据域名获取 ip ``` addrs, err := net.LookupHost("www.baidu.com") if err != nil { fmt.Println(err) return } for i, v := range addrs { fmt.Println(i, "=>", v) } //0 => 61.135.169.121 //1 => 61.135.169.125 ```