Differences between revisions 14 and 16 (spanning 2 versions)
Revision 14 as of 2005-04-14 12:20:36
Size: 4175
Editor: mail
Comment:
Revision 16 as of 2005-05-09 19:44:47
Size: 5367
Editor: 062016177244
Comment:
Deletions are marked like this. Additions are marked like this.
Line 60: Line 60:
Here's my own, non-functioning, effort: (I've replaced it with one that works. -- Asgeir S. Nilsen
[[DateTime(2005-05-09T19:25:00Z)]])
Line 67: Line 69:
#sock.bind(('127.0.0.1', 1000))
sock.bind(('', 1000))
#grpaddr = 0
#for byte in "239.192.0.100".split("."):
# grpaddr = (grpaddr << 8) | int(byte)
#mreq = struct.pack('ll', socket.htonl(grpaddr),
# socket.htonl(socket.INADDR_ANY))
mreq = struct.pack("sl", socket.inet_aton("239.192.0.100"), socket.INADDR_ANY)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 4242))
mreq = struct.pack("sl", socket.inet_aton("224.51.105.104"), socket.INADDR_ANY)
Line 76: Line 73:
print sock.recvfrom(100)
while True:
  
print sock.recv(10240)
Line 90: Line 89:
(You might want to reconsider the IP_MULTICAST_TTL setting here -- the recommended value for local-network multicasts is < 32, whilst a value > 32 indicates multicasts which should traverse onto the Internet -- Asgeir S. Nilsen)
Line 96: Line 96:

  ''The new version still doesn't work for me. I mean, it ''does'' work for local traffic: the host talking with itself. But as soon as I get to another link-local computer, and do the same over again, it doesn't work. I've replaced 127.0.0.1 with the actual IP address of the computer, and I've shut down the firewall (service iptables stop). No dice. This is all on my FC3 host, again.'' -- LionKimbro [[DateTime(2005-04-14T17:27:42Z)]]

  ''When implementing multicast, it's important to understand the requirements of [ftp://ftp.rfc-editor.org/in-notes/rfc3376.txt IGMP], especially when working in a switched network. IGMP describes how routers should exchange membership information, but does not describe how layer 2 switches should handle this. Many switches have a feature called IGMP snooping, where the switch snoops for IGMP traffic, thereby gaining knowledge of which switch ports belong to a multicast group. Cheap switches typically either does not handle this or handles it wrongly.'' -- Asgeir S. Nilsen [[DateTime(2005-05-09T19:39:00Z)]]

UDP Communication

TableOfContents()

See also SoapOverUdp, TcpCommunication

Sending

Here's simple code to post a note by UDP in Python:

   1 import socket
   2 
   3 UDP_IP="127.0.0.1"
   4 UDP_PORT=5005
   5 MESSAGE="Hello, World!"
   6 
   7 print "UDP target IP:", UDP_IP
   8 print "UDP target port:", UDP_PORT
   9 print "message:", MESSAGE
  10 
  11 sock = socket.socket( socket.AF_INET, # Internet
  12                       socket.SOCK_DGRAM ) # UDP
  13 sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )

Receiving

Here's simple code to receive UDP messages in Python:

   1 import socket
   2 
   3 UDP_IP="127.0.0.1"
   4 UDP_PORT=5005
   5 
   6 sock = socket.socket( socket.AF_INET, # Internet
   7                       socket.SOCK_DGRAM ) # UDP
   8 sock.bind( (UDP_IP,UDP_PORT) )
   9 
  10 while True:
  11     data, addr = sock.recvfrom( 1024 ) # buffer size is 1024 bytes
  12     print "received message:", data

Discussion

* It would seem easy to extend this to a simple means to open a file on the sender side, send datagrams to the receiver side, and write those packets to a file there - I just wonder about synchronisation issues regarding the buffer...Anyone smart care to put something down, say as a simple practical extension of what is already here? (And if you do it pls delete this message) *

Multicasting?

I've been googling for some time now, and still have yet to find a working example of Python multicast listening.

(The example below has been updated to work -- Steven Spencer DateTime(2005-04-14T13:19:00Z))

(I've replaced it with one that works. -- Asgeir S. Nilsen DateTime(2005-05-09T19:25:00Z))

   1 import socket
   2 import struct
   3 
   4 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
   5 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
   6 sock.bind(('', 4242))
   7 mreq = struct.pack("sl", socket.inet_aton("224.51.105.104"), socket.INADDR_ANY)
   8 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
   9 
  10 while True:
  11   print sock.recv(10240)

The mreq packing is based on [http://www.senux.com/linux/network/multicast/ some code that I found,] that does not work. On my computer, at least.

Sending to multicast groups is just fine; Here's some functional text:

   1 import socket
   2 
   3 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
   4 sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
   5 sock.sendto("robot", ("239.192.0.100", 1000))

(You might want to reconsider the IP_MULTICAST_TTL setting here -- the recommended value for local-network multicasts is < 32, whilst a value > 32 indicates multicasts which should traverse onto the Internet -- Asgeir S. Nilsen)

At this point, I'm beginning to think: "Python multicast simply does not work."

  • Are you running on Windows 2000/XP (pre-SP2)/Server 2003 with more than one network adapter? If so, the problem is Windows, not Python. The original code works for me on Windows 2000 (1 network adapter), but fails under XP Pro (pre-SP2, 3 adapters though 2 are disabled). Microsoft has a [http://support.microsoft.com/default.aspx?scid=kb;en-us;827536 support page] on the issue. The problem appears to be in the receiver: with both machines running the receiver, the Win2K machine sees packets sent from both machines, while the receiver on XP sees messages sent from the Win2K machine only. This, despite specifying the local IP address of the appropriate adapter in the second part of the mreq structure in the IP_ADD_MEMBERSHIP call. -- VinaySajip

    Hm, that's interesting. No, I'm not running on Windows; I'm running on FC3. That said, I hadn't considered the machine as a possible problem. What I'll do is this: I'll run this on my home FC3 computer, and on my home Redhat 9 computer, and see if I can get it to work on one of them. -- LionKimbro DateTime(2005-01-20T02:07:18Z)

    The new version still doesn't work for me. I mean, it does work for local traffic: the host talking with itself. But as soon as I get to another link-local computer, and do the same over again, it doesn't work. I've replaced 127.0.0.1 with the actual IP address of the computer, and I've shut down the firewall (service iptables stop). No dice. This is all on my FC3 host, again. -- LionKimbro DateTime(2005-04-14T17:27:42Z)

    When implementing multicast, it's important to understand the requirements of [ftp://ftp.rfc-editor.org/in-notes/rfc3376.txt IGMP], especially when working in a switched network. IGMP describes how routers should exchange membership information, but does not describe how layer 2 switches should handle this. Many switches have a feature called IGMP snooping, where the switch snoops for IGMP traffic, thereby gaining knowledge of which switch ports belong to a multicast group. Cheap switches typically either does not handle this or handles it wrongly. -- Asgeir S. Nilsen DateTime(2005-05-09T19:39:00Z)

It's too bad we don't have anything as simple as this:

   1 import UDP
   2 
   3 sock = UDP.MulticastListener("239.192.0.100", 1000)  # Listen on port 1000
   4 print sock.recv(100)

   1 import UDP
   2 
   3 UDP.send("Hello, world!", "239.192.0.100", 1000)

...or something like that.

-- LionKimbro DateTime(2005-01-19T19:54:19Z)

UdpCommunication (last edited 2020-05-19 21:27:34 by JonathanVirga)

Unable to edit the page? See the FrontPage for instructions.