-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsc_client.py
More file actions
47 lines (40 loc) · 1.34 KB
/
sc_client.py
File metadata and controls
47 lines (40 loc) · 1.34 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
import socket
import threading
import sys
HEADER = 64
PORT = 5050
FORMAT = "utf-8"
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = "IPV4-HERE" # Server's computer ipv4 address
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
def send_msg():
while True:
sen_msg = input('\nMessage: ')
if sen_msg == '!DISCONNECT':
send(DISCONNECT_MESSAGE)
print('Disconnected from the server.')
break
send(sen_msg)
def receive_msg():
while True:
message_length = client.recv(HEADER).decode(FORMAT)
message_info = client.recv(HEADER).decode(FORMAT)
sys.stdout.write('\r' + ' ' * 10 + '\r')
sys.stdout.flush()
print(f"[('{message_info.split(':')[0]}', {message_info.split(':')[1]})] {message_length}")
print("Message: ", end="", flush=True)
# Start a separate thread for receiving messages
receive_thread = threading.Thread(target=receive_msg)
receive_thread.start()
# Start a separate thread for sending messages
send_thread = threading.Thread(target=send_msg)
send_thread.start()