You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
750 B

package main
import (
"fmt"
"log"
"net"
"time"
"github.com/gansidui/gotcp/examples/echo"
)
func main() {
tcpAddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8989")
checkError(err)
conn, err := net.DialTCP("tcp", nil, tcpAddr)
checkError(err)
echoProtocol := &echo.EchoProtocol{}
// ping <--> pong
for i := 0; i < 3; i++ {
// write
conn.Write(echo.NewEchoPacket([]byte("hello"), false).Serialize())
// read
p, err := echoProtocol.ReadPacket(conn)
if err == nil {
echoPacket := p.(*echo.EchoPacket)
fmt.Printf("Server reply:[%v] [%v]\n", echoPacket.GetLength(), string(echoPacket.GetBody()))
}
time.Sleep(2 * time.Second)
}
conn.Close()
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}