MCPcopy
hub / github.com/txthinking/brook / EchoClient

Function EchoClient

echoclient.go:21–69  ·  view source on GitHub ↗
(server string, times int)

Source from the content-addressed store, hash-verified

19)
20
21func EchoClient(server string, times int) error {
22 c, err := DialTCP("tcp", "", server)
23 if err != nil {
24 return err
25 }
26 defer c.Close()
27 var b [1024 * 2]byte
28 for i := 0; i < times; i++ {
29 if _, err := c.Write([]byte(c.LocalAddr().String())); err != nil {
30 return err
31 }
32 i, err := c.Read(b[:])
33 if err != nil {
34 return err
35 }
36 if c.LocalAddr().String() == string(b[:i]) {
37 fmt.Printf("TCP: src:%s -> dst:%s\n", c.LocalAddr().String(), c.RemoteAddr().String())
38 fmt.Printf("TCP: dst:%s <- src:%s\n", c.LocalAddr().String(), c.RemoteAddr().String())
39 }
40 if c.LocalAddr().String() != string(b[:i]) {
41 fmt.Printf("TCP: src:%s -> dst:proxy -> src:proxy -> dst:%s\n", c.LocalAddr().String(), c.RemoteAddr().String())
42 fmt.Printf("TCP: dst:%s <- src:proxy <- dst:%s <- src:%s\n", c.LocalAddr().String(), string(b[:i]), c.RemoteAddr().String())
43 }
44 }
45
46 c1, err := DialUDP("udp", "", server)
47 if err != nil {
48 return err
49 }
50 defer c1.Close()
51 for i := 0; i < times; i++ {
52 if _, err := c1.Write([]byte(c1.LocalAddr().String())); err != nil {
53 return err
54 }
55 i, err := c1.Read(b[:])
56 if err != nil {
57 return err
58 }
59 if c1.LocalAddr().String() == string(b[:i]) {
60 fmt.Printf("UDP: src:%s -> dst:%s\n", c1.LocalAddr().String(), c1.RemoteAddr().String())
61 fmt.Printf("UDP: dst:%s <- src:%s\n", c1.LocalAddr().String(), c1.RemoteAddr().String())
62 }
63 if c1.LocalAddr().String() != string(b[:i]) {
64 fmt.Printf("UDP: src:%s -> dst:proxy -> src:proxy -> dst:%s\n", c1.LocalAddr().String(), c1.RemoteAddr().String())
65 fmt.Printf("UDP: dst:%s <- src:proxy <- dst:%s <- src:%s\n", c1.LocalAddr().String(), string(b[:i]), c1.RemoteAddr().String())
66 }
67 }
68 return nil
69}

Callers 1

mainFunction · 0.92

Calls 5

CloseMethod · 0.45
WriteMethod · 0.45
LocalAddrMethod · 0.45
ReadMethod · 0.45
RemoteAddrMethod · 0.45

Tested by

no test coverage detected