I am trying to work with UDP and Golang. Because my horizons are microcontrollers, where Go packets (TinyGo) are not completely supported, I need to use as minimal amount of packets as possible. So, I would like to use only the "net" packet, not "net/http". Here (with "net") I can work with UDP using such functions as DialUDP, ListenUDP, ReadToUDP and WriteToUDP. I want to use only them. I saw many samples of working with UDP in Go, showing how to send "Hello" message, but my question is: Is there a way to prepare a data (probably a slice of bytes) containing an "http request" and send it using the mentioned UDP functions? How to prepare such data?
So, is there a way to prepare such request working with UDP and get answers? Or only using "net/http" we can do it?
I mean standard request used to discover SSDP devices (for example network speakers) in a local network, which looks (when using "net/http" packet) like below here. Sending such broadcast message makes all SSDP devices responses, sending back their parameters.
const (
ssdpSearchMethod = "M-SEARCH"
ssdpDiscover = `"ssdp:discover"`
ssdpUDP4Addr = "239.255.255.250:1900"
ssdpAll = "ssdp:all"
)
responseBytes := make([]byte, 2048)
req := (&http.Request{
Method: ssdpSearchMethod,
Host: ssdpUDP4Addr,
URL: &url.URL{Opaque: "*"},
Header: http.Header{
"HOST": []string{ssdpUDP4Addr},
"MX": []string{strconv.FormatInt(int64(3), 10)},
"MAN": []string{ssdpDiscover},
"ST": []string{ssdpAll},
},
})
n, _, err := httpu.conn.ReadFrom(responseBytes)
bytes.Buffer, and put the contents of that buffer into a UDP packet.