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.

52 lines
968 B

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aceld/zinx/znet"
  5. "net"
  6. )
  7. func main() {
  8. //客户端goroutine,负责模拟粘包的数据,然后进行发送
  9. conn, err := net.Dial("tcp", "127.0.0.1:7777")
  10. if err != nil {
  11. fmt.Println("client dial err:", err)
  12. return
  13. }
  14. //创建一个封包对象 dp
  15. dp := znet.NewDataPack()
  16. //封装一个msg1包
  17. msg1 := &znet.Message{
  18. Id: 0,
  19. DataLen: 5,
  20. Data: []byte{'h', 'e', 'l', 'l', 'o'},
  21. }
  22. sendData1, err := dp.Pack(msg1)
  23. if err != nil {
  24. fmt.Println("client pack msg1 err:", err)
  25. return
  26. }
  27. msg2 := &znet.Message{
  28. Id: 1,
  29. DataLen: 7,
  30. Data: []byte{'w', 'o', 'r', 'l', 'd', '!', '!'},
  31. }
  32. sendData2, err := dp.Pack(msg2)
  33. if err != nil {
  34. fmt.Println("client temp msg2 err:", err)
  35. return
  36. }
  37. //将sendData1,和 sendData2 拼接一起,组成粘包
  38. sendData1 = append(sendData1, sendData2...)
  39. //向服务器端写数据
  40. conn.Write(sendData1)
  41. //客户端阻塞
  42. select {}
  43. }