socket programming
socket programming
def start_server():
"""
Start the server and handle client connections.
"""
host = '127.0.0.1' # Localhost
port = 65432 # Port to listen on
while True:
# Step 5: Receive data from the client
data = conn.recv(1024).decode()
if not data or data.lower() == "exit": # Check if the client wants to
disconnect
print("Client disconnected.")
break
if __name__ == "__main__":
start_server()
client.py-------------------------------------------------------
import socket
def start_client():
"""
Connect to the server and send computation requests.
"""
host = '127.0.0.1' # Server IP
port = 65432 # Server port
while True:
# Step 2: Get user input
message = input("Your request: ")
client_socket.send(message.encode()) # Step 3: Send request to the server
if __name__ == "__main__":
start_client()