You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to send and receive data simultaneously using bluetooth between client and server sides. The scripts are separate and running on separate devices, on for client and one for server. Initially, the rest of my programs would get blocked in a loop if there was a connection reset or issue with the client or server connection. So I added a single thread in both codes. Now it seems to fix the issue but I am not sure whether it's the correct way to solve this problem.
I have the following codes for client and server sides.
client side:
import bluetooth
import threading
# Set the server's Bluetooth address and port number
bd_addr = "00:e0:4c:9a:f8:64"
port = 1
def client():
while True:
try:
# Create a Bluetooth socket and connect to the server
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr, port))
# Continuously receive data from the server and print it out
while True:
data = sock.recv(1024)
print("Received from server:", data.decode())
# Prompt the user to type something to send back to the server
txt = input("Type something to send back to the server: ")
sock.send(txt.encode())
except Exception as e:
# If an error occurs, print the error message and close the socket
# print(e)
sock.close()
# Start the receive_data and send_data functions in separate threads
threading.Thread(target=client).start()
server side:
import bluetooth
import threading
def server():
while True:
try:
# Create a Bluetooth socket and bind it to the specified port
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port = 1
server_sock.bind(("", port))
server_sock.listen(1)
# Accept an incoming connection from a client
client_sock, address = server_sock.accept()
print("Accepted connection from", address)
# Continuously prompt the user to type something to send to the client
while True:
txt = input("Type something to send to the client: ")
client_sock.send(txt.encode())
# Receive data from the client and print it out
data = client_sock.recv(1024)
print("Received from client:", data.decode())
except Exception as e:
# If an error occurs, print the error message and close the sockets
# print(e)
client_sock.close()
server_sock.close()
# Start the receive_data and send_data functions in separate threads
threading.Thread(target=server).start()
Is using threading this way, to prevent the rest of the program in each script getting stuck, in case of connection error on either side, correct?
Are there any other/better ways to fix the issue, such as callback function?
The text was updated successfully, but these errors were encountered:
System
Issue
I am trying to send and receive data simultaneously using bluetooth between client and server sides. The scripts are separate and running on separate devices, on for client and one for server. Initially, the rest of my programs would get blocked in a loop if there was a connection reset or issue with the client or server connection. So I added a single thread in both codes. Now it seems to fix the issue but I am not sure whether it's the correct way to solve this problem.
I have the following codes for client and server sides.
client side:
server side:
Is using threading this way, to prevent the rest of the program in each script getting stuck, in case of connection error on either side, correct?
Are there any other/better ways to fix the issue, such as callback function?
The text was updated successfully, but these errors were encountered: