www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/ByteTransmissionUsingUDPExample.m

    %% Byte Transmission using UDP
%%
% *Note*: This example runs only in R2016b or later. If you are using an
% earlier release, replace each call to the function with the equivalent
% |step| syntax. For |dsp.UDPReceiver| System object(TM), myObject() becomes 
% step(myObject). For all other objects, myObject(x) becomes 
% step(myObject,x).

%%
% Send some UDP packets, and calculate the number of successfully 
% transmitted bytes:
hudpr = dsp.UDPReceiver('LocalIPPort',31000);
hudps = dsp.UDPSender('RemoteIPPort',31000);
% To prevent the loss of packets, call the |setup| method
% on the object before the first call to the |step| method.
setup(hudpr); 

bytesSent = 0;
bytesReceived = 0;
dataLength = 128;

for k = 1:20
   dataSent = uint8(255*rand(1,dataLength));
   bytesSent = bytesSent + dataLength;
   hudps(dataSent);
   dataReceived = hudpr();
   bytesReceived = bytesReceived + length(dataReceived);
end

release(hudps);
release(hudpr);

fprintf('Bytes sent:     %d\n', bytesSent);
fprintf('Bytes received: %d\n', bytesReceived);