forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_client.py
More file actions
30 lines (24 loc) · 738 Bytes
/
Copy pathchat_client.py
File metadata and controls
30 lines (24 loc) · 738 Bytes
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
import socket
import threading
def receive_messages(client_socket):
"""Receive messages from the server."""
while True:
try:
message = client_socket.recv(1024).decode('utf-8')
if message:
print(f"\n{message}")
else:
break
except:
break
# Set up the client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 12345)) # Connect to the server
# Start a thread to listen for messages
threading.Thread(target=receive_messages, args=(client,)).start()
while True:
message = input("You: ")
if message.lower() == 'exit':
break
client.send(message.encode('utf-8'))
client.close()