Main Content

Byte Transmission Using UDP

Send and receive UDP packets using the dsp.UDPSender and dsp.UDPReceiver System objects. Calculate the number of bytes successfully transmitted.

Note: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example, obj(x) becomes step(obj,x).

Set the RemoteIPPort of UDP sender and the LocalIPPort of the UDP receiver to 31000. Set the length of the data vector to 128 samples, which is less than the value of the MaximumMessageLength property of the receiver. To prevent the loss of packets, call the setup method on the receiver object before the first call to the object algorithm.

udpr = dsp.UDPReceiver('LocalIPPort',31000);
udps = dsp.UDPSender('RemoteIPPort',31000);


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

In each loop of iteration, send and receive a packet of data. At the end of the loop, use the fprintf function to print the number of bytes sent by the sender and the number of bytes received by the receiver.

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


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

See Also


Related Topics