1

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.

0

2 Answers 2

2

You need to create a new thread each time you get a new connection

import socket
import thread

host = '127.0.0.1'
port = 5000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket Ready...')

s.bind((host, port))
print('Bind Ready...')

print('Listening...')
s.listen(1)

def handle_client(client_socket):
    while True:
        data = client_socket.recv(1024)
        if not data: break
        print('Client says: ' + data)
        print('Sending: ' + data)
        client_socket.send(data)
    client_socket.close()

while True:
    client_socket, addr = s.accept()
    print('Conexion from: '+str(addr))
    thread.start_new_thread(handle_client ,(client_socket,))
s.close()
Sign up to request clarification or add additional context in comments.

10 Comments

with s.listen(1) ?
if i understood well from the documentation, it's the number of possible connections? I changed the number to 2 and still can't connect more than 1 to the server.
I think i got why it does not connect here 'client_socket, addr = s.accept()' the debugger says that there's no such 's' defined... I though when i invoke Main() it would be declared.
@dsgdfg I could change 's.listen(1)' to increase the backlog but I don't think it's necessary because as soon as a new connection is made it's given it's own thread so it doesn't block the main thread thus allowing the main thread to accept new connections straight away. In fact if I set it to 's.listen(0)' the code still runs fine and accepts many connections.
I should be doing something wrong but i can only make it work with 1 connection.
|
0

Ok, here's the code solved, i should have said i was working on Python3 version. Reading the docs i found out, here's the code and below the docs.

import socket
import os
import threading
from time import sleep
from threading import Thread
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(1)





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__':
Main()
while True:

    client_socket, addr = s.accept()
    os.system('cls')
    print('Conexion desde: '+str(addr))           
    _thread.start_new_thread(handle_client ,(client_socket,))
s.close()

https://docs.python.org/3/library/threading.html http://www.tutorialspoint.com/python3/python_multithreading.htm

The problem was at _thread.start_new_thread(handle_client ,(client_socket,)) just import _thread ask some questions here, keep researching and got it.

Thanks all of you.

WhiteGlove

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.