# Practical 5 – Timestamp Replay Detection: Client

import socket
import time
from datetime import datetime, timezone

with socket.socket() as client:
    client.connect(("127.0.0.1", 9999))
    client.sendall(b"alice|secret_password")

    if client.recv(1024) != b"AUTH_OK":
        print("Login failed")
    else:
        print("Login successful")

        while True:
            msg = input("Message (or exit): ")
            if msg == "exit":
                break

            timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S.%f")
            packet = f"{msg}||{timestamp}".encode()

            client.sendall(packet)
            print("Server:", client.recv(1024).decode())

            time.sleep(1)
            client.sendall(packet)
            print("Replay:", client.recv(1024).decode())

# What this code does: sends timestamped messages to the local replay-detection
# server and resends each message to demonstrate rejection of a replay.
