frp/tests/echo_server.go

44 lines
630 B
Go
Raw Normal View History

2017-03-09 17:52:32 +01:00
package tests
2016-05-17 13:13:37 +02:00
import (
2017-03-08 19:03:47 +01:00
"bufio"
2016-05-17 13:13:37 +02:00
"fmt"
"io"
2016-05-17 13:13:37 +02:00
2017-03-08 19:03:47 +01:00
"github.com/fatedier/frp/utils/net"
2016-05-17 13:13:37 +02:00
)
2017-03-09 17:52:32 +01:00
func StartEchoServer() {
l, err := net.ListenTcp("127.0.0.1", 10701)
2016-05-17 13:13:37 +02:00
if err != nil {
fmt.Printf("echo server listen error: %v\n", err)
return
}
for {
c, err := l.Accept()
if err != nil {
fmt.Printf("echo server accept error: %v\n", err)
return
}
go echoWorker(c)
}
}
2017-03-08 19:03:47 +01:00
func echoWorker(c net.Conn) {
br := bufio.NewReader(c)
2016-05-17 13:13:37 +02:00
for {
2017-03-08 19:03:47 +01:00
buf, err := br.ReadString('\n')
if err == io.EOF {
break
}
2016-05-17 13:13:37 +02:00
if err != nil {
fmt.Printf("echo server read error: %v\n", err)
return
}
2017-03-08 19:03:47 +01:00
c.Write([]byte(buf + "\n"))
2016-05-17 13:13:37 +02:00
}
}