Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
udp-demo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
demo
udp-demo
Commits
08e3cd02
Commit
08e3cd02
authored
Mar 25, 2021
by
barnettZQG
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change server
parent
6a20375b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
32 deletions
+41
-32
client.go
cmd/client/client.go
+20
-13
server.go
cmd/server/server.go
+14
-12
protocol.go
protocol/protocol.go
+3
-3
protocol_test.go
protocol/protocol_test.go
+4
-4
No files found.
cmd/client/client.go
View file @
08e3cd02
...
...
@@ -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
)
}
cmd/server/server.go
View file @
08e3cd02
...
...
@@ -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
=
r
esponseMessage
(
messageByte
)
response
=
protocol
.
R
esponseMessage
(
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
:=
c
reateCommandMessage
()
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
.
C
reateCommandMessage
()
conn
.
WriteToUDP
(
command
,
clientrAddr
)
fmt
.
Printf
(
"send command %X to %s
\n
"
,
command
,
clientrAddr
)
timer
.
Reset
(
time
.
Second
*
30
)
}
}
func
main
()
{
...
...
cmd/server
/protocol.go
→
protocol
/protocol.go
View file @
08e3cd02
package
main
package
protocol
import
(
"fmt"
...
...
@@ -54,7 +54,7 @@ func createTime() string {
return
fmt
.
Sprintf
(
"%X"
,
time
.
Now
()
.
Unix
())
}
func
r
esponseMessage
(
input
[]
byte
)
[]
byte
{
func
R
esponseMessage
(
input
[]
byte
)
[]
byte
{
response
:=
Hextob
(
"F1B000000000"
)
// 序列号
...
...
@@ -67,7 +67,7 @@ func responseMessage(input []byte) []byte {
var
num
int64
=
200
func
c
reateCommandMessage
()
[]
byte
{
func
C
reateCommandMessage
()
[]
byte
{
response
:=
Hextob
(
"F1B5FF000000"
)
num
+=
1
if
num
>
1000
{
...
...
cmd/server
/protocol_test.go
→
protocol
/protocol_test.go
View file @
08e3cd02
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
(
r
esponseMessage
(
Hextob
(
"F1B000000006000199014C000219100000ED8862120362D81046"
))))
t
.
Log
(
BytetoH
(
R
esponseMessage
(
Hextob
(
"F1B000000006000199014C000219100000ED8862120362D81046"
))))
}
func
TestCreateCommandMessage
(
t
*
testing
.
T
)
{
t
.
Log
(
strconv
.
FormatInt
(
201
,
16
))
t
.
Log
(
BytetoH
(
c
reateCommandMessage
()))
t
.
Log
(
BytetoH
(
C
reateCommandMessage
()))
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment