我需要recive udp包并将其发送到广播地址其他界面。
我写了udp转发服务器。这是工作! 如何将源udp地址更改为src地址收入包?
package main
import (
"fmt"
"net"
"os"
)
func CheckError(err error) {
if err != nil {
fmt.Println("Error: " , err)
os.Exit(0)
}
}
func main() {
ServerAddr,err := net.ResolveUDPAddr("udp4",":10001")
CheckError(err)
LocalAddr, err := net.ResolveUDPAddr("udp4", "192.168.164.83:10001")
CheckError(err)
RemoteAddr, err := net.ResolveUDPAddr("udp4", "192.168.164.255:10002")
CheckError(err)
Conn, err := net.DialUDP("udp4", LocalAddr, RemoteAddr)
CheckError(err)
defer Conn.Close()
ServerConn, err := net.ListenUDP("udp4", ServerAddr)
CheckError(err)
defer ServerConn.Close()
buf := make([]byte, 2048)
for {
n,addr,err := ServerConn.ReadFromUDP(buf)
fmt.Println("Received ",n , "bytes from ",addr)
if err != nil {
fmt.Println("Error-recive: ",err)
} else {
go Conn.Write(buf[0:n])
}
}
}