-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.py
52 lines (43 loc) · 1.27 KB
/
Client.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
48
49
50
51
52
import socket
import threading
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 5963))
sock.send(b'1')
print(sock.recv(1024).decode())
print(sock.recv(1024).decode())
nickName = input('input your nickname: ')
sock.send(nickName.encode())
def sendThreadFunc():
while True:
try:
myWord = input()
sock.send(myWord.encode())
# print(sock.recv(1024).decode())
except ConnectionAbortedError:
print('Server closed this connection!')
break
except ConnectionResetError:
print('Server is closed!')
break
def recvThreadFunc():
while True:
try:
otherWord = sock.recv(1024)
if otherWord == "disconnect" or not otherWord.strip():
sock.close()
break
else:
print(otherWord.decode())
except ConnectionAbortedError:
print('Server closed this connection!')
break
except ConnectionResetError:
print('Server is closed!')
break
th1 = threading.Thread(target=sendThreadFunc)
th2 = threading.Thread(target=recvThreadFunc)
threads = [th1, th2]
for t in threads:
t.setDaemon(True)
t.start()
t.join()