% 此脚本用来绘制折线图
%% 统计部分
clear all, close all, clc;
load 'data.mat';
N = length(data);
p1 = 0; % TCP出发端口指针
p2 = 0; % TCP目的端口指针
p3 = 0; % UDP出发端口指针
p4 = 0; % UDP目的端口指针
flag = 0; % 重复旗帜
for i = 1:N
temp = data(i);
if strcmp(temp.proto,'TCP')
if p1 == 0
p1 = p1 + 1;
Data_source_tcp(p1).port = temp.source_port;
Data_source_tcp(p1).num = 1;
else
for m = 1:p1
if Data_source_tcp(m).port == temp.source_port
flag = 1;
Data_source_tcp(m).num = Data_source_tcp(m).num + 1;
end
end
if flag == 0
p1 = p1 + 1;
Data_source_tcp(p1).port = temp.source_port;
Data_source_tcp(p1).num = 1;
end
flag = 0;
end
if p2 == 0
p2 = p2 + 1;
Data_dest_tcp(p2).port = temp.dest_port;
Data_dest_tcp(p2).num = 1;
else
for m = 1:p2
if Data_dest_tcp(m).port == temp.dest_port
flag = 1;
Data_dest_tcp(m).num = Data_dest_tcp(m).num + 1;
end
end
if flag == 0
p2 = p2 + 1;
Data_dest_tcp(p2).port = temp.dest_port;
Data_dest_tcp(p2).num = 1;
end
flag = 0;
end
else
if strcmp(temp.proto,'UDP')
if p3 == 0
p3 = p3 + 1;
Data_source_udp(p3).port = temp.source_port;
Data_source_udp(p3).num = 1;
else
for m = 1:p3
if Data_source_udp(m).port == temp.source_port
flag = 1;
Data_source_udp(m).num = Data_source_udp(m).num + 1;
end
end
if flag == 0
p3 = p3 + 1;
Data_source_udp(p3).port = temp.source_port;
Data_source_udp(p3).num = 1;
end
flag = 0;
end
if p4 == 0
p4 = p4 + 1;
Data_dest_udp(p4).port = temp.dest_port;
Data_dest_udp(p4).num = 1;
else
for m = 1:p4
if Data_dest_udp(m).port == temp.dest_port
flag = 1;
Data_dest_udp(m).num = Data_dest_udp(m).num + 1;
end
end
if flag == 0
p4 = p4 + 1;
Data_dest_udp(p4).port = temp.dest_port;
Data_dest_udp(p4).num = 1;
end
flag = 0;
end
end
end
end
%% 排序部分
[tempsort1,index_source_tcp] = sort([Data_source_tcp.num]);
[tempsort2,index_dest_tcp] = sort([Data_dest_tcp.num]);
[tempsort3,index_source_udp] = sort([Data_source_udp.num]);
[tempsort4,index_dest_udp] = sort([Data_dest_udp.num]);
%% 绘制折线图
Ni = 10;
%% tcp source 部分
for i = length(index_source_tcp):-1:length(index_source_tcp)-Ni+1
tempport = Data_source_tcp(index_source_tcp(i)).port;
plotdata = zeros(1,Data_source_tcp(index_source_tcp(i)).num);
p5 = 1;
for m = 1:N
temp = data(m);
if strcmp(temp.proto,'TCP')
if temp.source_port == tempport
plotdata(1,p5) = temp.length;
p5 = p5 + 1;
end
end
end
figure;
cdfplot(plotdata);
title(['cdf of length of port:',num2str(tempport),' in TCP source']);
end
%% tcp dest 部分
for i = length(index_dest_tcp):-1:length(index_dest_tcp)-Ni+1
tempport = Data_dest_tcp(index_dest_tcp(i)).port;
plotdata = zeros(1,Data_dest_tcp(index_dest_tcp(i)).num);
p5 = 1;
for m = 1:N
temp = data(m);
if strcmp(temp.proto,'TCP')
if temp.dest_port == tempport
plotdata(1,p5) = temp.length;
p5 = p5 + 1;
end
end
end
figure;
cdfplot(plotdata);
title(['cdf of length of port:',num2str(tempport),' in TCP dest']);
end
%% udp source 部分
for i = length(index_source_udp):-1:length(index_source_udp)-Ni+1
tempport = Data_source_udp(index_source_udp(i)).port;
plotdata = zeros(1,Data_source_udp(index_source_udp(i)).num);
p5 = 1;
for m = 1:N
temp = data(m);
if strcmp(temp.proto,'UDP')
if temp.source_port == tempport
plotdata(1,p5) = temp.length;
p5 = p5 + 1;
end
end
end
figure;
cdfplot(plotdata);
title(['cdf of length of port:',num2str(tempport),' in UDP source']);
end
%% udp dest 部分
for i = length(index_dest_udp):-1:length(index_dest_udp)-Ni+1
tempport = Data_dest_udp(index_dest_udp(i)).port;
plotdata = zeros(1,Data_dest_udp(index_dest_udp(i)).num);
p5 = 1;
for m = 1:N
temp = data(m);
if strcmp(temp.proto,'UDP')
if temp.dest_port == tempport
plotdata(1,p5) = temp.length;
p5 = p5 + 1;
end
end
end
figure;
cdfplot(plotdata);
title(['cdf of length of port:',num2str(tempport),' in UDP dest']);
end
评论0