I am writing a code where an arduino reads data from an accelerometer, passes it to a python script and then that computer running python sends the data to another computer over a socket conneciton. I have written code which can send and recieve one message at a time, slowly, but as soon as I do it bascially as fast as I can (about 100Hz) then the server side only prints the first message, but no more.
Here is the code for my server side:
import socket
class server:
def __init__(self, PORT=9077, MAX_CONNECTIONS=1000, BUFF_SIZE=1024):
self.s = socket.socket()
self.HOST = socket.gethostbyname(socket.gethostname())
self.PORT = PORT
self.MAX_CONNECTIONS = MAX_CONNECTIONS
self.BUFF_SIZE = BUFF_SIZE
self.s.bind((self.HOST, self.PORT))
self.s.listen(self.MAX_CONNECTIONS)
self.recievingData = False
print("Starting a server")
print("IP: " + str(self.HOST))
print("Port: " + str(self.PORT))
def recieveData(self):
self.recievingData = True
while self.recievingData:
print("Waiting for data")
c, addr = self.s.accept()
print(addr)
data = b''
part = c.recv(self.BUFF_SIZE)
data += part
while len(part) > self.BUFF_SIZE:
# print("looping")
part = c.recv(self.BUFF_SIZE)
print(len(part))
data += part
print(data)
c.close()
def stopRecieving(self):
self.revievingData = False
new_server = server()
new_server.recieveData()
and the client side:
class client:
def __init__(self, HOST="192.168.0.51", PORT=9077):
self.s = socket.socket()
self.HOST = HOST
self.PORT = PORT
self.s.connect((self.HOST, self.PORT))
def send_message(self, message):
message = message.encode()
sent = self.s.sendall(message)
print(sent)
and I basically just call send_message every time there is new data to send.
Is there a way to increase the speed at which the server can recieve messages from the same client? Do I need to create multiple threads for recieving data?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…