myconn.SetReadBuffer(1)
最关键的部分是设置缓冲区只有1字节
再用
myconn.Read 读取
这样 当缓冲区有1字节的数据 read就能响应了,而不至于等到默认的8字节时 才能读取、、





func testTcp() {
var( lists []*net.TCPConn myconn *net.TCPConn err error ) service := ":2000" //设置服务器监听的端口 tcpAddr, _ := net.ResolveTCPAddr("tcp", service) //创建 tcpAddr数据 fmt.Println("tcpAddr ") mylistener, _ := net.ListenTCP("tcp", tcpAddr) for { myconn, err = mylistener.AcceptTCP() fmt.Println(err) myconn.SetReadBuffer(1) lists = append(lists,myconn) } }

 参考

https://golang.hotexamples.com/zh/examples/net/TCPConn/SetReadBuffer/golang-tcpconn-setreadbuffer-method-examples.html

相关文章:

  • 2021-08-09
  • 2022-12-23
  • 2020-01-08
  • 2022-12-23
  • 2022-12-23
  • 2021-06-12
  • 2022-12-23
猜你喜欢
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
相关资源
相似解决方案