Search code examples
socketspython-3.xtcpasyncsocket

Server that accept multiple client


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


Solution

  • 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)