title: Sockets Python | chatting server multiple clients
body: <p>i am trying to implement a TLS secured socket server in python, and i want multiple clients to connect to the server and talk to each other,</p>
  <p>what i would like to do is that each client connecting can receive messages sent by other clients also,</p>
  <p><strong>Server code:</strong></p>
  <pre><code>import socket
  import threading
  
  HEADER = 64
  PORT = 5050
  SERVER = socket.gethostbyname(socket.gethostname())
  ADDR = (SERVER, PORT)
  FORMAT = 'utf-8'
  DISCONNECT_MESSAGE = &quot;!DISCONNECT&quot;
  
  server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server.bind(ADDR)
  
  
  def handle_client(conn, addr):
      print(f&quot;[NEW CONNECTION] {addr} connected.&quot;)
  
      connected = True
      while connected:
          msg_length = conn.recv(HEADER).decode(FORMAT)
          if msg_length:
              msg_length = int(msg_length)
              msg = conn.recv(msg_length).decode(FORMAT)
              if msg == DISCONNECT_MESSAGE:
                  connected = False
  
              print(f&quot;[{addr}] {msg}&quot;)
              conn.send(&quot;Msg received&quot;.encode(FORMAT))
  
      conn.close()
  
  
  def start():
      server.listen()
      print(f&quot;[LISTENING] Server is listening on {SERVER}&quot;)
      while True:
          conn, addr = server.accept()
          thread = threading.Thread(target=handle_client, args=(conn, addr))
          thread.start()
          print(f&quot;[ACTIVE CONNECTIONS] {threading.activeCount() - 1}&quot;)
  
  
  print(&quot;[STARTING] server is starting ...&quot;)
  start()
  </code></pre>
  <p><strong>Client Code:</strong></p>
  <pre><code>import socket
  
  HEADER = 64
  PORT = 5050
  FORMAT = 'utf-8'
  DISCONNECT_MESSAGE = &quot;!DISCONNECT&quot;
  SERVER = &quot;192.168.1.141&quot;
  ADDR = (SERVER, PORT)
  
  client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  client.connect(ADDR)
  
  
  def send(msg):
      message = msg.encode(FORMAT)
      msg_length = len(message)
      send_length = str(msg_length).encode(FORMAT)
      send_length += b' ' * (HEADER - len(send_length))
      client.send(send_length)
      client.send(message)
      print(client.recv(2048).decode(FORMAT))
  
  
  send(input('[Send a Message]:  '))
  
  
  send(DISCONNECT_MESSAGE)
  </code></pre>
  <p>where can i add the TLS in this?:/
  if something isn't right please inform me :)</p>
  <p>---------------------EDIT------------------------------</p>
  <p>SERVER:</p>
  <pre><code>import socket
  import select
  
  HEADER_LENGTH = 10
  IP = &quot;127.0.0.1&quot;
  PORT = 1234
  
  server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  server_socket.bind((IP, PORT))
  server_socket.listen(5)
  sockets_list = [server_socket]
  clients = {}
  
  
  def receive_message(client_socket):
      try:
          message_header = client_socket.recv(HEADER_LENGTH)
          if not len(message_header):
              return False
          message_length = int(message_header.decode('utf-8').strip())
          return {&quot;header&quot;: message_header, &quot;data&quot;: client_socket.recv(message_length)}
      except:
          return False
  
  
  while True:
      read_sockets, _, exception_sockets = select.select(
          sockets_list, [], sockets_list)
  
      for notified_socket in read_sockets:
          if notified_socket == server_socket:
              client_socket, client_address = server_socket.accept()
  
              user = receive_message(client_socket)
              if user is False:
                  continue
              sockets_list.append(client_socket)
              clients[client_socket] = user
  
              print(
                  f&quot;Accepted new connection from {client_address[0]}:{client_address[1]} username: {user['data'].decode('utf-8')}&quot;)
          else:
              message = receive_message(notified_socket)
              if message is False:
                  print(
                      f&quot;closed connection from {clients[notified_socket]['data'].decode('utf-8')}&quot;)
                  sockets_list.remove(notified_socket)
                  del clients[notified_socket]
                  continue
  
              user = clients[notified_socket]
  
              print(
                  f&quot;Received message from {user['data'].decode('utf-8')}: {message['data'].decode('utf-8')}&quot;)
  
              for client_socket in clients:
                  if client_socket != notified_socket:
                      client_socket.send(
                          user['header'] + user['data'] + message['header'] + message['data'])
  
      for notified_socket in exception_sockets:
          sockets_list.remove(notified_socket)
          del clients[notified_socket]
  </code></pre>
  <p>CLIENT:</p>
  <pre><code>from http import client
  import socket
  import select
  import errno
  import sys
  import threading
  
  
  HEADER_LENGTH = 10
  
  
  IP = '127.0.0.1'
  PORT = 1234
  
  my_username = input(&quot;Username: &quot;)
  client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  client_socket.connect((IP, PORT))
  client_socket.setblocking(False)
  
  username = my_username.encode('utf-8')
  username_header = f&quot;{len(username):&lt;{HEADER_LENGTH}}&quot;.encode('utf-8')
  client_socket.send(username_header + username)
  
  
  def send_message():
      message = input(f&quot;{my_username} &gt;  &quot;)
  
      if message:
          message = message.encode('utf-8')
          message_header = f&quot;{len(message):&lt;{HEADER_LENGTH}}&quot;.encode('utf-8')
          client_socket.send(message_header + message)
  
  
  while True:
      thread = threading.Thread(target=send_message)
      thread.start()
  
      try:
          while True:
              # receive things
              username_header = client_socket.recv(HEADER_LENGTH)
              if not len(username_header):
                  print(&quot;connection closed by the server&quot;)
                  sys.exit()
              username_length = int(username_header.decode('utf-8').strip())
              username = client_socket.recv(username_length).decode('utf-8')
  
              message_header = client_socket.recv(HEADER_LENGTH)
              message_length = int(message_header.decode('utf-8').strip())
              message = client_socket.recv(message_length).decode('utf-8')
  
              print(f&quot;{username} &gt; {message}&quot;)
  
      except IOError as e:
          if e.errno != errno.EAGAIN and e.errno != errno.EWOULDBLOCK:
              print('Reading error', str(e))
              sys.exit()
          continue
  
      except Exception as e:
          print('General error', str(e))
          sys.exit()
  
  </code></pre>
  <p>unfortunately, i cant seem to get the buffer to work right , other clients only receive other clients input when they send messages, so the output is always unordered correctly.</p>