FAQ
Hey

I want to know witch computers are connected to my multicast network:

package main

import (
"bufio"
"fmt"
"net"
"os"
"time"
)

func main() {
laddr, err := net.ResolveUDPAddr("udp", ":0")
mcaddr, err := net.ResolveUDPAddr("udp", "224.0.1.60:1888")
check(err)
conn, err := net.ListenMulticastUDP("udp", nil, mcaddr)
lconn, err := net.ListenUDP("udp", laddr)
check(err)
go listen(conn)
go write(conn, lconn, mcaddr)
go connections(mcaddr)
time.Sleep(1e15)
}

func listen(conn *net.UDPConn) {
for {
b := make([]byte, 256)
_, _, err := conn.ReadFromUDP(b)
check(err)
fmt.Println("read", string(b))
}
}

func write(conn, lconn *net.UDPConn, mcaddr *net.UDPAddr) {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("Input: ")
txt, _, err := reader.ReadLine()
b := make([]byte, 256)
copy(b, txt)
check(err)
_, err = lconn.WriteToUDP(b, mcaddr)
check(err)
}
}

func connections(mcaddr *net.UDPAddr) {
for {
names, err := net.LookupAddr("224.0.1.60")
check(err)
nConnections := len(names)
fmt.Println(nConnections)
}
}

func check(err error) {
if err != nil {
panic(err)
}
}

I want the connections function to continously uppdate the names array with
the computers connected to the network, but it seems like i use the wrong
address as input to the LookupAddr function, witch address should i use and
how do i get it? i have been trying different ways to make the mcaddr into
a string and try using that as an input to the function. Is what i am
trying to do even possible?

What happens now is that each computer on the multicast only prints 1, and
only have their own name in the names list. I want to see the number of
connected computers and their names.

