title: Messaging using Python Socket(Threading)
body: <p>I was working with Socket and threading library but threading does not work as well as i expected. For e.g. when you send a message from server to client message wont be appear, but i you send a message after that from client to server the message(came from server before) will be appear.</p>
  <p>Here is the code for Server side</p>
  <pre><code>import socket
  from threading import Thread as t
  
  port = 8000
  ip = &quot;localhost&quot;  # or your ip
  
  s = socket.socket()
  
  s.bind((ip,port))
  
  s.listen()
  
  c, addr = s.accept()
  print(&quot;Connected&quot;)
  
  def se():
          while True:
                  msg = input(&quot;Your msg: &quot;)
                  c.send(msg.encode())
  
  def re():
          while True:
                  msg = c.recv(1024)
                  print(msg.decode())
  
  t1 = t(target=se)
  t2 = t(target=re)
  
  
  t1.start()
  t2.start()
  </code></pre>
  <p>and here is code for client</p>
  <pre><code>import socket
  from threading import Thread as t
  
  port = 8000
  ip = &quot;localhost&quot;   # or your ip
  
  s = socket.socket()
  s.connect((ip,port))
  print(&quot;Connected&quot;)
  
  def se():
          while True:
                  msg = input(&quot;Your msg: &quot;)
                  s.send(msg.encode())
  
  def re():
          while True:
                  msg = s.recv(1024)
                  print(msg.decode())
  
  t1 = t(target=se)
  t2 = t(target=re)
  
  t1.start()
  t2.start()
  
  </code></pre>
  <p>How can i fix it(i mean i send a message to client and he can see that and when he sends me something i can see that too, with any order and arrangement)</p>
  <p><a href="https://i.stack.imgur.com/va5kS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/va5kS.png" alt="When i send hey from client side it must appear on server side but as you can see that is not happening" /></a></p>