move towards single implementation for socket client/server using asyncore and threads
This commit is contained in:
29
datagram/UdpMulticastClient.py
Normal file
29
datagram/UdpMulticastClient.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
|
||||
multicast_group = '224.3.29.71'
|
||||
server_address = ('', 10000)
|
||||
|
||||
# Create the socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
# Bind to the server address
|
||||
sock.bind(server_address)
|
||||
|
||||
# Tell the operating system to add the socket to the multicast group
|
||||
# on all interfaces.
|
||||
group = socket.inet_aton(multicast_group)
|
||||
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
|
||||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
|
||||
|
||||
# Receive/respond loop
|
||||
while True:
|
||||
print >>sys.stderr, '\nwaiting to receive message'
|
||||
data, address = sock.recvfrom(1024)
|
||||
|
||||
print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
|
||||
print >>sys.stderr, data
|
||||
|
||||
print >>sys.stderr, 'sending acknowledgement to', address
|
||||
sock.sendto('ack', address)
|
||||
39
datagram/UdpMulticastServer.py
Normal file
39
datagram/UdpMulticastServer.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
|
||||
message = 'very important data'
|
||||
multicast_group = ('224.3.29.71', 10000)
|
||||
|
||||
# Create the datagram socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
# Set a timeout so the socket does not block indefinitely when trying
|
||||
# to receive data.
|
||||
sock.settimeout(0.2)
|
||||
|
||||
# Set the time-to-live for messages to 1 so they do not go past the
|
||||
# local network segment.
|
||||
ttl = struct.pack('b', 1)
|
||||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
|
||||
|
||||
try:
|
||||
|
||||
# Send data to the multicast group
|
||||
print >>sys.stderr, 'sending "%s"' % message
|
||||
sent = sock.sendto(message, multicast_group)
|
||||
|
||||
# Look for responses from all recipients
|
||||
while True:
|
||||
print >>sys.stderr, 'waiting to receive'
|
||||
try:
|
||||
data, server = sock.recvfrom(16)
|
||||
except socket.timeout:
|
||||
print >>sys.stderr, 'timed out, no more responses'
|
||||
break
|
||||
else:
|
||||
print >>sys.stderr, 'received "%s" from %s' % (data, server)
|
||||
|
||||
finally:
|
||||
print >>sys.stderr, 'closing socket'
|
||||
sock.close()
|
||||
23
datagram/UdpSender.py
Normal file
23
datagram/UdpSender.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import socket
|
||||
import sys
|
||||
|
||||
# Create a UDP socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
server_address = ('localhost', 10000)
|
||||
message = 'This is the message. It will be repeated.'
|
||||
|
||||
try:
|
||||
|
||||
# Send data
|
||||
print >>sys.stderr, 'sending "%s"' % message
|
||||
sent = sock.sendto(message, server_address)
|
||||
|
||||
# Receive response
|
||||
print >>sys.stderr, 'waiting to receive'
|
||||
data, server = sock.recvfrom(4096)
|
||||
print >>sys.stderr, 'received "%s"' % data
|
||||
|
||||
finally:
|
||||
print >>sys.stderr, 'closing socket'
|
||||
sock.close()
|
||||
21
datagram/UdpServer.py
Normal file
21
datagram/UdpServer.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import socket
|
||||
import sys
|
||||
|
||||
# Create a TCP/IP socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
# Bind the socket to the port
|
||||
server_address = ('localhost', 10000)
|
||||
print >>sys.stderr, 'starting up on %s port %s' % server_address
|
||||
sock.bind(server_address)
|
||||
|
||||
while True:
|
||||
print >>sys.stderr, '\nwaiting to receive message'
|
||||
data, address = sock.recvfrom(4096)
|
||||
|
||||
print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
|
||||
print >>sys.stderr, data
|
||||
|
||||
if data:
|
||||
sent = sock.sendto(data, address)
|
||||
print >>sys.stderr, 'sent %s bytes back to %s' % (sent, address)
|
||||
Reference in New Issue
Block a user