roku1.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "os"
  7. "time"
  8. )
  9. func PostFind() string {
  10. file, err := os.ReadFile("finder.txt")
  11. if err != nil {
  12. log.Print(err)
  13. }
  14. addr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
  15. if err != nil {
  16. log.Print(err)
  17. }
  18. laddr, err := net.ResolveUDPAddr("udp", ":0") // "192.168.254.13:9000")
  19. if err != nil {
  20. log.Print(err)
  21. }
  22. // "192.168.254.13:9000")
  23. // conn, err := net.DialUDP("udp", laddr, addr)
  24. /*
  25. ListenUDP -- to SEND? WAT?
  26. https://github.com/golang/go/issues/13391
  27. Ok, maybe you don't want to read it -- the developers are cranky when
  28. asked why Listen would be able to SEND.
  29. */
  30. conn, err := net.ListenUDP("udp", laddr)
  31. if err != nil {
  32. log.Print(err)
  33. }
  34. defer conn.Close()
  35. conn.WriteTo(file, addr)
  36. // conn.Write(file)
  37. log.Println("Posted Request for devices")
  38. // future: Handle multiple devices
  39. conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(5)))
  40. buffer := make([]byte, 512)
  41. n, fromaddr, err := conn.ReadFrom(buffer) // Read(buffer)
  42. fmt.Printf("%d, %#v, %#v\n", n, fromaddr, err)
  43. var output string = string(buffer[0:n])
  44. //fmt.Printf("GOT: [%s]\n", output)
  45. // log.Printf("Read: %#v\n", buffer)
  46. return output
  47. }
  48. /*
  49. func main() {
  50. fmt.Println("Start:")
  51. PostFind()
  52. fmt.Println("Done!")
  53. }
  54. */