[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Can anyone help, please?

maehhheeyy

2/12/2008 10:11:00 PM

Hi, right now I'm using Python and Multicast. I have the code for
Multicast receiver on Python but I keep getting this error;

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))

This is the code that I am using:

import socket
import time

ANY = "0.0.0.0"
SENDERPORT = 1501
MCAST_ADDR = "224.168.2.9"
MCAST_PORT = 1600

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
status = sock.setsockopt(socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(MCAST_ADDR) +
socket.inet_aton(ANY));

sock.setblocking(0)
ts = time.time()
while 1:
try:
data, addr = sock.recvfrom(1024)
except socket.error, e:
pass
else:
print "We got data!"
print "FROM: ", addr
print "DATA: ", data

I'm using the UDP multicast. Is there anything that I did wrong in
this code?

Can anyone please help me solve this problem?
2 Answers

Paul McGuire

2/12/2008 11:43:00 PM

0

On Feb 12, 4:10 pm, maehhheeyy <maehhhe...@gmail.com> wrote:
> Hi, right now I'm using Python and Multicast. I have the code for
> Multicast receiver on Python but I keep getting this error;
>
> 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))
>
> This is the code that I am using:
>
<snip>
>
> I'm using the UDP multicast. Is there anything that I did wrong in
> this code?
>
> Can anyone please help me solve this problem?

I cannot help you directly, but googling for this error number and
message and chasing a few Usenet posts led me to "Beej's Guide to
Network Programming Using Internet Sockets" (http://beej...
bgnet/), which someone else having your problem reported to have been
helpful (and it looks like it was recently updated).

Good luck,
-- Paul

see

2/13/2008 2:46:00 PM

0

In article <c38126a1-0a4e-4a2e-9c64-70afc313f622@s12g2000prg.googlegroups.com>,
maehhheeyy <maehhheeyy@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- .