-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPC_Client.py
More file actions
41 lines (32 loc) · 997 Bytes
/
IPC_Client.py
File metadata and controls
41 lines (32 loc) · 997 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
30
31
32
33
34
35
36
37
38
39
40
41
import socket
import threading
def receive_messages(client_socket):
while True:
try:
reply = client_socket.recv(1024).decode("utf-8")
if reply:
print(reply)
else:
break
except:
break
print("Disconnected from server.")
client_socket.close()
def start_client():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client_socket.connect(("localhost", 8888))
print("Connected to server")
name = input("Enter your name: ")
client_socket.send(name.encode("utf-8"))
threading.Thread(
target=receive_messages, args=(client_socket,), daemon=True
).start()
while True:
message = input()
if message:
client_socket.send(message.encode("utf-8"))
except Exception as e:
print(f"Connection error: {e}")
client_socket.close()
start_client()