-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
88 lines (70 loc) · 3.02 KB
/
Copy pathclient.py
File metadata and controls
88 lines (70 loc) · 3.02 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import socket
import time
import sys
# CLI Input Function
def get_user_input(prompt, default, min_val=None, max_val=None, dtype=str):
while True:
try:
value = input(f"{prompt} (default {default}): ") or default
value = dtype(value)
if min_val is not None and max_val is not None and not (min_val <= value <= max_val):
print(f"⚠️ Value must be between {min_val} and {max_val}. Try again.")
else:
return value
except ValueError:
print("⚠️ Invalid input. Please enter a valid value.")
# Get user-defined values
SERVER_IP = get_user_input("Enter server IP address", "192.168.1.100")
SERVER_PORT = 5005
NUM_PACKETS = int(get_user_input("Enter number of packets", 1000, 1, 100000, int))
PAYLOAD_SIZE = int(get_user_input("Enter packet size (bytes)", 128, 1, 1472, int))
PACKET_INTERVAL = get_user_input("Enter packet interval (seconds)", 0.005, 0.0001, 1.0, float)
# Create UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(2) # 2 seconds timeout for responses
latencies = []
successful_packets = 0
lost_packets = 0
start_time = time.time()
print(f"\n🚀 Sending {NUM_PACKETS} packets to {SERVER_IP}:{SERVER_PORT} with {PAYLOAD_SIZE} bytes each, every {PACKET_INTERVAL:.4f} seconds...\n")
for i in range(NUM_PACKETS):
send_time = time.time()
payload = b"x" * PAYLOAD_SIZE # Create dummy payload
try:
# Send packet
sock.sendto(payload, (SERVER_IP, SERVER_PORT))
# Receive timestamp from server
data, _ = sock.recvfrom(1024)
recv_time = time.time()
server_time = float(data.decode())
latency = (recv_time - send_time) * 1000 # Convert to ms
latencies.append(latency)
successful_packets += 1
except socket.timeout:
lost_packets += 1
latencies.append(None)
# Live stats
avg_latency = sum(filter(None, latencies)) / successful_packets if successful_packets else 0
packet_loss = (lost_packets / (successful_packets + lost_packets)) * 100
sys.stdout.write(f"\r📡 Sent: {i+1}/{NUM_PACKETS} | ✅ Received: {successful_packets} | ❌ Lost: {lost_packets} | 📊 Latency: {avg_latency:.2f} ms | 🚫 Loss: {packet_loss:.2f}% ")
sys.stdout.flush()
time.sleep(PACKET_INTERVAL) # Delay between packets
# Send END signal to stop the server
sock.sendto(b"END", (SERVER_IP, SERVER_PORT))
end_time = time.time()
elapsed_time = end_time - start_time
# Calculate throughput
total_bytes_sent = successful_packets * PAYLOAD_SIZE
throughput_mbps = (total_bytes_sent * 8) / (elapsed_time * 1e6)
# Final Stats
print("\n\n📊 Final Results:")
print(f" - Server IP: {SERVER_IP}")
print(f" - Packets sent: {NUM_PACKETS}")
print(f" - Packets received: {successful_packets}")
print(f" - Packet loss: {packet_loss:.2f}%")
if successful_packets:
print(f" - Average latency: {avg_latency:.2f} ms")
else:
print(" - No packets received.")
print(f" - Throughput: {throughput_mbps:.2f} Mbps")
sock.close()