package main
import (
"fmt"
"net"
)
func main() {
tcpAddr, _ := net.ResolveTCPAddr("tcp4", ":6000")
listener, _ := net.ListenTCP("tcp", tcpAddr)
defer listener.Close()
for {
con, err := listener.AcceptTCP()
if err != nil {
//fmt.Println(err)
continue
}
fmt.Println(con.RemoteAddr().String(), " tcp connect success")
// 处理连接
go handleClient(con)
}
}
func handleClient(con *net.TCPConn) {
con.SetReadDeadline(time.Now().Add(time.Second * 30)) // 30s读取超时
defer con.Close()
buf := make([]byte, 1024)
for {
n, err := con.Read(buf)
if err != nil {
break
}
fmt.Printf("Read %x\n", buf[:n])
}
}
有疑问加站长微信联系(非本文作者)