title: Creating a simple Chat application in Python (Sockets)
body: <p>I'm trying to create a simple chat application using sockets (python). Where a client can send a message to server and server simply broadcast the message to all other clients except the one who has sent it.</p>
  
  <p>Client has two threads, which are running forever</p>
  
  <blockquote>
    <p><code>send</code>: Send simply sends the cleints message to server.</p>
    
    <p><code>receive</code>: Receive the message from the server.</p>
  </blockquote>
  
  <p>Server also has two threads,  which are running forever</p>
  
  <blockquote>
    <p><code>accept_cleint</code>: To accept the incoming connection from the client.</p>
    
    <p><code>broadcast_usr</code>: Accepts the message from the client and just broadcast it to all other clients.</p>
  </blockquote>
  
  <p>But I'm getting erroneous output (Please refer the below image). All threads suppose to be active all the times but Some times client can send message sometimes it can not. Say for example Tracey sends 'hi' 4 times but its not broadcasted, When John says 'bye' 2 times then 1 time its message gets braodcasted. It seems like there is some <code>thread synchronization</code> problem at sever, I'm not sure. Please tell me what's wrong. </p>
  
  <p><a href="https://i.stack.imgur.com/XO6E4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XO6E4.png" alt="enter image description here"></a></p>
  
  <p>Below is the code. </p>
  
  <p>chat_client.py</p>
  
  <pre><code>import socket, threading
  
  def send():
      while True:
          msg = raw_input('\nMe &gt; ')
          cli_sock.send(msg)
  
  def receive():
      while True:
          sen_name = cli_sock.recv(1024)
          data = cli_sock.recv(1024)
  
          print('\n' + str(sen_name) + ' &gt; ' + str(data))
  
  if __name__ == "__main__":   
      # socket
      cli_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
      # connect
      HOST = 'localhost'
      PORT = 5023
      cli_sock.connect((HOST, PORT))     
      print('Connected to remote host...')
      uname = raw_input('Enter your name to enter the chat &gt; ')
      cli_sock.send(uname)
  
      thread_send = threading.Thread(target = send)
      thread_send.start()
  
      thread_receive = threading.Thread(target = receive)
      thread_receive.start()
  </code></pre>
  
  <p>chat_server.py</p>
  
  <pre><code>import socket, threading
  
  def accept_client():
      while True:
          #accept    
          cli_sock, cli_add = ser_sock.accept()
          uname = cli_sock.recv(1024)
          CONNECTION_LIST.append((uname, cli_sock))
          print('%s is now connected' %uname)
  
  def broadcast_usr():
      while True:
          for i in range(len(CONNECTION_LIST)):
              try:
                  data = CONNECTION_LIST[i][1].recv(1024)
                  if data:
                      b_usr(CONNECTION_LIST[i][1], CONNECTION_LIST[i][0], data)
              except Exception as x:
                  print(x.message)
                  break
  
  def b_usr(cs_sock, sen_name, msg):
      for i in range(len(CONNECTION_LIST)):
          if (CONNECTION_LIST[i][1] != cs_sock):
              CONNECTION_LIST[i][1].send(sen_name)
              CONNECTION_LIST[i][1].send(msg)
  
  if __name__ == "__main__":    
      CONNECTION_LIST = []
  
      # socket
      ser_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
      # bind
      HOST = 'localhost'
      PORT = 5023
      ser_sock.bind((HOST, PORT))
  
      # listen    
      ser_sock.listen(1)
      print('Chat server started on port : ' + str(PORT))
  
      thread_ac = threading.Thread(target = accept_client)
      thread_ac.start()
  
      thread_bs = threading.Thread(target = broadcast_usr)
      thread_bs.start()
  </code></pre>