import socket

def start_server(host='0.0.0.0', port=12345):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
        server_socket.bind((host, port))
        server_socket.listen()
        print(f"Server listening on {host}:{port}")

        while True:
            conn, addr = server_socket.accept()
            with conn:
                print(f"Connected by {addr}")
                with open("wifiFile.txt", "a") as file:  # Open the file in append mode
                    while True:
                        data = conn.recv(1024)
                        if not data:
                            break
                        decoded_data = data.decode()
                        print("Received:", decoded_data)
                        file.write(decoded_data + "\n")  # Write data to file with a newline

start_server()
