github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/poller/cmd/client/main.go (about) 1 package main 2 3 import ( 4 "log" 5 "net" 6 "strconv" 7 8 "github.com/weedge/lib/poller/cmd/common" 9 ) 10 11 func init() { 12 log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) 13 } 14 15 func main() { 16 go start() 17 select {} 18 } 19 20 func start() { 21 log.Println("start") 22 conn, err := net.Dial("tcp", "127.0.0.1:8085") 23 if err != nil { 24 log.Println("Error dialing", err.Error()) 25 return // 终止程序 26 } 27 28 codec := common.NewCodec(conn) 29 30 go func() { 31 for { 32 _, err = codec.Read() 33 if err != nil { 34 log.Println(err) 35 return 36 } 37 for { 38 bytes, ok, err := codec.Decode() 39 // 解码出错,需要中断连接 40 if err != nil { 41 log.Println(err) 42 return 43 } 44 if ok { 45 log.Println("receive:", string(bytes)) 46 continue 47 } 48 break 49 } 50 } 51 }() 52 53 n := 1 54 for i := 0; i < n; i++ { 55 msg := []byte("hello" + strconv.Itoa(i)) 56 _, err := conn.Write(common.Encode(msg)) 57 if err != nil { 58 log.Println(err) 59 return 60 } 61 log.Println("send:", string(msg)) 62 } 63 }