证明socket是全双工,read,write是相互独立的
package main
import (
"fmt"
"net"
"time"
)
func main() {
listener, _ := net.Listen("tcp","0.0.0.0:5566")
go func() {
client, _ := net.Dial("tcp", "127.0.0.1:5566")
s := "hello"
client.Write([]byte(s))
s2 := "world"
client.Write([]byte(s2))
}()
conn, _ := listener.Accept()
time.Sleep(time.Second * 10)
var buf [10]byte
conn.Read(buf[:]) // [104 101 108 108 111 119 111 114 108 100]
fmt.Println(buf)
conn.Read(buf[:])
fmt.Println(buf) // empty
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: