forked from makazis/Placeholdeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtpl_client.py
38 lines (32 loc) · 1.04 KB
/
mtpl_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
import socket
import threading
# Server settings
HOST = '127.0.0.1' # Server address
PORT = 12345 # Server port
# Function to receive messages from the server
def receive_messages(client_socket):
while True:
try:
message = client_socket.recv(1024).decode('utf-8')
if message:
print(message)
except ConnectionResetError:
print("Disconnected from server.")
break
# Main client function
def start_client():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
print("Connected to the server.")
# Start a thread to listen for messages
threading.Thread(target=receive_messages, args=(client_socket,)).start()
# Main loop to send messages
try:
while True:
message = input("You: ")
client_socket.sendall(message.encode('utf-8'))
except KeyboardInterrupt:
print("Exiting...")
client_socket.close()
if __name__ == "__main__":
start_client()