-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer (Receiver) Code
More file actions
30 lines (22 loc) · 939 Bytes
/
Server (Receiver) Code
File metadata and controls
30 lines (22 loc) · 939 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
import socket
def start_server(host='127.0.0.1', port=12345):
"""Starts a TCP server that listens for incoming messages."""
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen(5)
print(f"Server listening on {host}:{port}...")
while True:
client_socket, client_address = server_socket.accept()
print(f"Connected to {client_address}")
while True:
data = client_socket.recv(1024).decode()
if not data or data.lower() == 'exit':
print(f"Connection closed by {client_address}")
break
print(f"Received: {data}")
response = f"Server received: {data}"
client_socket.send(response.encode())
client_socket.close()
server_socket.close()
if __name__ == "__main__":
start_server()