A python script that can send multicast data on LINUX. In fact, it is not too difficult to compile. The specific code is as follows: first declare that this is not what I wrote, but what I found, but what others wrote is quite good.
The code is as follows:
# UDP multicast examples, Hugo Vincent, 2005-05-14.
Import socket
Def send (data, port = 50000, addr = '192.1.100 '):
"Send (data [, port [, addr])-multicasts a UDP datax ."""
# Create the socket
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
# Make the socket multicast-aware, and set TTL.
S. setsockopt (socket. IPPROTO_IP, socket. IP_MULTICAST_TTL, 20) # Change TTL (= 20) to suit
# Send the data
S. sendto (data, (addr, port ))
Def recv (port = 50000, addr = "239.192.1.100", buf_size = 1024 ):
"Recv ([port [, addr [, buf_size])-waits for a datax and returns the data ."""
# Create the socket
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
# Set some options to make it multicast-friendly
S. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
Try:
S. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEPORT, 1)
T AttributeError:
Pass # Some systems don't support SO_REUSEPORT
S. setsockopt (socket. SOL_IP, socket. IP_MULTICAST_TTL, 20)
S. setsockopt (socket. SOL_IP, socket. IP_MULTICAST_LOOP, 1)
# Bind to the port
S. bind ('', port ))
# Set some more multicast options
Intf = socket. gethostbyname (socket. gethostname ())
S. setsockopt (socket. SOL_IP, socket. IP_MULTICAST_IF, socket. inet_aton (intf) + socket. inet_aton ('0. 0.0.0 '))
S. setsockopt (socket. SOL_IP, socket. IP_ADD_MEMBERSHIP, socket. inet_aton (addr) + socket. inet_aton ('0. 0.0.0 '))
# Receive the data, then unregister multicast receive membership, then close the port
Data, sender_addr = s. recvfrom (buf_size)
S. setsockopt (socket. SOL_IP, socket. IP_DROP_MEMBERSHIP, socket. inet_aton (addr) + socket. inet_aton ('0. 0.0.0 '))
S. close ()
Return data
The code is as follows:
#! /Usr/bin/env python
From socket import * from time import sleep, time, ctime
HOST = '1970. 0.0.15 'PORT = 229 BUFSIZ = 9999 ADDR = (HOST, PORT)
UdpCliSock = socket (AF_INET, SOCK_DGRAM)
Data = 't' * 200 count = 0 while (count <100): udpCliSock. sendto (data, ADDR) sleep (1) count = count + 1
UdpCliSock. close ()