没难度,直接上代码
package main
import (
"io"
"log"
"net"
"time"
)
func main() {
listener,err := net.Listen("tcp","localhost:8000")
if err != nil {
log.Fatal(err)
}
for{
//等待客户端请求
conn,err := listener.Accept()
if err != nil {
log.Fatal(err)
return
}
//网客户端发送消息
go handler1(conn)
}
}
func handler1(c net.Conn) {
for {
_,err := io.WriteString(c,"我来自服务端\n")
if err != nil {
log.Fatal(err)
continue
}
time.Sleep(1 * time.Second)
}
}