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.
64 lines
1.2 KiB
64 lines
1.2 KiB
package discovery
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
"xtcfs/config"
|
|
)
|
|
|
|
// 分隔符
|
|
const separator = "|"
|
|
|
|
func Listen() {
|
|
// 创建udp
|
|
addr, err := net.ResolveUDPAddr("udp", config.G.LocalIP+":"+config.G.Port)
|
|
ChkError(err)
|
|
//
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
ChkError(err)
|
|
// 延迟关闭
|
|
defer conn.Close()
|
|
|
|
buf := make([]byte, 1024)
|
|
for {
|
|
//
|
|
n, src, err := conn.ReadFromUDP(buf)
|
|
|
|
remoteAddr := src.IP.String() + ":" + config.G.Port
|
|
|
|
if err != nil {
|
|
fmt.Printf("Failed to read from %s: %v\n", remoteAddr, err)
|
|
continue
|
|
}
|
|
|
|
// 处理接收到的信息
|
|
message := string(buf[:n])
|
|
// 对消息进行切割
|
|
parts := strings.Split(message, separator)
|
|
if len(parts) != 2 {
|
|
fmt.Printf("Received malformed message from %s: %s\n", remoteAddr, message)
|
|
continue
|
|
}
|
|
|
|
// 切割
|
|
msgtype := parts[0]
|
|
// 有这个的话就进行操作
|
|
// 告知后面的程序执行下面的目录
|
|
|
|
if msgtype == "xdml" {
|
|
opdir := parts[1]
|
|
// 查找对应的文件目录
|
|
fmt.Print("opertation directory is :%v", opdir)
|
|
}
|
|
fmt.Print("parts from udp read :%v", parts)
|
|
}
|
|
}
|
|
|
|
func ChkError(err error) {
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "Faltat error:", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|