-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
72 lines (66 loc) · 1.38 KB
/
Copy pathserver_test.go
File metadata and controls
72 lines (66 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"testing"
"fmt"
"net"
"strconv"
"time"
)
func TestStartServer(t *testing.T) {
host := "127.0.0.1"
pin := 8888
pout := 8089
ans := make(chan string)
msg := "TEST\r\n"
go StartServer(&host, &pin, &pout)
go SendUDPMessage(&host, &pin, &msg)
go ReciveUDPmessage(&host, &pout, ans)
select {
case result := <-ans:
if result == "TEST" {
fmt.Println("Test Succeded")
} else {
t.Fail()
break
}
case <- time.After(7 * time.Second):
fmt.Println("Test timouted")
}
}
func SendUDPMessage(host *string, port *int, msg *string) {
packet := make([]byte, 1024)
address := *host + ":" + strconv.Itoa(*port)
conn, err := net.Dial("udp", address)
if err != nil {
fmt.Printf("Some error %v", err)
return
}
fmt.Fprintf(conn, *msg)
conn.Write(packet)
if err != nil {
fmt.Printf("Some error %v\n", err)
}
conn.Close()
}
func ReciveUDPmessage(host *string, port *int, comm chan <- string){
connect := "CONNECT\r\n"
packet := make([]byte, 1024)
p := make([]byte, 1024)
address := *host + ":" + strconv.Itoa(*port)
conn, err := net.Dial("udp", address)
if err != nil {
fmt.Printf("Some error %v", err)
return
}
fmt.Fprintf(conn, connect)
conn.Write(packet)
if err != nil {
fmt.Printf("Some error %v\n", err)
}
_ ,err = conn.Read(p)
if err != nil {
fmt.Printf("Some error %v\n", err)
}
result := string(p[0:4])
comm <- result
}