-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
57 lines (45 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
import socket
import threading
import time
HEADER = 64 # we dont know how long the next message will be, the first msg to the server each time will be length 64
PORT = 5050
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!dc"
#SERVER = "10.247.222.133"
SERVER = socket.gethostbyname(socket.gethostname()) # gets the ip address automatically, avoids hardcoding
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
connected = True
# this function allows us to send messages from client to server
def send(msg):
message = msg.encode(FORMAT) # we have to encode messages in byte format before we send them
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
# b' ' is byte space
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
#print(client.recv(2048).decode(FORMAT))
# this function runs on a thread and actively listens for new messages
def receive():
while connected:
time.sleep(3)
print(client.recv(2048).decode(FORMAT))
def initialize():
print(client.recv(2048).decode(FORMAT)) # server asks what the user's name is
name = input()
send(name) # user sends their name
print(client.recv(2048).decode(FORMAT)) # welcome message from the user
initialize()
thread = threading.Thread(target=receive) # creates a thread to listen to other user messages
thread.start()
while True:
new_msg = input()
if new_msg == DISCONNECT_MESSAGE:
send(DISCONNECT_MESSAGE)
connected = False # exits the thread loop
break
send(new_msg)
thread.join() # "closes" the thread
print("DISCONNECTING FROM SERVER...")