python - Why do I have to restart the tcp_server program once it receives the data? -
Here's the code I'm using in a simple tcp_server in Python: -
Import socket TCP_IP = '127.0.0.1' TCP_PORT = 5005 BUFFER_SIZE = 1024 s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.bind ((TCP_IP, TCP_PORT)) s.listen (1) conn, addr = s .accept () #inputs = [conn, serial_obj] output = [] # read_input.read_input (input, output) while 1: data = conn.recv (BUFFER_SIZE) data then: Brake print "Data received:", data Conn.send conn.close ()
Once my data is received, the connection has been removed and I will To start the campaign the tcp_server program is to run again. I am listening to it forever.
The external loop always keeps running for each incoming connection. The internal loop runs according to the data received on the current connection. Try it:
import socket TCP_IP = '127.0.0.1' TCP_PORT = 5005 BUFFER_SIZE = 1024 s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.bind ((TCP_IP, TCP_PORT) )) S.listen (1) While true: conn, addr = s.accept () #inputs = [conn, serial_obj] outputs = [] # read_input.read_input (input, output) while 1: data = conn.recv (BUFFER_SIZE) data: Brake print "Received Data:", data conn.send (data) conn.close ()
Comments
Post a Comment