No description
Find a file
David Krause (enthus1ast) 0b374fb3cc fix tests/sender.nim
2024-05-20 11:02:47 +02:00
tests fix tests/sender.nim 2024-05-20 11:02:47 +02:00
.gitignore let older git versions ignore nimcache/ fixes #1 2019-01-18 18:43:23 +01:00
LICENSE Create LICENSE 2017-08-03 21:50:01 +02:00
multicast.nim Move pragma to the object name side 2024-05-18 17:36:19 +05:30
multicast.nimble Bump minimum Nim version to 1.0.10 2024-05-18 17:42:37 +05:30
README.md fixed poor english 2018-01-21 13:34:37 +01:00

nimMulticast

procs to join and leave a multicast group

installation

nimble install multicast

or

nimble install https://github.com/enthus1ast/nimMulticast

usage:

import net
import multicast

const 
  HELLO_PORT = 1900
  HELLO_GROUP = "239.255.255.250" # router discovery
  MSG_LEN = 1024

var 
  data: string = ""
  address: string = ""
  port: Port

var socket = newSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
socket.setSockOpt(OptReuseAddr, true)
socket.bindAddr(Port(HELLO_PORT))

if not socket.joinGroup(HELLO_GROUP):
  echo "could not join multicast group"
  quit()

socket.enableBroadcast true
echo "enabled broadcast for the socket, this is not needet for multicast only!"

# For testing we speak upnp manually
var disc = """M-SEARCH * HTTP/1.1
Host:239.255.255.250:1900
ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1
Man:"ssdp:discover"
MX:3""" & "\c\r\c\r" 

# Socket is still a "normal" socket.
# To send to the multicast group just send to its address
# Sending sockets does not have to be in the multicast group
discard socket.sendTo(HELLO_GROUP, Port(HELLO_PORT), disc)

# The socket is supposed to receive every udp datagram
# sent to the multicast group.
while true:
  echo "R: ", socket.recvFrom(data, MSG_LEN, address, port ), " ", address,":", port, " " , data

# Instruct the kernel to leave the group. 
assert socket.leaveGroup(HELLO_GROUP) == true