0

Hello all I have create a chat in python with tcp socket but the server accept only one client I tried to search but I have very difficulties to understand how to make a server that accepts multiple clients...can anyone help me?thanks

user3782573
  • 95
  • 1
  • 8

1 Answers1

0

You can take a look at this. Basically, it should be something like (note that the sample handling is just an example, you will have to do something better):

import threading                                                                                                                                                      
import socket   

def print_message(socket):                                                                                                                                            
  data = socket.recv(512)                                                                                                                                             
  print(data)                                                                                                                                                         

def handle_client(clientsocket):                                                                                                                                      
  print("We have a new client")                                                                                                                                         
  thread = threading.Thread(target=print_message, args=(clientsocket,))                                                                                                 
  thread.run()


# create an INET, STREAMing socket                                                                                                                                    
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                                                                                                      
# bind the socket to a public host, and a well-known port                                                                                                             
serversocket.bind((socket.gethostname(), 8080))                                                                                                                       
# become a server socket                                                                                                                                              
serversocket.listen(5)                                                                                                                                                

while True:                                                                                                                                                           
  # accept connections from outside                                                                                                                                   
  (clientsocket, address) = serversocket.accept()                                                                                                                     
  # now do something with the clientsocket                                                                                                                            
  # in this case, we'll pretend this is a threaded server                                                                                                             
  ct = handle_client(clientsocket)                                                                                                                                    
Ivaylo Petrov
  • 1,162
  • 9
  • 18
  • Ehi thanks man the code work fine but the threading is the best/only method?because i read that threading performs poorly in python is right? – user3782573 Jul 18 '14 at 10:20
  • I have read somewhere that some Python implementations don't have good threading performance for CPU-bound tasks. Your case seems like IO-bound task, however, so I think that there is no problem to use threads. Furthermore, you can not achieve multiple clients server, without threads or processes (which I believe won't perform better). The problem is that before you can accept a new client, you need to invoke `serversocket.accept()`, which in single-threaded application will happen after `handle_client` is completed. – Ivaylo Petrov Jul 18 '14 at 15:04