I'm learning some Networking through Python and came up with this idea of TCPServer Multithread so i can have multiple clients connected. The problem is that i can only connect one client.
import socket
import os
import threading
from time import sleep
from threading import Thread
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket Preparado...')
def Main():
host = '127.0.0.1'
port = 5000
s.bind((host, port))
print('Enlaze listo...')
print('Escuchando...')
s.listen(5)
c, addr, = s.accept()
os.system('cls')
print('Conexion desde: '+str(addr))
def handle_client(client_socket):
while True:
data = client_socket.recv(1024).decode('utf-8')
if not data: break
print('Client says: ' + data)
print('Sending: ' + data)
client_socket.send(data.encode('utf-8'))
client_socket.close()
if __name__ == '__main__':
while True:
Main()
client_socket, addr = s.accept()
os.system('cls')
print('Conexion desde: '+str(addr))
Thread.start_new_thread(handle_client ,(client_socket,))
s.close()
Edit: This is my actual code, to test it i open up two Client.py codes and try to connect to it. The first Client.py successfully connects (Although there's bugs in receiving and sending back info)The second one executes but it's not shown in the server output as connected or something, it just compiles and stays like that.