frp/test/e2e/pkg/request/request.go

32 lines
667 B
Go
Raw Normal View History

2020-06-02 22:48:55 +08:00
package request
import (
"fmt"
"net"
"time"
)
func SendTCPRequest(port int, content []byte, timeout time.Duration) (res string, err error) {
c, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
err = fmt.Errorf("connect to tcp server error: %v", err)
return
}
defer c.Close()
c.SetDeadline(time.Now().Add(timeout))
return sendTCPRequestByConn(c, content)
}
func sendTCPRequestByConn(c net.Conn, content []byte) (res string, err error) {
c.Write(content)
buf := make([]byte, 2048)
n, errRet := c.Read(buf)
if errRet != nil {
err = fmt.Errorf("read from tcp error: %v", errRet)
return
}
return string(buf[:n]), nil
}