I want to create a multicast socket connection in a ad-hoc network topology.
I have a client/server implementation in python that is WORKING FINE on a normal network configuration, meaning a standard network config with internet connection.
This is the code for the client connector, the "subscriber":
if __name__ == "__main__":
multicast_group = '224.0.0.1'
server_address = ('', 10000)
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the server address
sock.bind(server_address)
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
In a normal, internet connected network, this works fine, but in an ad-hoc network topology, this throws the error:
File "main.py" line 33
setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
File "usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self.__sock, name)(*args)
socket.error: [Error 19] No such device
Any ideas what is happening?
Thanks
A working solution is to manually set your network interface for socket to use:
sock.setsockopt(socket.SOL_SOCKET, 25, 'bat0')
And add membership to the group and hostname:
group = socket.inet_aton(multicast_group)
intf = socket.gethostbyname(socket.gethostname())
sock.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, group + socket.inet_aton(intf))