Size: 1081
Comment: How do you subscribe to a multicast address?
|
Size: 651
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
Sending
(to be written)
Receiving
Here's simple code to serve TCP in Python:
Toggle line numbers
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!)