Commit 08e3cd02 authored by barnettZQG's avatar barnettZQG

change server

parent 6a20375b
......@@ -6,28 +6,35 @@ import (
"time"
"github.com/sirupsen/logrus"
"rainbond.io/udptest/protocol"
)
func main() {
//address := "106.15.152.33:27065"
address := "127.0.0.1:8080"
address := "106.15.152.33:27065"
//address := "127.0.0.1:8080"
udpAddr, err := net.ResolveUDPAddr("udp", address)
con, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
logrus.Errorf("create udp client failure %s", err.Error())
}
var n = 0
for {
message := fmt.Sprintf("hello word %d", n)
con.Write([]byte(message))
fmt.Printf("send:%s \n", message)
n++
data := make([]byte, 1024)
n, _, err := con.ReadFromUDP(data)
if err != nil {
fmt.Println("failed read udp msg, error: " + err.Error())
go func() {
for {
data := make([]byte, 1024)
n, _, err := con.ReadFromUDP(data)
if err != nil {
fmt.Println("failed read udp msg, error: " + err.Error())
}
fmt.Printf("receive response: %X \n", data[:n])
}
fmt.Println(string(data[:n]))
}()
write(protocol.Hextob("F1B000000006031B99014C00020E100000ED88621203A0DC6955"), con, udpAddr)
for {
time.Sleep(time.Second * 3)
write(protocol.Hextob("F1AA00000000031899014C00020E1000985DF5D9"), con, udpAddr)
}
}
func write(data []byte, con *net.UDPConn, addr *net.UDPAddr) {
fmt.Printf("write %X \n", data)
con.Write(data)
}
......@@ -4,8 +4,10 @@ import (
"fmt"
"net"
"os"
"time"
"github.com/sirupsen/logrus"
"rainbond.io/udptest/protocol"
)
// 限制goroutine数量
......@@ -28,10 +30,10 @@ func udpProcess(conn *net.UDPConn, index int) {
fmt.Printf("addr %s resquest: %X\n", rAddr, messageByte)
clientrAddr = rAddr
var response []byte
requestType := BytetoH(messageByte)[0:4]
requestType := protocol.BytetoH(messageByte)[0:4]
switch requestType {
case "F1B0":
response = responseMessage(messageByte)
response = protocol.ResponseMessage(messageByte)
default:
fmt.Printf("not support message type %s \n", requestType)
}
......@@ -63,16 +65,16 @@ func udpServer(address string) {
func sendCommand(conn *net.UDPConn, addr *net.UDPAddr) {
fmt.Println("Start waiting send command")
// timer := time.NewTimer(time.Second * 30)
// for {
// select {
// case <-timer.C:
// }
command := createCommandMessage()
conn.WriteToUDP(command, clientrAddr)
fmt.Printf("send command %X to %s\n", command, clientrAddr)
// timer.Reset(time.Second * 30)
// }
timer := time.NewTimer(time.Second * 30)
for {
select {
case <-timer.C:
}
command := protocol.CreateCommandMessage()
conn.WriteToUDP(command, clientrAddr)
fmt.Printf("send command %X to %s\n", command, clientrAddr)
timer.Reset(time.Second * 30)
}
}
func main() {
......
package main
package protocol
import (
"fmt"
......@@ -54,7 +54,7 @@ func createTime() string {
return fmt.Sprintf("%X", time.Now().Unix())
}
func responseMessage(input []byte) []byte {
func ResponseMessage(input []byte) []byte {
response := Hextob("F1B000000000")
// 序列号
......@@ -67,7 +67,7 @@ func responseMessage(input []byte) []byte {
var num int64 = 200
func createCommandMessage() []byte {
func CreateCommandMessage() []byte {
response := Hextob("F1B5FF000000")
num += 1
if num > 1000 {
......
package main
package protocol
import (
"strconv"
......@@ -13,11 +13,11 @@ func TestCRC32(t *testing.T) {
t.Log(CRC32(Hextob("F1B000000006000199014C000219100000ED88621203")))
}
func TestResponseMessage(t *testing.T) {
t.Log(BytetoH(responseMessage(Hextob("F1B000000006000199014C000219100000ED8862120362D81046"))))
t.Log(BytetoH(ResponseMessage(Hextob("F1B000000006000199014C000219100000ED8862120362D81046"))))
}
func TestCreateCommandMessage(t *testing.T) {
t.Log(strconv.FormatInt(201, 16))
t.Log(BytetoH(createCommandMessage()))
t.Log(BytetoH(CreateCommandMessage()))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment