Main Content

udp

(To be removed) Create UDP object

udp will be removed in a future release. Use udpport instead. For more information on updating your code, see Version History.

Description

u = udp creates a UDP object, u, not associated with a remote host. If you use this syntax, you must assign a remote host after object creation if you want to send data. If you want to only receive data, you do not need to set a remote host.

The UDP object must be bound to the local socket with the fopen function. The default local host in multihome hosts is the system default. The LocalPort property defaults to a value of [], allowing any free local port to be used. LocalPort is updated with a value when fopen is issued. When the UDP object is created, its Status property value is 'closed'. Once the object is bound to the local socket with fopen, Status is configured to 'open'.

The maximum packet size for reading is 8192 bytes. The input buffer can hold as many packets as defined by the InputBufferSize property value. You can write any data size to the output buffer. The data is sent in packets of at most 4096 bytes.

u = udp(RemoteHost) creates a UDP object associated with the remote host RemoteHost.

u = udp(RemoteHost,RemotePort) creates a UDP object with the specified remote port value, RemotePort. If not specified, the default remote port is 9090.

u = udp(___,Name,Value) creates a UDP object and specifies additional options with one or more name-value pair arguments. If you specify an invalid property name or property value, the object is not created.

example

Examples

collapse all

Use a UDP object to write to an echo server and read back the message.

Start the echo server and create a UDP object.

echoudp('on',4012)
u = udp('127.0.0.1',4012);

Connect the UDP object to the host.

fopen(u)

Write to the host, and then read from the host.

fwrite(u,65:74)
A = fread(u,10)
A =

    65
    66
    67
    68
    69
    70
    71
    72
    73
    74

Stop the echo server and disconnect the UDP object from the host.

echoudp('off')
fclose(u)

Input Arguments

collapse all

Remote host ID, specified as a character vector or string, identifying IP address or host name.

Example: '127.0.0.1'

Data Types: char | string

Port on remote host, specified as a numeric integer value from 1 to 65535.

Example: 8001

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'LocalPort',4080

Commonly used properties for this object are:

Port on local host, specified as a numeric integer value from 1 to 65535.

Example: 4080

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Port mode on local host, specified as 'manual' or 'auto'.

Example: 'manual'

Data Types: char | string

Time limit in seconds for communication, specified as a numeric value. The default is 10 seconds.

Example: 60

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

UDP interface, returned as an interface object.

Version History

Introduced before R2006a

expand all

R2022a: Warns

udp will be removed in a future release. Use udpport instead.

This example shows how to connect to a UDP socket using the recommended functionality.

FunctionalityUse This Instead
u = udp;
fopen(u)
u = udpport("byte");
u = udpport("datagram");

The recommended interface has additional capabilities and improved performance. See Transition Your Code to udpport Interface for more information about using the recommended functionality.

See Also

Functions