-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChatClient.py
47 lines (37 loc) · 828 Bytes
/
ChatClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
import socket
import threading
import time
tLock = threading.Lock()
shutdown = False
def receving(name,sock):
while not shutdown:
try:
tLock.acquire()
while True:
data,addr = sock.recvfrom(1024)
print str(data)
except:
pass
finally:
tLock.release()
host = "127.0.0.1"
port = 0
server = ("127.0.0.1",5000)
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind((host,port))
s.setblocking(0)
rT = threading.Thread(target=receving,args=("RecvThread",s))
rT.start()
alias = raw_input("Name: ")
message = raw_input(alias + "->")
while message != 'q':
if message != '':
s.sendto(alias + ": " + message,server)
tLock.acquire()
message = raw_input(alias + "->")
tLock.release()
time.sleep(0.2)
shutdown = True
rT.join()
s.close()