문제

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!

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top