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.UDPSender System object

Send UDP packets to network


The UDPSender object sends UDP packets to the network.

To send UDP packets to the network:

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

  2. Call step to send the packets according to the properties of dsp.UDPSender. 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,x) and y = obj(x) perform equivalent operations.


udps = dsp.UDPSender returns a UDP sender object, udps, that sends UDP packets to a specified port.

udps = dsp.UDPSender('PropertyName',PropertyValue, ...) returns a UDP sender object, udps, with each property set to the specified value.



Remote address to which to send data

Specify the remote (that is, host) IP address to which the data is sent. The default is '', which is the local host.


Remote port to which to send data

Specify the port at the remote IP address to which the data is sent. This property is tunable in generated code but not tunable during simulation. The default is 25000.


Source of the LocalIPPort property

Specify how to determine the port on the host as | Auto | Property |. If you specify Auto, the object selects the port dynamically from the available ports. If you specify Property, the object uses the source specified in the LocalIPPort property. The default is Auto.


Local port from which to send data

Specify the port from which to send data. This property applies when you set the LocalIPPortSource property to Auto. The default is 25000.


stepSend a UDP packet to network
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

Extended Capabilities

See Also

System Objects


Introduced in R2012a

Was this topic helpful?