golang 实现
最后更新于:2022-04-02 02:57:23
[TOC]
## 安装
安装protobuf库
`go get github.com/golang/protobuf/proto
`
目录
```
protobuf_dmeo
├── go.mod
├── proto
│ ├── test.pb.go
│ └── test.proto
├── server.go
└──client.go
```
运行 ``` cd protoc protoc --go_out=. test.proto cd ../ go run server.go go run client.go ```
';
server.go
``` package main import ( "fmt" "net" "os" stProto "protobuf_dmeo/proto" //protobuf编解码库,下面两个库是相互兼容的,可以使用其中任意一个 "github.com/golang/protobuf/proto" //"github.com/gogo/protobuf/proto" ) func main() { //监听 listener, err := net.Listen("tcp", "localhost:6600") if err != nil { panic(err) } for { conn, err := listener.Accept() if err != nil { panic(err) } fmt.Println("new connect", conn.RemoteAddr()) go readMessage(conn) } } //接收消息 func readMessage(conn net.Conn) { defer conn.Close() buf := make([]byte, 4096, 4096) for { //读消息 cnt, err := conn.Read(buf) if err != nil { panic(err) } stReceive := &stProto.UserInfo{} pData := buf[:cnt] //protobuf解码 err = proto.Unmarshal(pData, stReceive) if err != nil { panic(err) } fmt.Println("receive", conn.RemoteAddr(), stReceive) if stReceive.Message == "stop" { os.Exit(1) } } } ```client.go
``` package main import ( "bufio" "fmt" "net" "os" stProto "protobuf_dmeo/proto" "time" //protobuf编解码库,下面两个库是相互兼容的,可以使用其中任意一个 "github.com/golang/protobuf/proto" //"github.com/gogo/protobuf/proto" ) func main() { strIP := "localhost:6600" var conn net.Conn var err error //连接服务器 for conn, err = net.Dial("tcp", strIP); err != nil; conn, err = net.Dial("tcp", strIP) { fmt.Println("connect", strIP, "fail") time.Sleep(time.Second) fmt.Println("reconnect...") } fmt.Println("connect", strIP, "success") defer conn.Close() //发送消息 cnt := 0 sender := bufio.NewScanner(os.Stdin) for sender.Scan() { cnt++ stSend := &stProto.UserInfo{ Message: sender.Text(), Length: *proto.Int(len(sender.Text())), Cnt: *proto.Int(cnt), } //protobuf编码 pData, err := proto.Marshal(stSend) if err != nil { panic(err) } //发送 conn.Write(pData) if sender.Text() == "stop" { return } } } ```proto/test.proto
``` syntax = "proto3"; //指定版本,必须要写(proto3、proto2) package proto; option go_package = "./"; enum FOO { X = 0; }; //message是固定的。UserInfo是类名,可以随意指定,符合规范即可 message UserInfo{ string message = 1; //消息 int32 length = 2; //消息大小 int32 cnt = 3; //消息计数 } ```运行 ``` cd protoc protoc --go_out=. test.proto cd ../ go run server.go go run client.go ```