1 year ago

#349758

test-img

James Calo

Multicast UDP communication using golang.org/x/net/ipv4 Not working even after copying comments to existing question

I couldn't comment on question which is essentially the same question I am asking but my code will not work. I have modified the same example they used so that it is a hybrid of the example and their question with the comments implemented and yet it still wont work. Any help will be greatly appriciated.

package main

import (
    "fmt"
    "log"
    "net"

    "golang.org/x/net/ipv4"
)

func main() {
    c, err := net.ListenPacket("udp4", ":0") // mDNS over UDP
    if err != nil {
        log.Fatal("1 ", err)
    }
    defer c.Close()
    p := ipv4.NewPacketConn(c)

    en0, err := net.InterfaceByName("WiFi")
    if err != nil {
        log.Fatal("2 ", err)
    }
    // ipRemote := net.UDPAddr{IP: net.IPv4(224, 4, 3, 224), Port: 60300}
    ipRemote, _ := net.ResolveUDPAddr("udp", "224.4.3.224:60300")
    if err := p.JoinGroup(en0, ipRemote); err != nil {
        log.Fatal("3 ", err)
    }
    defer p.LeaveGroup(en0, ipRemote)
    // err = p.SetControlMessage(ipv4.FlagTTL, true)
    // if err != nil {
    //  log.Fatal("4 Why error? Because it is not Implemented on Windows  ", err)
    // }

    b := make([]byte, 1500)
    for {
        _, _, peer, err := p.ReadFrom(b)
        if err != nil {
            log.Fatal("5 ", err)
        }
        fmt.Println(peer)
        fmt.Println("\n\n*****************************\n\n", b)
        /*if !cm.Dst.IsMulticast() || !cm.Dst.Equal(ipRemote.IP) {
            continue
        }*/
        answers := []byte("FAKE-MDNS-ANSWERS") // fake mDNS answers, you need to implement this
        if _, err := p.WriteTo(answers, nil, peer); err != nil {
            log.Fatal("6 ", err)
        }
    }
}


go

web

udp

multicast

0 Answers

Your Answer

Accepted video resources