Differences between revisions 1 and 3 (spanning 2 versions)
Revision 1 as of 2004-08-04 19:01:50
Size: 1081
Editor: h-68-167-255-80
Comment: How do you subscribe to a multicast address?
Revision 3 as of 2004-11-24 20:10:46
Size: 1537
Editor: rba-cache1-vif1
Comment:
Deletions are marked like this. Additions are marked like this.
Line 23: Line 23:
sock.sendto( MESSAGE, (UDP_IP,_PORT) ) sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )
Line 42: Line 42:
    data, addr = sock.recvfrom( 1024 ) # what is 1024? "buf"..?     data, addr = sock.recvfrom( 1024 ) # buffer size is 1024 bytes
Line 50: Line 50:
   It is the buffer size.[[BR]]
   >>> help (sock.recvfrom)
Line 53: Line 55:

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

UDP Communication

TableOfContents()

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

I have two questions:

  • What is the 1024 in recvfrom?
    • It is the buffer size.BR >>> help (sock.recvfrom)

  • How do you subscribe to a Multicast_address ?

It seems that just setting UDP_IP to "224.0.0.250" (say) isn't quite good enough.

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

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

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