-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.py
56 lines (44 loc) · 1.68 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
import socket
import select
import errno
import sys
HEADER_LENGTH = 10
Host = "127.0.0.1"
Port = 8080
my_username = input("Username: ")
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#socket.socket([family], [type] , [proto] )
# AF_INET:Server與Server間的串接
# SOCK_STREAM:用TCP方式
client_socket.connect((Host, Port))
client_socket.setblocking(False)
# 取消socket的阻塞狀態
username = my_username.encode('utf-8')
username_header = f"{len(username):<{HEADER_LENGTH}}".encode('utf-8')
client_socket.send(username_header + username)
while True:
message = input(f'{my_username} > ')
if message:
message = message.encode('utf-8')
message_header = f"{len(message):<{HEADER_LENGTH}}".encode('utf-8')
client_socket.send(message_header + message)
try:
while True:
username_header = client_socket.recv(HEADER_LENGTH)
if not len(username_header):
print('Connection closed by the server')
sys.exit()
username_length = int(username_header.decode('utf-8').strip())
username = client_socket.recv(username_length).decode('utf-8')
message_header = client_socket.recv(HEADER_LENGTH)
message_length = int(message_header.decode('utf-8').strip())
message = client_socket.recv(message_length).decode('utf-8')
print(f'{username} > {message}')
except IOError as e:
if e.errno != errno.EAGAIN and e.errno != errno.EWOULDBLOCK:
print('Reading error: {}'.format(str(e)))
sys.exit()
continue
except Exception as e:
print('Reading error: {}'.format(str(e)))
sys.exit()