沒難度肤粱,直接上代碼
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
}
//網(wǎng)客戶端發(fā)送消息
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)
}
}