Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
67 lines (55 loc) · 1.61 KB
/
Copy pathclient.py
File metadata and controls
67 lines (55 loc) · 1.61 KB
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
import socket
import sys
from threading import Thread
def receive(clientSocket):
"""
Continuosly receive and print messages from the server.
Args:
clientSocket (socket.socket): The client's socket object.
Returns:
None
"""
while True:
try:
data = clientSocket.recv(144)
print(str(data.decode("utf-8")))
except Exception as e:
print("You have been disconnected from The Chat App: ", e)
break
def main():
"""
Entry point for The Chat App client program
Returns:
None
"""
# Get client data from the user
host = input("Host: ")
port = input("Port: ")
if not host:
host = "localhost"
if not port:
port = 3585
else:
port = int(port)
# Attempt connection to the server
try:
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect((host, port))
except Exception as e:
print("Could not make a connection to The Chat App: ", e)
input("Press enter to quit")
sys.exit(0)
# Wait for incoming messages on a different thread
receiveThread = Thread(target = receive, args = (clientSocket, ))
receiveThread.daemon = True
receiveThread.start()
# Transmit user's message to the server
while True:
message = input()
clientSocket.sendall(str.encode(message))
if (message == "{quit}"):
break
clientSocket.close()
sys.exit(0)
if __name__ == "__main__":
main()