https://wiki.python.org/moin/TcpCommunication works just fine for me, except if the client stops sending data to the server, the server socket script stops running, so you can't restart the client without restarting the server.
I added a second while loop so that the server waits on "s.accept" to accept a new connection but that doesn't work. How kan i keep the server alive? (One connection at a time is sufficient, no need for multiple connections using threads)...
Code: Select all
import socket TCP_IP = '127.0.0.1' TCP_PORT = 5005 BUFFER_SIZE = 20 # Normally 1024, but we want fast response s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((TCP_IP, TCP_PORT)) s.listen(1) while 1: try: conn, addr = s.accept() while 1: data = conn.recv(BUFFER_SIZE) conn.send(data) # echo except: conn.close()