And yeah, i'm quite new to go, so any help would be much apreciated!

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Marius Westgaard at Feb 26, 2013 at 12:36 pm
    I have set up a UDP multicast network, now i wan't to know who is on it,
    how can i do that?
    My code so far is:

    package main

    import (
    "bufio"
    "fmt"
    "net"
    "os"
    "time"
    )

    func main() {
    laddr, err := net.ResolveUDPAddr("udp", ":0")
    mcaddr, err := net.ResolveUDPAddr("udp", "224.0.1.60:1888")
    check(err)
    conn, err := net.ListenMulticastUDP("udp", nil, mcaddr)
    lconn, err := net.ListenUDP("udp", laddr)
    check(err)
    go listen(conn)
    go write(conn, lconn, mcaddr)
    go connections(mcaddr)
    time.Sleep(1e15)
    }

    func listen(conn *net.UDPConn) {
    for {
    b := make([]byte, 256)
    _, _, err := conn.ReadFromUDP(b)
    check(err)
    fmt.Println("read", string(b))
    }
    }

    func write(conn, lconn *net.UDPConn, mcaddr *net.UDPAddr) {
    reader := bufio.NewReader(os.Stdin)
    for {
    fmt.Print("Input: ")
    txt, _, err := reader.ReadLine()
    b := make([]byte, 256)
    copy(b, txt)
    check(err)
    _, err = lconn.WriteToUDP(b, mcaddr)
    check(err)
    }
    }

    func connections(mcaddr *net.UDPAddr) {
    for {
    names, err := net.LookupAddr("224.0.1.60")
    check(err)
    nConnections := len(names)
    fmt.Println(nConnections)
    time.Sleep(time.Second*1)
    }
    }

    func check(err error) {
    if err != nil {
    panic(err)
    }
    }

    I want the connections function to keep an updated list of who is on the
    network (going to use the list later, first i just need to know how to get
    it) and how many computers. With my current code everything that happens is
    that every computer just prints 1, and they only got themselves in the
    list. The multicast part works fine tough, i can send messages from one
    computer and every other computer on the network running the script prints
    them. How can i see who is connected? is it even possible? i have been
    trying to experiment with changing the input to the net.LookupAddr function
    by trying to convert mcaddr to a string type IP without any luck. Meaning i
    never managed to convert mcaddr to a string.

    I am quite new at go so any help would be appreciated!

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Niklas Schnelle at Feb 26, 2013 at 2:23 pm
    AFAIK LookupAdr just looks up a list of addresses for e.g. a domain name
    and multicast networking has no such thing as looking up all members of a
    multicast group.
    That wouldn't make much sense in a network where faults happen and
    multicast groups can be arbitarily large.
    If you want each client to know about any other client you'll have to make
    them communicate with each other. For simplicity let's assume messages
    don't get lost
    (they do in reality however on say a LAN (not wireless) thats quite rare).
    Then you could have all clients send their own address to the multicast
    group announcing their presence. For more reliability you could do this
    repeatably as a kind of heartbeat maybe making sure that not all clients
    send their message at the same time.
    Then you can have each client maintain a list of all known peers, scrubbing
    all clients that it hasn't heart from in e.g. 3 heartbeattimes.
    On Tuesday, February 26, 2013 12:41:36 PM UTC+1, Marius Westgaard wrote:

    Hey

    I want to know witch computers are connected to my multicast network:

    package main

    import (
    "bufio"
    "fmt"
    "net"
    "os"
    "time"
    )

    func main() {
    laddr, err := net.ResolveUDPAddr("udp", ":0")
    mcaddr, err := net.ResolveUDPAddr("udp", "224.0.1.60:1888")
    check(err)
    conn, err := net.ListenMulticastUDP("udp", nil, mcaddr)
    lconn, err := net.ListenUDP("udp", laddr)
    check(err)
    go listen(conn)
    go write(conn, lconn, mcaddr)
    go connections(mcaddr)
    time.Sleep(1e15)
    }

    func listen(conn *net.UDPConn) {
    for {
    b := make([]byte, 256)
    _, _, err := conn.ReadFromUDP(b)
    check(err)
    fmt.Println("read", string(b))
    }
    }

    func write(conn, lconn *net.UDPConn, mcaddr *net.UDPAddr) {
    reader := bufio.NewReader(os.Stdin)
    for {
    fmt.Print("Input: ")
    txt, _, err := reader.ReadLine()
    b := make([]byte, 256)
    copy(b, txt)
    check(err)
    _, err = lconn.WriteToUDP(b, mcaddr)
    check(err)
    }
    }

    func connections(mcaddr *net.UDPAddr) {
    for {
    names, err := net.LookupAddr("224.0.1.60")
    check(err)
    nConnections := len(names)
    fmt.Println(nConnections)
    }
    }

    func check(err error) {
    if err != nil {
    panic(err)
    }
    }

    I want the connections function to continously uppdate the names array
    with the computers connected to the network, but it seems like i use the
    wrong address as input to the LookupAddr function, witch address should i
    use and how do i get it? i have been trying different ways to make the
    mcaddr into a string and try using that as an input to the function. Is
    what i am trying to do even possible?

    What happens now is that each computer on the multicast only prints 1, and
    only have their own name in the names list. I want to see the number of
    connected computers and their names.

    And yeah, i'm quite new to go, so any help would be much apreciated!
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jeff R. Allen at Feb 27, 2013 at 2:39 pm
    I put together a little demo of how to receive UDP multicast messages just
    now:

    http://code.google.com/p/jra-go/source/browse/cmd/hsrp/routers.go

    As a bonus, it also shows how to use enocding/binary.Read in order to
    unmarshal a wire format into a structure that Go can use.

    If you don't have any routers on your subnet that are talking HSRP, you
    won't see anything. You'd need to write something to send out
    advertisements on some other port (just in case someone is listening for
    HSRP) and then change the port in this demo.

    -jeff

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 26, '13 at 12:35p
activeFeb 27, '13 at 2:39p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2020 Grokbase