package main import ( "fmt" "io" "net" "time" "git.chuangxin1.com/csacred/toolkit/tcp" ) func main() { tcp.NewTCPServer(":2020", func(conn *net.TCPConn) { defer conn.Close() conn.SetNoDelay(true) //conn.SetKeepAlive(keepalive) //bufr := bufio.NewReader(conn) var buf = make([]byte, 4096) for { conn.SetReadDeadline(time.Now().Add(time.Second * 10)) //n, err := bufr.Read(buf) n, err := conn.Read(buf) if err != nil { if err == io.EOF { fmt.Println("EOF") } else if operr, ok := err.(*net.OpError); ok { fmt.Println("net.OpError") if operr.Timeout() { fmt.Println("I/O timeout: ", conn.RemoteAddr().String(), time.Now().Format("2006-01-02 15:04:05")) continue } } fmt.Println("err:", err) break } fmt.Printf("Read %d bytes\n", n) conn.Write(buf[:n]) } println("End ", conn.RemoteAddr().String()) }) }