0

I've found a code for a chat app in Python, but I can't find anything about the author or anyone on the site to help me with it..

this is a link to the whole code: http://files.myopera.com/manojsheokand666/blog/chat.py

I'm getting a feeling something is missing.. and I need this, I want to modify it and try to learn something more

I did some reading and this is my third time editing this post.. NOW, I'm able to stay connected without getting any error, but when I try to send(type in) something it's not sending nor receiving. But whenever I try to run a second app as another "person", I am getting a message on the first running app that "person" has connected, and the first app crashes with this error:

KeyError: ('127.0.0.1',62833) - note, the port is always diferent

While, the second app stays but it's not receiving anything or crashes if I run the app again.

What I did:

host = gethostbyname(gethostname()) #this actually gets 192.168.0.101 (my local IP to the router)
s.setsockopt(SOL_IP,IP_ADD_MEMBERSHIP,\
            inet_aton(addr)+inet_aton(host)) #i write 225.0.0.1 as 'addr'

Is there other way to get this working? I can run a simple server/chat using telnet but this GUI(tkinter) think makes it complicated for me, and I want to learn how this works.. Thanks!

Mite Stojanov
  • 69
  • 2
  • 11
  • Take a look at http://msdn.microsoft.com/en-gb/library/windows/desktop/ms740668%28v=vs.85%29.aspx and scroll down to error 10049. This should explain the error – ed_me Feb 22 '13 at 22:45
  • I got that.. But if I'm trying to bind to 0.0.0.0 isn't it suppose to work like that? It's like I'm saying to it that it can bind anywhere? – Mite Stojanov Feb 22 '13 at 22:55
  • If you include more code, it would be more helpful for people to answer. I can't see anything wrong with your snippet of code. – ed_me Feb 22 '13 at 23:09
  • nothing wrong with the code, just not a multicast address - see answer. – isedev Feb 22 '13 at 23:20

1 Answers1

0

From the definition of the IP_ADD_MEMBERSHIP option, the first address is a multicast group address and the second is an interface address.

You are using 127.0.0.1 as the first address. This is not a multicast address.

Multicast addresses are in the range 224.0.0.0/4 (i.e. 224.0.0.1 to 239.255.255.254, not including network and broadcast addresses).

For example, using the first (all hosts on same network segment) multicast address works just fine:

>>> s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>>> s.setsockopt(socket.SOL_IP,socket.IP_ADD_MEMBERSHIP,
                 socket.inet_aton('224.0.0.1')+socket.inet_aton('0.0.0.0'))

Check this reference for more information about multicast addresses.

So you need to choose an unassigned multicast address in 224/4 for your application and use that (e.g. anything in the ad-hoc range, like 244.0.2.0). Note the multicast address has nothing to do with the interface address (using '0.0.0.0', you associate all local interfaces with the multicast address, meaning all interfaces can be used to receive/send multicast packets for that group).

isedev
  • 18,848
  • 3
  • 60
  • 59
  • that explains a lot! thanks.. but, I'm still having problems (i'm starting to think something else is wrong in the code, or I can't get it's 'way' of working) *More code added in original post* – Mite Stojanov Feb 23 '13 at 00:30
  • I edited just know.. and yes, thanks for pointing this out. Your answer is accepted also! – Mite Stojanov Feb 23 '13 at 00:47