This actually works fine for me, though I had to adjust the port in client.py to match the one in server.py. I also had to add an exception to handle KeyboardInterrupt in server.py so there would be a way to exit the program.
Listening for client . . . Connected to client at ('127.0.0.1', 53944) Message received from client: GET SONAR
Listening for client . . . Connected to client at ('127.0.0.1', 53945) Message received from client: GET SONAR
EDIT:
I took a shot at improving the architecture. I create one connection and use it to pass multiple messages, pausing between each one in the client to wait for the server to send an acknowledgement.
client.py:
import socket
import time
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 8220))
for index in xrange(5):
data = "GET\nSONAR%d\n\n" % index
print 'send to server: ' + data
client_socket.send(data)
while client_socket.recv(2048) != "ack":
print "waiting for ack"
print "ack received!"
#send disconnect message
dmsg = "disconnect"
print "Disconnecting"
client_socket.send(dmsg)
client_socket.close()
server.py:
import socket
import sys
host = 'localhost'
port = 8220
address = (host, port)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)
print "Listening for client . . ."
conn, address = server_socket.accept()
print "Connected to client at ", address
#pick a large output buffer size because i dont necessarily know how big the incoming packet is
while True:
output = conn.recv(2048);
if output.strip() == "disconnect":
conn.close()
sys.exit("Received disconnect message. Shutting down.")
conn.send("dack")
elif output:
print "Message received from client:"
print output
conn.send("ack")