I am trying to do a server-client socketing program using PyBluez, where data are transferred between server and clients via Bluetooth connection.
For this, I used 2 RPi 4s as clients, and a mini PC running Ubuntu 22.04 acting as server. I ran the devices, and the data transfers seem to be working fine. However, after a few hours, the data transfers just inexplicably stops.
I checked if the Bluetooth connections are still intact, and it said the Bluetooth connections are fine.
Snippets of server and client Python scripts:
- server
# thread for each client connection
def handle_client(connection):
while True:
try:
data = connection.recv(1024).decode('utf-8')
if not data:
break
reply = f'Received from {str(connection)}'
connection.sendall(reply)
except Exception as e:
print(str(e))
connection.close()
connection.close()
...
- client:
...
while True:
try:
data = MacAddr #MacAddr obtained using subprocess
conn.sendall(data + '\n')
res = conn.recv(1024).decode('utf-8')
except Exception as e:
# attempt to reconnect to server
...
...
I have kept logs using logger (not seen in the code snippets) to ensure programs are running properly (receiving and sending data) and left them running continuously. However, after a few hours, the programs continue to run, but stop logging, meaning that the sockets must be malfunctioning.
Based on the logs, the problems didn't seem to be linked to buffer overflow or CPU/memory. I tried stopping and restarting the programs, but the problem persisted. The only temporary fix was to power-cycle the server PC.
I want the server-client socket program to be able to run indefinitely, but I cannot figure out the problem beyond this.
Help is appreciated.