title: what is the use of the header in sockets
body: <p>i am new to coding , i am learning sockets right now so i watched this video on youtube <a href="https://youtu.be/CV7_stUWvBQ" rel="nofollow noreferrer">here</a> and the other 3 videos of the tutorial but still i didn't understand clearly the use of the header why can't we just send it in byte format like what is his role . i made some improvment with the code with threads, here is it (sorry of my english)</p>
  <pre><code>///server side///
  
  import socket
  import select
  import time
  
  HEADER  = 10
  IP = socket.gethostbyname(socket.gethostname())
  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()
  print(&quot;sioud-server is on &quot;)
  socket_list = [server_socket]
  
  clients = {}
  
  def message_recv(client_socket):
  
      try:
          message_header = client_socket.recv(HEADER)
          if not len(message_header) :
              return False
  
          message_length = int(message_header.decode(&quot;utf-8&quot;).strip())
          return {&quot;header&quot; : message_header , &quot;data&quot; : client_socket.recv(message_length) }
  
      except:
          return False
  
  
  while True :
      read_socket , _ , exception_socket = select.select(socket_list , [] , socket_list )
  
      for notified_socket in read_socket :
          if notified_socket == server_socket:
              client_socket , client_address = server_socket.accept()
  
              user = message_recv(client_socket)
              if user is False :
                  continue
  
              socket_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 = message_recv(notified_socket)
              if message is False :
                  print(f&quot;closed connection from {clients[notified_socket]['data'].decode('utf-8')}&quot;)
                  socket_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_socket :
          socket_list.remove(notified_socket)
          del clients[notified_socket]
  </code></pre>
  <p>client-side</p>
  <pre><code>import socket
  import select
  import errno
  import sys
  import time
  import threading
  
  HEADER  = 10
  IP = &quot;your ip adrss&quot;
  PORT= 1234
  
  my_username = input(&quot;Username: &quot;)
  time.sleep(1)
  print(&quot;welcome in sioud-server&quot;)
  print(&quot;note : click 'enter' to see the other users msgs or just send a msg&quot;)
  client_socket = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
  client_socket.connect((IP , PORT))
  
  
  username = my_username.encode(&quot;utf-8&quot;)
  username_header = f&quot;{len(username):&lt;{HEADER}}&quot;.encode(&quot;utf-8&quot;)
  client_socket.send(username_header + username)
  pol = True
  running = True
  def recieve_thread():
      while running :
          username_header = client_socket.recv(HEADER)
          if not len(username_header):
              print('Connection closed by the server')
              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)
          message_length = int(message_header.decode('utf-8').strip())
          message = client_socket.recv(message_length).decode('utf-8')
          time.sleep(1)
          print(f'{username}: {message}')
  
  def send_msg():
  
      while pol:
          message = input(f&quot;/&quot;)
          if message :
              message = message.encode(&quot;utf-8&quot;)
              message_header = f&quot;{len(message):&lt;{HEADER}}&quot;.encode(&quot;utf-8&quot;)
              client_socket.send(message_header + message)
  
  
  thread = threading.Thread(target =recieve_thread)
  thread.start()
  thread_ = threading.Thread(target =send_msg)
  thread_.start()
  </code></pre>