title: forking server in python
body: <p>I have used python 3 to write a forking server which will accept many clients (I have not set a limit of number of clients). Each client will be assigned an index value in the server in order of their connection with the server. for example 1st client will be referred as CLIENT 1 then 2nd one is CLIENT 2 and so on. </p>
  
  <p>But the problem is this index is not being assigned to clients correctly. Please help me out for this. I am sending the python code for server and client and also sample output</p>
  
  <p>For the server the code is</p>
  
  <pre><code>import os, socket
  
  host="127.0.0.1"
  port=7000
  s=socket.socket()
  s.bind((host, port))
  s.listen(10)
  
  def handle_client(s, addr, i):
      while True:
          data=s.recv(1024)
          decoded_data=data.decode("utf-8")
          if not decoded_data:
                  print("\nconnection with client " + str(i) + " broken\n")
                  break
          print("  CLIENT " + str(i) + " -&gt; " + decoded_data)
  
  def server():
      i=1
      while i&lt;=10:
          c, addr=s.accept()
          child_pid=os.fork()
          if child_pid==0:
                  print("\nconnection successful with client " + str(i) + str(addr) + "\n")
                  handle_client(c, addr, i)
          else:
                  i+=1
  
  server()
  </code></pre>
  
  <p>For the client the code is</p>
  
  <pre><code>import socket
  
  def client():
      host="127.0.0.1"
      port=7000
      s=socket.socket()
      s.connect((host, port))
      msg=str(input("\n -&gt; "))
      encoded_msg=bytes(msg, "utf-8")
      while msg!='q':
              s.send(encoded_msg)
              msg=str(input("\n -&gt; "))
              encoded_msg=bytes(msg, "utf-8")
  
  client()
  </code></pre>
  
  <p>Now I open 2 terminals and run the server in one and in the other terminal I run clients one by one</p>
  
  <p>The terminal in which I run the clients is like this</p>
  
  <pre><code>debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py
  
   -&gt; hii
  
   -&gt; q
  debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py
  
   -&gt; hello
  
   -&gt; q
  debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py
  
   -&gt; hii how are you???
  
   -&gt; q
  debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_client.py
  
   -&gt; good day
  
   -&gt; q
  debesh@laptop:~/Documents/programs/python/network$ 
  </code></pre>
  
  <p>The terminal in which the server is running is like this</p>
  
  <pre><code>debesh@laptop:~/Documents/programs/python/network$ python3 forking_chat_server.py
  
  connection successful with client 1('127.0.0.1', 50362)
  
    CLIENT 1 -&gt; hii
  
  connection with client 1 broken
  
  
  connection successful with client 2('127.0.0.1', 50363)
  
    CLIENT 2 -&gt; hello
  
  connection with client 2 broken
  
  
  connection successful with client 1('127.0.0.1', 50364)
  
    CLIENT 1 -&gt; hii how are you???
  
  connection with client 1 broken
  
  
  connection successful with client 3('127.0.0.1', 50365)
  
    CLIENT 3 -&gt; good day
  
  connection with client 3 broken
  </code></pre>
  
  <p>As you can see in the server terminal the index 1 is assigned to two clients-the 1st one and the 3rd one. How does this happen? Whats the fault in my code? Please help me</p>