From 497eec41f426b720273560da9bff44fc17b8b091 Mon Sep 17 00:00:00 2001 From: boyxiaolong Date: Sat, 12 Aug 2017 23:15:25 +0800 Subject: [PATCH 1/2] add client and server sample --- client_sample.go | 27 +++++++++++++++++++++++++++ server_sample.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 client_sample.go create mode 100644 server_sample.go diff --git a/client_sample.go b/client_sample.go new file mode 100644 index 0000000..9b784c9 --- /dev/null +++ b/client_sample.go @@ -0,0 +1,27 @@ +package main + +import ( + "log" + "net" + "bytes" + "encoding/binary" +) + +func main() { + serverinfo := "localhost:30000" + conn, err := net.Dial("tcp", serverinfo) + if err != nil { + log.Println("conn err", err) + return + } + + + var buf bytes.Buffer + head := make([]byte, 4) + data := "hello world!!" + binary.BigEndian.PutUint32(head, uint32(len(data))) + binary.Write(&buf, binary.BigEndian, head) + buf.WriteString(data) + conn.Write(buf.Bytes()) + conn.Close() +} diff --git a/server_sample.go b/server_sample.go new file mode 100644 index 0000000..28fd6d4 --- /dev/null +++ b/server_sample.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "awesomeProject/tcp" +) + +type TcpCallback struct { + +} + +func (tcpCallbak *TcpCallback)OnConnected(conn *tcp.TCPConn) { + fmt.Println("connected ",conn.LocalIP()) + +} +func (tcpCallbak *TcpCallback)OnDisconnected(conn *tcp.TCPConn) { + fmt.Println("disconnected ",conn.LocalIP()) +} + +func (tcpCallbak *TcpCallback)OnMessage(conn *tcp.TCPConn, p tcp.Packet){ + fmt.Println("get msg ", string(p.Bytes())) +} + +func main() { + tcpCallback := TcpCallback{} + protocol := tcp.DefaultProtocol{} + server := tcp.NewTCPServer("localhost:30000", &tcpCallback, &protocol) + server.ListenAndServe() +} \ No newline at end of file From 67078f04edf567c89f8f092037a6d7345c0e3882 Mon Sep 17 00:00:00 2001 From: boyxiaolong Date: Sun, 13 Aug 2017 22:19:06 +0800 Subject: [PATCH 2/2] modify --- .gitignore | 2 ++ client_sample.go | 23 ++++++++++++++++++++--- server_sample.go | 12 +++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index daf913b..bf2bb1d 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ _testmain.go *.exe *.test *.prof + +.DS_Store diff --git a/client_sample.go b/client_sample.go index 9b784c9..469ea6e 100644 --- a/client_sample.go +++ b/client_sample.go @@ -5,6 +5,8 @@ import ( "net" "bytes" "encoding/binary" + "fmt" + "io" ) func main() { @@ -17,11 +19,26 @@ func main() { var buf bytes.Buffer - head := make([]byte, 4) + writehead := make([]byte, 4) data := "hello world!!" - binary.BigEndian.PutUint32(head, uint32(len(data))) - binary.Write(&buf, binary.BigEndian, head) + binary.BigEndian.PutUint32(writehead, uint32(len(data))) + binary.Write(&buf, binary.BigEndian, writehead) buf.WriteString(data) conn.Write(buf.Bytes()) + + readhead := make([]byte, 4) + _, err = io.ReadFull(conn, readhead) + if err != nil { + log.Fatal(err) + } + + packetLength := binary.BigEndian.Uint32(readhead) + sendBuf := make([]byte, packetLength) + _, err = io.ReadFull(conn, sendBuf) + if err != nil { + fmt.Println("err ", err) + } + + fmt.Println(string(sendBuf)) conn.Close() } diff --git a/server_sample.go b/server_sample.go index 28fd6d4..b22e3ff 100644 --- a/server_sample.go +++ b/server_sample.go @@ -3,6 +3,7 @@ package main import ( "fmt" "awesomeProject/tcp" + "log" ) type TcpCallback struct { @@ -18,7 +19,16 @@ func (tcpCallbak *TcpCallback)OnDisconnected(conn *tcp.TCPConn) { } func (tcpCallbak *TcpCallback)OnMessage(conn *tcp.TCPConn, p tcp.Packet){ - fmt.Println("get msg ", string(p.Bytes())) + getBytes := p.Bytes() + + fmt.Println("get msg ", string(getBytes)) + + dataLen := len(getBytes) + packet := tcp.NewDefaultPacket(tcp.PacketType(dataLen), getBytes) + err := conn.Send(packet) + if err != nil { + log.Fatal(err) + } } func main() {