Home > Software engineering >  A server that handles clients in the background while you can still type in commands
A server that handles clients in the background while you can still type in commands

Time:11-11

I have been trying to make a server where multiple people can connect but i keep getting stuck on one problem. Allowing the server to listen to a client while the server host is still able to type in commands.

running = True

while running:
    command = input('>> ') # Allow for inputs but still connect new users (ex, a minecraft server)

    conn, addr = self.server.accept()
    print(f"[NEW CONNECTION] {conn}:{addr}")

    thread = threading.Thread(target=self.clientThread, args=(conn, addr))
    thread.start()

In short it listens for new connections or allows inputs but it never does both... I know i can do this by running it in different cmd's but it would be way better to keep it all in one place.

CodePudding user response:

you could create a ClientHandler or create two methods. smth like this:

def connecting_users():
    # accept connections
    thread = threading.Thread(target=server_commands,)
    thread.start()
    while True:
          conn, addr = server.accept()
          # do your stuff

def server_commands():
    while True:
          command = input......

Note that this is not tested

  • Related