-
Notifications
You must be signed in to change notification settings - Fork 0
/
USE_chatroom_client2.py
64 lines (49 loc) · 1.92 KB
/
USE_chatroom_client2.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
import socket
import select
import errno
import sys
HEADER_LENGTH = 10
# 127.0.0.1
# online "DO staff" ip: 10.151.40.77
IP = "127.0.0.1"
PORT = 1909
my_username = input("Username: ")
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((IP, PORT))
client_socket.setblocking(False)
# Send the username to the server
username = my_username.encode('utf-8')
username_header = f"{len(username):<{HEADER_LENGTH}}".encode('utf-8')
client_socket.send(username_header + username)
while True:
try:
# Wait for user to input a message
message = input(f'{my_username} > ')
if message:
# Encode message to bytes, prepare header and convert to bytes, then send
message = message.encode('utf-8')
message_header = f"{len(message):<{HEADER_LENGTH}}".encode('utf-8')
client_socket.send(message_header + message)
# Handle incoming messages from the server
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:
# Handle non-blocking socket
if e.errno == errno.EAGAIN or e.errno == errno.EWOULDBLOCK:
continue
else:
print('Reading error: {}'.format(str(e)))
sys.exit()
except Exception as e:
# Any other exception - something happened, exit
print('Error: {}'.format(str(e)))
sys.exit()