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
|
function udpcrossping(servermode, hostname, port)
count = 0;
% Open udpsocket and bind udp port adress to it.
udp=pnet('udpsocket', port);
% Act as server or client?
if servermode
% Server waits for echo-requests and then responds with his system
% time:
% Use timeout to not block forever, makes it possible to update resized window.
while count < 1000
% Wait/Read udp packet to read buffer
len=pnet(udp,'readpacket');
if len>0,
% if packet larger then 1 byte then read maximum of 1 doubles in network byte order
data=pnet(udp,'read',1,'double');
%figure(fg);
% .... and plot doubles in axis.
%plot(data);
end
% Send reply:
pnet(udp,'write', GetSecs); % Write system time to write buffer
% [ip,rport]=pnet(udp, 'gethost');
pnet(udp,'writepacket', hostname, port); % Send buffer as UDP packet
% Increment count:
count = count + 1;
end
else
% Client sends request, waits for answer, times roundtrip:
localtime=zeros(1,1000);
remotetime=zeros(1,1000);
for i=1:1000
% Send local time:
pnet(udp,'write', GetSecs); % Write system time to write buffer
tpresend=GetSecs;
pnet(udp,'writepacket', hostname, port); % Send buffer as UDP packet
tpostsend=GetSecs;
tsent = (tpresend + tpostsend)/2;
% Wait for response:
len=pnet(udp,'readpacket');
tpostreceive = GetSecs;
roundtriptime = tpostreceive - tsent;
if len>0,
% if packet larger then 1 byte then read maximum of 1 doubles in network byte order
remotetimesample=pnet(udp,'read',1,'double');
%figure(fg);
% .... and plot doubles in axis.
%plot(data);
end
localtime(i)=tsent + 0.5 * roundtriptime;
remotetime(i)=remotetimesample;
end
end
% Close udp connection.
pnet(udp,'close');
if ~servermode
% Absolute values:
plot(localtime, remotetime);
% Realtive values:
figure;
baseline = min(localtime, remotetime);
plot(localtime - baseline, remotetime - baseline);
end
% End.
return;
|