-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
36 lines (30 loc) · 858 Bytes
/
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
import socket
import threading
# Create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define the port on which you want to connect
IP = 'localhost'
PORT = 12345
# connect to the server on local computer
try:
client.connect((IP, PORT))
except socket.error as e:
print(f"Could not connect to server! Error: {e}")
exit()
def receive():
while True:
try:
message = client.recv(1024).decode('utf-8')
print(message)
except Exception as e:
print(f"Error receiving message: {e}")
client.close()
break
def send():
while True:
message = input('')
client.send(message.encode('utf-8'))
thread_receive = threading.Thread(target=receive)
thread_receive.start()
thread_send = threading.Thread(target=send)
thread_send.start()