FAQ
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.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 4 | next ›
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 © 2021 Grokbase