Can anyone help, please?

Douglas Wells see at signature.invalid
Wed Feb 13 09:46:04 EST 2008


In article <c38126a1-0a4e-4a2e-9c64-70afc313f622 at s12g2000prg.googlegroups.com>,
  maehhheeyy <maehhheeyy at gmail.com> writes:
> Hi, right now I'm using Python and Multicast. I have the code for
> Multicast receiver on Python but I keep getting this error;

Hi,

In the future please use a Subject: line that is relevant to your
inquiry.  Many people only read articles with "interesting"
subjects.  This is particularly true in groups like this one with
200+ messages a day.

> File "<string>", line 1, in bind
> error: (10049, "Can't assign requested address")
> 
> The error is coming from this line;
> sock.bind ((MCAST_ADDR, MCAST_PORT))

You didn't provide either the OS that you're running on, nor your
network interface configuration, both of which are relevant to use
of IP-multicast.  (Many/most languages, including Python, do not
have a separate networking model:  they simply provide access to
the one provided by the target platform.

> This is the code that I am using:
> 
> [ ... ]
> 
> ANY = "0.0.0.0"
> [ ... ]
> 
> status = sock.setsockopt(socket.IPPROTO_IP,
>                          socket.IP_ADD_MEMBERSHIP,
>                          socket.inet_aton(MCAST_ADDR) +
> socket.inet_aton(ANY));

If I had to guess based on facts available, I would guess that
your operating system requires you to select the appropriate
interface.  Try replacing the second argument (the "ANY" one) with
the address of the networking interface over which you wish to
communicate.  Note that the interface *must* be multicast-capable
(on many platforms), and "localhost" usually is not.  So, you want
an/the address on your Ethernet interface, for example.

I include below an example program that works on my system (FreeBSD)
and has been somewhat altered to be similar to your failing test case.

 - dmw

=========================================================================
#! /usr/bin/env python

# Receiver:

##############################

import socket
import struct
import time

MULTICAST_ADDR = '225.0.0.1'
PORT = 1200
REPORT_LENGTH = 1024000
BUFFSIZE = 1024

ANY_IPADDR = struct.pack ("!L", socket.INADDR_ANY)
INTERFACE_ADDR = socket.gethostbyname (socket.gethostname ())

ip = socket.inet_aton (MULTICAST_ADDR) + socket.inet_aton (INTERFACE_ADDR)

sock = socket.socket (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

sock.setsockopt (socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, ip)

sock.bind ((socket.inet_ntoa (ANY_IPADDR), PORT))

start = time.time ()
total_data_length = 0
print 'Listening ...'

while 1:
	try:
		data, addr = sock.recvfrom (BUFFSIZE)
		total_data_length += len (data)
		if (total_data_length % REPORT_LENGTH) == 0:
			elapsed = time.time () - start
			print "%7.3f: %d octets from %s" % (elapsed, total_data_length, addr)
	except socket.error, e:
		print "Socket error" + e
		break

##############################

=========================================================================

-- 
.   Douglas Wells             .  Connection Technologies      .
.   Internet:  -sp9804- -at - contek.com-                     .



More information about the Python-list mailing list