-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsgs_client.py
77 lines (74 loc) · 2.37 KB
/
sgs_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
import socket
import re
import sys
import sgs_server
def receive_command(socket):
command = ""
state = "BEGIN"
data_len = 0
received_len = 0
i = 0
while True:
char = socket.recv(1).decode()
if not char:
break
if state == "BEGIN":
if char != sgs_server.start_mark:
print(f"Warning: unrecognized start of command: {char}")
else:
state = "LENGTH"
len_buff = ""
continue
if state == "LENGTH":
if char.isnumeric():
len_buff += char
elif char == sgs_server.end_len_mark:
data_len = int(len_buff)
state = "COMMAND"
command_buff = ""
else:
print(f"Warning: unexpected char encountered when looking for len: {char}")
continue
if state == "COMMAND":
if char.isalpha():
command_buff += char
elif char == sgs_server.end_mark:
state = "DATA"
data_buff = ""
received_len = 0
if received_len >= data_len:
break
continue
if state == "DATA":
data_buff += char
received_len += 1
if received_len >= data_len:
break
else:
continue
return (command_buff, data_buff)
def start_client(host, port):
client_socket = socket.socket() # instantiate
client_socket.connect((host, port)) # connect to the server
while True:
command, data = receive_command(client_socket)
if not command:
# if data is not received break
break
if command == sgs_server.input_command:
user_input = input(data)
client_socket.send(user_input.encode())
continue
if command == sgs_server.print_command:
print(data)
continue
if command == sgs_server.quit_command:
print("Bye! Server side ends the game.")
break
print(f"Warning: Unrecoginized command: {command}: {data}")
client_socket.close() # close the connection
if __name__ == "__main__":
if len(sys.argv) > 1:
start_client(sys.argv[1], sgs_server.port)
else:
start_client(socket.gethostname(), sgs_server.port)