CS144-check4

overview

这个实验相当于测试前几个实验的搭建的TCP栈的正确性,我的代码实现没有问题,所以在实验4不再需要改主要的代码。只需要在webget中修改TCPSocketCS144TCPSocket并加上对应的头文件,运行配置的脚本,就可以使用我们自己实现的TCP协议栈在网络中通信了!

值得注意的是,UTM的Emulated VLAN模式下好像并不能ping,要使用Shared Network才行。

由于时间问题,我只跑了一个小时的数据,以下为数据的分析以及分析的源码。

data analyzing

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
89
90
91
92
93
94
95
96
import re
import matplotlib.pyplot as plt
import pandas as pd

# Read the data from the file
with open('data.txt', 'r') as file:
data = file.read()

# compute the overall delivery date
icmp_seq_numbers = re.findall(r'icmp_seq=(\d+)', data)
icmp_seq_numbers = list(map(int, icmp_seq_numbers)) # Convert to integers

min_seq = min(icmp_seq_numbers)
max_seq = max(icmp_seq_numbers)

expected_seq_numbers = set(range(min_seq, max_seq + 1))

missing_seq_numbers = expected_seq_numbers - set(icmp_seq_numbers)
missing_numbers_len = len(missing_seq_numbers)
overall_delivery_rate = 1 - len(missing_seq_numbers) / len(expected_seq_numbers)

print(f"overall delivery date: {overall_delivery_rate}")

# find the longest consecutive string of successful pings
longest_streak = 1
current_streak = 1

for i in range(1, len(icmp_seq_numbers)):
if icmp_seq_numbers[i] == icmp_seq_numbers[i - 1] + 1:
current_streak += 1
else:
longest_streak = max(longest_streak, current_streak)
current_streak = 1 # Reset for the next potential sequence

longest_streak = max(longest_streak, current_streak)

print(f"Length of the longest consecutive sequence: {longest_streak}")

# Find the length of the longest consecutive missing sequence
missing_seq_numbers = sorted(expected_seq_numbers - set(icmp_seq_numbers))
longest_missing_streak = 0
current_streak = 0

for i in range(1, len(missing_seq_numbers)):
if missing_seq_numbers[i] == missing_seq_numbers[i - 1] + 1:
current_streak += 1
else:
longest_missing_streak = max(longest_missing_streak, current_streak + 1) # +1 to count the first missing number
current_streak = 0

longest_missing_streak = max(longest_missing_streak, current_streak + 1)

print(f"Length of the longest consecutive missing sequence: {longest_missing_streak}")

# Compute the independent or correlated of "packet losses"
received_replies = set(icmp_seq_numbers)

successful_n = 0
successful_n_plus_1 = 0

for n in range(min(received_replies), max(received_replies)):
if n in received_replies:
successful_n += 1
if n + 1 in received_replies:
successful_n_plus_1 += 1

if successful_n > 0:
probability = successful_n_plus_1 / successful_n
else:
probability = 0.0

print(f"Probability that echo request seqno #(N+1) was replied to given seqno #N was replied to: {probability:.2f}")

unsuccessful_n = 0
successful_n_plus_1 = 0

for n in range(min(received_replies), max(received_replies)):
if n not in received_replies:
unsuccessful_n += 1
if (n + 1) in received_replies:
successful_n_plus_1 += 1

if unsuccessful_n > 0:
probability = successful_n_plus_1 / unsuccessful_n
else:
probability = 0.0

print(f"Probability that echo request seqno #(N+1) was replied to given seqno #N did not receive a reply: {probability:.2f}")

# RTT
rtt_values = re.findall(r'time=(\d+\.?\d*) ms', data)
rtt_values = list(map(float, rtt_values))
min_rtt = min(rtt_values)
max_rtt = max(rtt_values)
print(f"Minimum RTT seen over the entire interval: {min_rtt:.2f} ms")
print(f"Maximum RTT seen over the entire interval: {max_rtt:.2f} ms")

测试的结果:

