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.

60 lines
946 B

  1. package znet
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "time"
  7. )
  8. /*
  9. 模拟客户端
  10. */
  11. func ClientTest() {
  12. fmt.Println("Client Test ... start")
  13. //3秒之后发起测试请求,给服务端开启服务的机会
  14. time.Sleep(3 * time.Second)
  15. conn,err := net.Dial("tcp", "127.0.0.1:7777")
  16. if err != nil {
  17. fmt.Println("client start err, exit!")
  18. return
  19. }
  20. for {
  21. _, err := conn.Write([]byte("hahaha"))
  22. if err !=nil {
  23. fmt.Println("write error err ", err)
  24. return
  25. }
  26. buf :=make([]byte, 512)
  27. cnt, err := conn.Read(buf)
  28. if err != nil {
  29. fmt.Println("read buf error ")
  30. return
  31. }
  32. fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt)
  33. time.Sleep(1*time.Second)
  34. }
  35. }
  36. //Server 模块的测试函数
  37. func TestServer(t *testing.T) {
  38. /*
  39. 服务端测试
  40. */
  41. //1 创建一个server 句柄 s
  42. s := NewServer("[zinx V0.1]")
  43. /*
  44. 客户端测试
  45. */
  46. go ClientTest()
  47. //2 开启服务
  48. s.Serve()
  49. }