github.com/songzhibin97/gkit@v1.2.13/net/tcp/conn_func.go (about) 1 package tcp 2 3 import "time" 4 5 // Send 拨号并发送消息,关闭链接 6 func Send(addr string, data []byte, retry *Retry) error { 7 c, err := NewConn(addr, nil) 8 if err != nil { 9 return err 10 } 11 defer c.Close() 12 return c.Send(data, retry) 13 } 14 15 // SendRecv 拨号发送并读取响应 16 func SendRecv(addr string, data []byte, length int, retry *Retry) ([]byte, error) { 17 conn, err := NewConn(addr, nil) 18 if err != nil { 19 return nil, err 20 } 21 defer conn.Close() 22 return conn.SendRecv(data, length, retry) 23 } 24 25 // SendWithTimeout 创建并发送具有写入超时限制的连接 26 func SendWithTimeout(addr string, data []byte, timeout time.Duration, retry *Retry) error { 27 conn, err := NewConn(addr, nil) 28 if err != nil { 29 return err 30 } 31 defer conn.Close() 32 return conn.SendWithTimeout(data, timeout, retry) 33 } 34 35 // SendRecvWithTimeout 创建链接发送读取有超时限制的连接 36 func SendRecvWithTimeout(addr string, data []byte, receive int, timeout time.Duration, retry *Retry) ([]byte, error) { 37 conn, err := NewConn(addr, nil) 38 if err != nil { 39 return nil, err 40 } 41 defer conn.Close() 42 return conn.SendRecvWithTimeout(data, timeout, receive, retry) 43 }