commit 12b5878f54b4c2273a0d51acebc5667cfb676d79 Author: Rik Veenboer Date: Tue Jun 2 21:10:28 2015 +0200 add example tcp/udp communication using socket diff --git a/socket/TcpClient.py b/socket/TcpClient.py new file mode 100644 index 0000000..0e15194 --- /dev/null +++ b/socket/TcpClient.py @@ -0,0 +1,29 @@ +import socket +import sys + +# Create a TCP/IP socket +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# Connect the socket to the port where the server is listening +server_address = ('localhost', 10000) +print >>sys.stderr, 'connecting to %s port %s' % server_address +sock.connect(server_address) + +try: + # Send data + message = 'This is the message. It will be repeated.' + print >>sys.stderr, 'sending "%s"' % message + sock.sendall(message) + + # Look for the response + amount_received = 0 + amount_expected = len(message) + + while amount_received < amount_expected: + data = sock.recv(16) + amount_received += len(data) + print >>sys.stderr, 'received "%s"' % data + +finally: + print >>sys.stderr, 'closing socket' + sock.close() \ No newline at end of file diff --git a/socket/TcpServer.py b/socket/TcpServer.py new file mode 100644 index 0000000..e6c1536 --- /dev/null +++ b/socket/TcpServer.py @@ -0,0 +1,36 @@ +import socket +import sys + +# Create a TCP/IP socket +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# 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) + +# Listen for incoming connections +sock.listen(1) + +while True: + # Wait for a connection + print >>sys.stderr, 'waiting for a connection' + connection, client_address = sock.accept() + + try: + print >>sys.stderr, 'connection from', client_address + + # Receive the data in small chunks and retransmit it + while True: + data = connection.recv(1024) + print >>sys.stderr, 'received "%s"' % data + if data: + print >>sys.stderr, 'sending data back to the client' + connection.sendall(data) + else: + print >>sys.stderr, 'no more data from', client_address + break + + finally: + # Clean up the connection + connection.close() \ No newline at end of file diff --git a/socket/UdpClient.py b/socket/UdpClient.py new file mode 100644 index 0000000..b205241 --- /dev/null +++ b/socket/UdpClient.py @@ -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() \ No newline at end of file diff --git a/socket/UdpMulticastReceiver.py b/socket/UdpMulticastReceiver.py new file mode 100644 index 0000000..ff8393c --- /dev/null +++ b/socket/UdpMulticastReceiver.py @@ -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) \ No newline at end of file diff --git a/socket/UdpMulticastSender.py b/socket/UdpMulticastSender.py new file mode 100644 index 0000000..a485e25 --- /dev/null +++ b/socket/UdpMulticastSender.py @@ -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() \ No newline at end of file diff --git a/socket/UdpServer.py b/socket/UdpServer.py new file mode 100644 index 0000000..e905542 --- /dev/null +++ b/socket/UdpServer.py @@ -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) \ No newline at end of file