This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

dsp.UDPReceiver System object

Receive UDP packets from network


The UDPReceiver object receives UDP packets from the network.

To receive UDP packets from the network:

  1. Define and set up your UDP receiver. See Construction.

  2. Call step to receive the UDP packets according to the properties of dsp.UDPReceiver. The behavior of step is specific to each object in the toolbox.


Starting in R2016b, instead of using the step method to perform the operation defined by the System object™, you can call the object with arguments, as if it were a function. For example, y = step(obj) and y = obj() perform equivalent operations.


udpr = dsp.UDPReceiver returns a UDP receiver System object, udpr, that receives UDP packets from a specified port.

udpr = dsp.UDPReceiver('PropertyName',PropertyValue, ...) returns a window object, udpr, with each property set to the specified value.



Local port on which to receive data

Specify the port on which to receive data. This property is tunable in generated code but not tunable during simulation. The default is 25000. The value can be in the range [1 65535].


Address from which data was sent

Specify the remote IP address from which to receive data. The default is '', which indicates that data can be accepted from any remote IP address.


Maximum size of internal buffer

Specify the size of the buffer that receives UDP packets, in bytes. The default is 8192.


Maximum size of output message

Specify the size of the output message. If received packets exceed the specified value, their contents are truncated. The default is 255.


Data type of the message

Specify the data type of the message as | double | single | int8 | uint8 | int16 | uint16 | int32 | uint32 | logical |. The default is uint8.


stepReceive UDP packet
Common to All System Objects

Create System object with same property values


Expected number of inputs to a System object


Expected number of outputs of a System object


Check locked states of a System object (logical)


Allow System object property value changes


expand all

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™, 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:

udpr = dsp.UDPReceiver('LocalIPPort',31000);
udps = 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.

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

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

Note: This example runs only in R2017a or later.

The TunableUDP function contains the algorithm to send and receive sine wave data over a UDP network. The inputs to this function are the local IP port number and the remote IP port number of the dsp.UDPReceiver and the dsp.UDPSender System objects, respectively. The outputs of this function are the number of bytes sent and received over the UDP network.

type TunableUDP
function [bytesSent,bytesReceived] = TunableUDP(localipport,remoteipport)

persistent udpRx
persistent udpSend
persistent sine

if isempty(udpRx) 
    udpRx = dsp.UDPReceiver('MessageDataType','double'); 
    udpSend = dsp.UDPSender;
    sine = dsp.SineWave('SamplesPerFrame',250);

udpRx.LocalIPPort = localipport; 
udpSend.RemoteIPPort = remoteipport; 

bytesSent = 0;
bytesReceived = 0;
dataLength = 250;
dataSent = sine();

for i = 1:10
    dataSent = sine();
    bytesSent = bytesSent + dataLength;
    dataReceived = udpRx();
    bytesReceived = bytesReceived + length(dataReceived);

You can change the local IP port number and the remote IP port number in the generated code without having to regenerate the code. Generate a MEX file from the TunableUDP.m function.

codegen TunableUDP -args {65000,65000}

Call the MEX function, TunableUDP_mex, with RemoteIPPort and LocalIPPort numbers set to 65000.

[bytesSentPort65000,bytesReceivedPort65000] = TunableUDP_mex(65000,65000)
bytesSentPort65000 = 2500
bytesReceivedPort65000 = 2500

All the 2500 bytes sent are successfully received.

Clear the MEX function and change the port numbers to 25000, 20000, and 45000. Verify the number of bytes transmitted and received.

clear mex;
[bytesSentPort25000,bytesReceivedPort25000] = TunableUDP_mex(25000,25000)
bytesSentPort25000 = 2500
bytesReceivedPort25000 = 2500

Clear the MEX function and change the port numbers to 20000.

clear mex;
[bytesSentPort20000,bytesReceivedPort20000] = TunableUDP_mex(20000,20000)
bytesSentPort20000 = 2500
bytesReceivedPort20000 = 2500

Clear the MEX function and change the port numbers to 45000.

clear mex;
[bytesSentPort45000,bytesReceivedPort45000] = TunableUDP_mex(45000,45000)
bytesSentPort45000 = 2500
bytesReceivedPort45000 = 2500


This object implements the algorithm, inputs, and outputs described on the UDP Receive block reference page. The object properties correspond to the block parameters, except:

  • The Output variable-size signal parameter is not included in the System object.

  • The Sample time parameter is not included in the System object.

Extended Capabilities

See Also

System Objects


Introduced in R2012a

Was this topic helpful?