Advertisement
j7sx

client

Sep 15th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import socket
  2. import threading
  3. import time
  4.  
  5. tLock = threading.Lock()
  6. shutdown = False
  7.  
  8. def receving(name, sock):
  9.     while not shutdown:
  10.         try:
  11.             tLock.acquire()
  12.             while True:
  13.                 data, addr = sock.recvfrom(1024)
  14.                 print (str(data))
  15.         except:
  16.             pass
  17.         finally:
  18.             tLock.release()
  19.  
  20. host = '127.0.0.1'
  21. port = 0
  22.  
  23. server = ('127.0.0.1',5000)
  24.  
  25. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  26. s.bind((host, port))
  27. s.setblocking(0)
  28.  
  29. rT = threading.Thread(target=receving, args=("RecvThread",s))
  30. rT.start()
  31.  
  32. alias = input("Name: ")
  33. message = input(alias + "-> ")
  34. while message != 'q':
  35.     if message != '':
  36.         s.sendto(alias + ": " + message, server)
  37.     tLock.acquire()
  38.     message = input(alias + "-> ")
  39.     tLock.release()
  40.     time.sleep(0.2)
  41.  
  42. shudown = True
  43. rT.join()
  44. s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement