title: Python multithreading two clients to run indefinitely at the same time with one server
body: <p>I'm attempting to figure out a multithreading problem with two clients running forever at the same time sending pings to the server while the server continuously echos back a message.</p>
  
  <p>I looked into multithreading after I found out how one client/server can echo back and forth forever. </p>
  
  <p>When I added a second client, it would receive the last output of the first client. The second client would then echo repeatedly with the server while the first client never receives a message, freezing it.</p>
  
  <p>I was stuck on this for several days so assistance would be nice.</p>
  
  <p>Anyway, this is the code I been using to test:</p>
  
  <p><strong>ClientA.py</strong></p>
  
  <pre><code>import socket
  import time
  
  host = '127.0.0.1' 
  port = 2004
  BUFFER_SIZE = 2000 
  
  tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
  tcpClientA.connect((host, port))
  
  while True:
      data = 'this is client A'
      tcpClientA.send(data)
      data2 = tcpClientA.recv(BUFFER_SIZE)
      print " Client A received data:", data2
  </code></pre>
  
  <p><strong>Clientb.py</strong> is the same as client A, but with a b</p>
  
  <p><strong>Server.py</strong></p>
  
  <pre><code>import socket
  import time
  import thread
  
  from threading import Thread 
  from SocketServer import ThreadingMixIn 
  
  
  BUFFER_SIZE = 1024
  
  TCP_IP = '127.0.0.1' 
  TCP_PORT = 2004
  tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
  tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
  
  threads = []
  
  def client(ip,port):
      while True:
          data = conn.recv(2048)
          conn.send(data)
      conn.close()
  
  print "Server waiting"
  tcpServer.bind((TCP_IP, TCP_PORT))
  tcpServer.listen(4)
  while True:
      conn,addr=tcpServer.accept()
      thread.start_new_thread(client,(conn,addr))
  tcpServer.close()
  </code></pre>