Differences between revisions 1 and 10 (spanning 9 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 10 as of 2005-03-30 23:57:21
Size: 651
Editor: 168-103-146-113
Comment: something to start with
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= UDP Communication = = TCP Communication =
Line 7: Line 7:
Here's simple code to post a note by UDP in Python: (to be written)

== Receiving ==

Here's simple code to serve TCP in Python:
Line 11: Line 15:
#!/usr/bin/env python
Line 13: Line 19:
UDP_IP="127.0.0.1"
UDP_PORT=5005
MESSAGE="Hello, World!"
Line 17: Line 20:
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
Line 21: Line 23:
sock = socket.socket( socket.AF_INET, # Internet
                      socket.SOCK_DGRAM ) # UDP
sock.sendto( MESSAGE, (UDP_IP,_PORT) )
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
    data = conn.recv(10) # buffer size is 10 bytes
    if not data: break
    print "received data:", data
    conn.send(data) # echo
conn.close()
Line 26: Line 37:
== Receiving == ----
Line 28: Line 39:
Here's simple code to receive UDP messages in Python: See also: UdpCommunication
Line 30: Line 41:
{{{
#!python
import socket
= Discussion =
Line 34: Line 43:
UDP_IP="127.0.0.1"
UDP_PORT=5005

sock = socket.socket( socket.AF_INET, # Internet
                      socket.SOCK_DGRAM ) # UDP
sock.bind( (UDP_IP,UDP_PORT) )

while True:
    data, addr = sock.recvfrom( 1024 ) # what is 1024? "buf"..?
    print "received message:", data
}}}

== Discussion ==

I have two questions:
 * What is the 1024 in recvfrom?
 * How do you subscribe to a WikiPedia:Multicast_address ?

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

TCP Communication

TableOfContents()

Sending

(to be written)

Receiving

Here's simple code to serve TCP in Python:

   1 #!/usr/bin/env python
   2 
   3 import socket
   4 
   5 
   6 TCP_IP = '127.0.0.1'
   7 TCP_PORT = 5005
   8 
   9 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  10 s.bind((TCP_IP, TCP_PORT))
  11 s.listen(1)
  12 
  13 conn, addr = s.accept()
  14 print 'Connection address:', addr
  15 while 1:
  16     data = conn.recv(10)  # buffer size is 10 bytes
  17     if not data: break
  18     print "received data:", data
  19     conn.send(data)  # echo
  20 conn.close()


See also: UdpCommunication

Discussion

  • (none yet!)

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

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