-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient2.py
37 lines (31 loc) · 1006 Bytes
/
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
import socket
import threading
def receive_messages():
while True:
try:
received_data = client_socket.recv(1024).decode()
print(received_data)
except ConnectionResetError:
print("Server closed the connection unexpectedly")
break
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '127.0.0.1'
port = 12345
client_socket.connect((host, port))
username = input("Enter your username (max 16 characters): ")[:16]
client_socket.send(username.encode())
welcome_message = client_socket.recv(1024).decode()
print(welcome_message)
receive_thread = threading.Thread(target=receive_messages)
receive_thread.start()
try:
while True:
response = input("")
if response.lower() == 'exit':
break
message_to_send = f"{response}"
client_socket.send(message_to_send.encode())
except KeyboardInterrupt:
pass
finally:
client_socket.close()