Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > multicast

Reply
Thread Tools

multicast

 
 
Seb
Guest
Posts: n/a
 
      11-05-2009
I'm trying to implement a multicast module. I'm inspired by this blog
http://chaos.weblogs.us/archives/164

I'd like to get some comments on the code so far but more importantly
some suggestions on how I can implement reliable multicasting.


best regards,
Seb
 
Reply With Quote
 
 
 
 
Seb
Guest
Posts: n/a
 
      11-05-2009
Forgot the code... doh!

#! /usr/bin/env python

import socket
import time

class MulticastSender(object):
def __init__(self, MCAST_ADDR = "224.168.2.9", MCAST_PORT = 1600):
self.MCAST_ADDR = MCAST_ADDR
self.MCAST_PORT = MCAST_PORT
ANY = "0.0.0.0"
SENDERPORT=1501
#create a UDP socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
#allow multiple sockets to use the same PORT number
self.sock.setsockopt(socket.SOL_SOCKET,socket.SO_R EUSEADDR,1)
#The sender is bound on (0.0.0.0:1501)
self.sock.bind((ANY,SENDERPORT))
#Tell the kernel that we want to multicast and that the data is sent
#to everyone (255 is the level of multicasting)
self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL,
255)

def send(self, data):
self.sock.sendto(data, (self.MCAST_ADDR, self.MCAST_PORT));

class MulticastReceiver(object):
def __init__(self, MCAST_ADDR = "224.168.2.9", MCAST_PORT = 1600):
ANY = "0.0.0.0"
#create a UDP socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
#allow multiple sockets to use the same PORT number
self.sock.setsockopt(socket.SOL_SOCKET,socket.SO_R EUSEADDR,1)
#Bind to the port that we know will receive multicast data
self.sock.bind((ANY,MCAST_PORT))
#tell the kernel that we are a multicast socket
self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL,
255)
#Tell the kernel that we want to add ourselves to a multicast group
#The address for the multicast group is the third param
status = self.sock.setsockopt(socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP, socket.inet_aton(MCAST_ADDR) +
socket.inet_aton(ANY));
self.sock.setblocking(0)

def setblocking(self, flag):
self.sock.setblocking(flag)

def recv(self, size = 1024):
return self.sock.recvfrom(size)

class Multicast(object):
def __init__(self):
self.__ms = MulticastSender()
self.__mr = MulticastReceiver()

def send(self, data):
self.__ms.send(data)

def recv(self, size = 1024):
return self.__mr.recv()

if __name__ == "__main__":
mc = Multicast()
while 1:
try:
data, addr = mc.recv()
except socket.error, e:
#print "sock.error: ", e
pass
else:
print "FROM: ", addr
print "DATA: ", data
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IPRIP could not join the multicast group 224.0.0.9 =?Utf-8?B?U2NvdA==?= Wireless Networking 0 12-12-2004 07:29 PM
QOS for multicast P Cisco 0 10-19-2003 12:26 AM
multicast in msmq 3.0 Vladik ASP .Net 0 09-09-2003 11:42 AM
Multicast on Catalyst 2950 Rick Cisco 2 07-25-2003 01:34 PM
Broadcast and Multicast problem sPiDEr Cisco 3 07-11-2003 07:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57