once a client reconnects, i cant take input and send. anyone knows what i am doing wrong?
import socket
import threading
import time
def receiveData(conn):
while True:
try:
data = conn.recv(1024)
if not data:
break
decodedData = data.decode()
print(f"Received data: {decodedData}")
except ConnectionResetError:
print("connected reset by client.")
print("waiting for new client")
break
except ConnectionAbortedError:
print("connection aborted.")
break
except Exception as e:
print(f"Error receiving data: {str(e)}")
break
def sendData(conn):
while True:
try:
if conn:
userInput = input("what do you want to send? ").strip()
conn.sendall(userInput.encode())
time.sleep(3)
except Exception as e:
print(f"Error sending data: {str(e)}")
print("will try to ask for new input again")
continue
except KeyboardInterrupt:
print("dont press ctrl.")
continue
def ClientHandle(server):
while True:
conn, addr = server.accept()
print(f"New client connected: {addr}")
t_receive = threading.Thread(target=receiveData, args=(conn,))
t_send = threading.Thread(target=sendData, args=(conn,))
t_receive.start()
t_send.start()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.0.148"
port = 6666
server.bind((host, port))
server.listen()
t_new_client = threading.Thread(target=ClientHandle, args=(server,))
t_new_client.start()
t_new_client.join()