-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
92 lines (82 loc) · 3.67 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# + set username
# + provide functionality to switch contact to chat with, quit and send messages
import time
import socket
import threading
class ClientClass:
TCP_IP = socket.gethostbyname(socket.gethostname())
TCP_PORT = 1234
HEADERSIZE = 20
USERNAME = ""
def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((ClientClass.TCP_IP, ClientClass.TCP_PORT))
self.clientLock = threading.Lock()
print("---- Client active ----\n")
def main(self):
print("Connection from client: ", socket.gethostbyname(socket.gethostname()), " to server: ", ClientClass.TCP_IP, "\n")
t0 = threading.Thread(target=self.send, args=("{name}",))
t0.start()
t1 = threading.Thread(target=self.recv)
t1.daemon = True
t1.start()
t2 = threading.Thread(target=self.send, args=("{switch}",))
t2.start()
while True:
if not t2.isAlive():
displayText = f"<{ClientClass.USERNAME}> ({{switch}}/{{quit}})"
msgOrTypeOfMsg = self.entry(displayText)
t3 = threading.Thread(target=self.send, args=(msgOrTypeOfMsg,))
t3.start()
else:
time.sleep(2)
pass
def entry(self, displayText):
while True:
rawMsg = input(displayText)
if not (rawMsg and not rawMsg.isspace()):
pass
else:
return rawMsg
def send(self, msgOrTypeOfMsg=None):
with self.clientLock:
if msgOrTypeOfMsg == "{switch}":
switchPrefix = msgOrTypeOfMsg + str(len(msgOrTypeOfMsg.encode("utf-8")))
msg = f"{switchPrefix:<{ClientClass.HEADERSIZE}}" + msgOrTypeOfMsg
self.sock.send(bytes(msg, "utf-8"))
displayText = "Send to?: "
destination = self.entry(displayText)
switchPrefix = msgOrTypeOfMsg + str(len(destination.encode("utf-8")))
msg = f"{switchPrefix:<{ClientClass.HEADERSIZE}}" + destination
self.sock.send(bytes(msg, "utf-8"))
elif msgOrTypeOfMsg == "{quit}":
print(msgOrTypeOfMsg, "//quit")
elif msgOrTypeOfMsg == "{name}":
displayText = "Username ? "
ClientClass.USERNAME = self.entry(displayText)
print(f"--> You chose {ClientClass.USERNAME}\n")
userNamePrefix = msgOrTypeOfMsg + str(len(ClientClass.USERNAME.encode("utf-8")))
msg = f"{userNamePrefix:<{ClientClass.HEADERSIZE}}" + ClientClass.USERNAME
self.sock.send(bytes(msg, "utf-8"))
else:
msg = f"{str(len(msgOrTypeOfMsg.encode('utf-8'))):<{ClientClass.HEADERSIZE}}" + msgOrTypeOfMsg
self.sock.send(bytes(msg, "utf-8"))
def recv(self):
try:
while True:
fullServerMsg = bytearray()
newServerMsg = True
while True:
msg = self.sock.recv(32)
if newServerMsg:
msgLen = int(msg[: ClientClass.HEADERSIZE])
newServerMsg = False
fullServerMsg += msg
if len(fullServerMsg) - ClientClass.HEADERSIZE == msgLen:
fullServerMsg = fullServerMsg.decode("utf-8")
print(f"\n{fullServerMsg[ClientClass.HEADERSIZE :]}")
break
except ConnectionResetError as e:
print("Server closed the connection\n", "OS-Error:", e, "\nApplication quitted")
clientObject = ClientClass()
clientObject.main()