1
2
3
4
5
6
7
overall delivery date: 0.958145555662096
Length of the longest consecutive sequence: 311
Length of the longest consecutive missing sequence: 300
Probability that echo request seqno #(N+1) was replied to given seqno #N was replied to: 0.98
Probability that echo request seqno #(N+1) was replied to given seqno #N did not receive a reply: 0.41
Minimum RTT seen over the entire interval: 457.00 ms
Maximum RTT seen over the entire interval: 1164.00 ms

graph analyzing

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
import re
import matplotlib.pyplot as plt
import pandas as pd

# Read the data from the file
with open('data.txt', 'r') as file:
data = file.read()

# Extract RTT values and timestamps using regular expressions
timestamps = re.findall(r'\[(\d+\.\d+)\] 64 bytes', data)
rtt_values = re.findall(r'time=(\d+\.?\d*) ms', data)

# Convert RTT values to float
rtt_values = list(map(float, rtt_values))

# Convert timestamps to datetime format
timestamps = [pd.to_datetime(float(ts), unit='s', origin='unix') for ts in timestamps]

# Create a DataFrame for easier plotting
df = pd.DataFrame({'Time': timestamps, 'RTT': rtt_values})

# Plotting
plt.figure(figsize=(12, 6))
plt.plot(df['Time'], df['RTT'], marker='o', linestyle='-', color='b')
plt.title('RTT as a Function of Time')
plt.xlabel('Time of Day')
plt.ylabel('RTT (ms)')
plt.xticks(rotation=45)
plt.grid()

# Show the plot
plt.tight_layout()
plt.show()

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
import re
import matplotlib.pyplot as plt
import numpy as np

# Read the data from the file
with open('data.txt', 'r') as file:
data = file.read()

# Extract RTT values using regular expressions
rtt_values = re.findall(r'time=(\d+\.?\d*) ms', data)
rtt_values = list(map(float, rtt_values)) # Convert to float

# Check if there are RTT values
if not rtt_values:
print("No RTT values found.")
else:
# Plot Histogram
plt.figure(figsize=(12, 6))
plt.hist(rtt_values, bins=30, edgecolor='black', alpha=0.7)
plt.title('Histogram of RTTs')
plt.xlabel('RTT (ms)')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()

# Plot Cumulative Distribution Function (CDF)
plt.figure(figsize=(12, 6))
sorted_rtt = np.sort(rtt_values)
cdf = np.arange(len(sorted_rtt)) / float(len(sorted_rtt))
plt.plot(sorted_rtt, cdf, marker='.', linestyle='none')
plt.title('Cumulative Distribution Function (CDF) of RTTs')
plt.xlabel('RTT (ms)')
plt.ylabel('CDF')
plt.grid(True)
plt.show()


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
import re
import matplotlib.pyplot as plt
import numpy as np

# Read the data from the file
with open('data.txt', 'r') as file:
data = file.read()

# Extract RTT values using regular expressions
rtt_values = re.findall(r'time=(\d+\.?\d*) ms', data)
rtt_values = list(map(float, rtt_values)) # Convert to float

# Create pairs of consecutive RTT values (N and N+1)
if len(rtt_values) < 2:
print("Not enough RTT values for correlation analysis.")
else:
rtt_N = rtt_values[:-1] # RTT of ping #N
rtt_N_plus_1 = rtt_values[1:] # RTT of ping #N+1

# Plot RTT of ping #N vs RTT of ping #N+1
plt.figure(figsize=(8, 6))
plt.scatter(rtt_N, rtt_N_plus_1, alpha=0.6, edgecolors='black')
plt.title('Correlation between RTT of Ping #N and RTT of Ping #N+1')
plt.xlabel('RTT of Ping #N (ms)')
plt.ylabel('RTT of Ping #N+1 (ms)')
plt.grid(True)
plt.show()

由图表可以看出,两者几乎是独立的概率,前者并不会影响后者。

总结

阶段性胜利!!!,让我们接着实现IP栈吧!


CS144-check4
https://pactheman123.github.io/2024/09/25/CS144-check4/
作者
Xiaopac
发布于
2024年9月25日
许可协议