add example tcp/udp communication using socket
This commit is contained in:
29
socket/TcpClient.py
Normal file
29
socket/TcpClient.py
Normal file
@@ -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()
|
||||
36
socket/TcpServer.py
Normal file
36
socket/TcpServer.py
Normal file
@@ -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()
|
||||
23
socket/UdpClient.py
Normal file
23
socket/UdpClient.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()
|
||||
29
socket/UdpMulticastReceiver.py
Normal file
29
socket/UdpMulticastReceiver.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
socket/UdpMulticastSender.py
Normal file
39
socket/UdpMulticastSender.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()
|
||||
21
socket/UdpServer.py
Normal file
21
socket/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