On the client, I want to set the UDP source port when sending a udp packet. On the server, I want to know what the UDP source port was received on.
Client:
package main
import (
"net"
)
func main() {
s, err := net.ResolveUDPAddr("udp4", "127.0.0.1:1234")
c, err := net.DialUDP("udp4", nil, s)
if err != nil {
fmt.Println(err)
return
}
}
Server:
package main
import (
"net"
"time"
)
func main() {
s, err := net.ResolveUDPAddr("udp4", "127.0.0.1:1234")
if err != nil {
fmt.Println(err)
return
}
connection, err := net.ListenUDP("udp4", s)
if err != nil {
fmt.Println(err)
return
}
}
In the above client code, is there a way for me to set the source port? In the above server code, is there a way for me to know the source port used?
https://golang.org/pkg/net/#DialUDP
func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error)
Both laddr and raddr use UDPAddr struct, but you're not setting laddr.
laddr, err := net.ResolveUDPAddr("udp", "<source_int>:50000")
raddr := net.UDPAddr{IP: net.ParseIP("<dest>"), Port: 50000}
conn, err := net.DialUDP("udp", laddr, &raddr)