0

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.

  • can you make it reconnect once disconnected ? for example if the bluetooth connections dropped for 5 seconds the code breaks, but it could restart.... – D.L Aug 22 '22 at 08:04
  • not entirely sure what you're asking. but normally, the clients are able to reconnect if it was disconnected. but after sometime has passed, the server and clients socket suddenly just stop communicating with each other, meaning the clients can no longer reconnect to the server, while the server continues to wait for any connections – baeksangyeol Sep 01 '22 at 15:53
  • basically, you can run a check that the system is alive => if the system is alive: good, else: reconnect. – D.L Sep 01 '22 at 16:10
  • i found this article, and i wanted to give it a try: https://stackoverflow.com/questions/31331741/what-does-disableplugins-pnat-do – baeksangyeol Sep 15 '22 at 21:58

0 Answers0