package main import ( "io" "log" "net" "time" ) func handleConn(conn net.Conn ){ defer conn.Close(); for { _,err := io.WriteString(conn,time.Now().Format("15:04:05 ")); if err !=nil { return; } time.Sleep(1 * time.Second); } } func main(){ listener,err:= net.Listen("tcp","localhost:8000"); if err != nil { log.Fatal("error"); } for { conn,err:= listener.Accept();//循环阻塞 if err != nil { log.Fatal("connect is break"); continue; } handleConn(conn); } }