服务端
package main import( "fmt" "net" "io/ioutil" ) func main(){ service := ":9999" tcpAddr,_ := net.ResolveTCPAddr("tcp4",service) listener,_ := net.ListenTCP("tcp",tcpAddr) for{ conn,err := listener.Accept() if err != nil{ continue } result,_:= ioutil.ReadAll(conn) fmt.Println(string(result),"\n") conn.Close() } }
客户端
package main import( "fmt" "net" ) func main(){ tcpAddr,_ := net.ResolveTCPAddr("tcp4",":9999") conn,_ := net.DialTCP("tcp",nil,tcpAddr) for i:=0;i<10;i++{ conn.Write([]byte("hello world\n")) fmt.Println("Write Success!") } }
有疑问加站长微信联系(非本文作者)