Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
796 B
1
Indexable
Never
package main

import (
	"fmt"
	"gopkg.in/natefinch/lumberjack.v2"
	"log"
	"net"
)

func main() {
	fmt.Printf("Beginning of the script")
	MCAST_GRP := "239.255.0.1"
	//MCAST_PORT := 10042
	logFile := &lumberjack.Logger{
		Filename:   "udp_multicast.log",
		MaxSize:    1, // megabytes
		MaxBackups: 5,
		MaxAge:     30, // days
	}
	defer logFile.Close()
	log.SetOutput(logFile)

	addr, err := net.ResolveUDPAddr("udp", MCAST_GRP+":"+"9999")
	if err != nil {
		panic(err)
	}

	conn, err := net.ListenMulticastUDP("udp", nil, addr)
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	buffer := make([]byte, 1024)
	for {
		n, _, err := conn.ReadFromUDP(buffer)
		if err != nil {
			panic(err)
		}
		fmt.Println("Received message:", string(buffer[:n]))
		log.Printf("%s", string(buffer[:n]))

	}
}