Hey folks,
Our next pit stop here introduces you to the concept of Multicast Addressing and how we plan to
implement the very concept of it for sending and receiving data among various
devices.
Imagine a project group leader assigns a task to his group and all the other members abide him and execute the instruction? After that, each person reports to everyone else in the group during a formal meeting about their progress over time during the course of the project. In a lay man’s language, that is what exactly is the concept of multicast.
To talk more in
detail about the various functionality aspects of different multicast address (each
in specific), the above link would provide an in detail explanation of the
same.

Here are a few FAQ's which may help you on your way through...
Question 1 : Whom do we send the data to?
Answer : Whoever I want to!
Question 2 : What information do I need to know about whom to send the data to?
Answer : Get to know their IP address within the local multicast group.
What now, once I get to know the IP address....?
Question 3 : How to send the data to the IP address?
Answer : Socket programming.
Now, we begin with a step by step implementation of our device Discovery protocol...
# Step 1 : Device Discovery to fetch IP Address
Imagine a project group leader assigns a task to his group and all the other members abide him and execute the instruction? After that, each person reports to everyone else in the group during a formal meeting about their progress over time during the course of the project. In a lay man’s language, that is what exactly is the concept of multicast.
In depth about multicasting
Here’s to bring
to you a few insights about multicast addressing in technical terms..
As our research
goes and with respect to Wiki sources, multicast addressing is a logical
identifier for a group of hosts in a computer network, that are
available to process datagrams or frames intended to be multicast for
a designated network service.
Multicast addressing can be used in the Link Layer (Layer 2 in the OSI model), such as Ethernet multicast, and at the Internet Layer (Layer 3 for OSI) for Internet Protocol Version 4 (IPv4) or Version 6 (IPv6) multicast. The group range for addresses varies from 224.0.0.0 to 239.255.255.255.
Multicast addressing can be used in the Link Layer (Layer 2 in the OSI model), such as Ethernet multicast, and at the Internet Layer (Layer 3 for OSI) for Internet Protocol Version 4 (IPv4) or Version 6 (IPv6) multicast. The group range for addresses varies from 224.0.0.0 to 239.255.255.255.
So anyone can
take one of the addresses in the range and form a group of their own. However a
few addresses are reserved for specific purposes which you can find here

Device Discovery Protocol
Here are a few FAQ's which may help you on your way through...
Question 1 : Whom do we send the data to?
Answer : Whoever I want to!
Question 2 : What information do I need to know about whom to send the data to?
Answer : Get to know their IP address within the local multicast group.
What now, once I get to know the IP address....?
Question 3 : How to send the data to the IP address?
Answer : Socket programming.
Now, we begin with a step by step implementation of our device Discovery protocol...
# Step 1 : Device Discovery to fetch IP Address
import socket
import sys
import struct
from select import *
import subprocess , platform
# The commands array list takes care of recognising the Operating system (key) and executing the specific command (value) to get our own IP address
commands = {
'Darwin': "ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'",
'Linux': "/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'"
}
# Protocol messages to identify the beginning and ending of the device discovery protocol
JOINING_MESSAGE = "Hi"
CLOSING_MESSAGE = "Bye"
# device discovery forks a sub process and returns the IP address
def get_ip_address():
proc = subprocess.Popen( commands[platform.system()], shell=True, stdout=subprocess.PIPE )
return proc.communicate()[0].replace('\n', '')
# Step 2 : Creating a socket to start communicating with other devices
Assuming we have set MCAST_GRP = '224.3.29.X' and MCAST_PORT = 10000
#Create a datagram socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
AF_INET constant represent the address (and protocol) families, used for the first argument to socket().
SOCK_DGRAM constants represent the socket type as datagram, used for the second argument to socket().
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
AF_INET constant represent the address (and protocol) families, used for the first argument to socket().
SOCK_DGRAM constants represent the socket type as datagram, used for the second argument to socket().
The bind() system call is used to specify the association (local address, local port)
sock.bind(('', MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GROUP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
# Step 3 : Creating a list to dynamically populate it with IP addresses in the local network
multicast_group_ip = []
t=1
while True:
t = t+t;
if t > 10:
t = 1
We create 3 lists : read, write and error list and pass the arguments to the select api.
The last argument is a timeout value.
rlist , wlist, xlist = select([sock] , [] , [sock] ,t)
Note the use of a select API here.
The select api is a blocking and so we give it a timeout according to our needs.
The protocol we implement here for the timeout parameter is that of additive increase. Initially, when time=1 sec, it sends a multicast message. The next message is sent at t=2 sec. The consecutive messages are sent at t=4 sec and t=8 sec, following which it crosses a certain threshold of t=10 sec and we revert it back to t=1 sec.
if rlist:
# Read from list : We wait to receive messages from other active users
print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(1024)
#Print the received data
#Once data is received, send an acknowledgement message back to the sender
print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
print >>sys.stderr, 'sending acknowledgement to', address
sock.sendto('ack', address)
else:
# Send joining message to the multicast group
print >>sys.stderr, 'sending "%s"' % joining_message
sent = sock.sendto(joining_message, (MCAST_GRP,MCAST_PORT))
# Step 4 : Close the socket
sock.sendto(closing_message,(MCAST_GRP,MCAST_PORT))
print "Sending closing message",closing_message
sock.close()
We hope to have delivered the crux logic of the program.
You too can implement your own multicast client-server programs with an ease.
You too can implement your own multicast client-server programs with an ease.
Feel free to leave comments and share our blog on Facebook or Google+.
Till then, stay tuned folks ! :)