---
title: "UdpClient (_UdpClient class)"
canonical: "https://docs.cuesystem.com/space/CVCHELP/522289235/UdpClient%20(_UdpClient%20class)"
format: markdown
---
> Macro (toc)

---

# ClearReceiveBuffer

The command removes received data from a receive buffer. 

#### Syntax 

```visualbasic
Function ClearReceiveBuffer()
```

#### Example 

```visualbasic
UdpClient1.ClearReceiveBuffer()
```

# Connect

The command opens a connection to the server. It returns True on success or False if an error occurs. 

A function <u>[GetLastError](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[87](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#87)</u>, <u>[88](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#88)</u>, <u>[89](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#89)</u>, <u>[90](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#90)</u>. 

#### Syntax 

```visualbasic
Function Connect( 
  serveraddress As Text, 
  serverport As Long) 
As Boolean
```

#### Parameters 

serveraddress 

<u>[Text](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the address of a server. 

serverport 

<u>[Long](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Long)</u> It is the number of a UDP port of a server. 

#### Return value 

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. A function GetLastError returns the error number. 

#### Example 

```visualbasic
Var RecvData As ByteString[100] 

// connect to address "192.168.100.100", UDP port 3456 
If UdpClient1.Connect("192.168.100.100", 3456) = True Then 
  UdpClient1.Send(&S"SomeRequest") 
  // wait for an answer 
  If UdpClient1.Receive(RecvData) = True Then 
    // process answer 
  End If 
  UdpClient1.Disconnect() 
End If
```

# ConnectMulticast

The command opens a connection to the multicast address. It returns True on success or False if an error occurs. 

**Note:** Default Gateway (AdminWeb / Ip Settings ) must be set up to full multicast functionality. 

A function <u>[GetLastError](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[87](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#87)</u>, <u>[88](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#88)</u>, <u>[89](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#89)</u>, <u>[90](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#90)</u>, <u>[148](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#148)</u>. 

#### Syntax 

```visualbasic
Function ConnectMulticast( 
  mutlicastaddress As Text, 
  port As Long,
  Optional ttl As Byte:= 1,
  Optional loopenable As Boolean := True ) 
As Boolean
```

#### Parameters 

mutlicastaddress

<u>[Text](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the multicast address. 

port

<u>[Long](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Long)</u> It is the number of a UDP port. 

ttl

<u>[Byte](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Byte)</u> It is the time-to-live value of outgoing multicast packets for this socket.  It is very important for multicast packets to set the smallest TTL possible.  The default is 1 which means that multicast packets don't leave the local network. 

loopenable

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It determines whether sent multicast packets should be looped back to UDP channels in this unit.

#### Return value 

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. A function GetLastError returns the error number. 

#### Example 

```visualbasic
Var RecvData As ByteString[100] 

// connect to multicast address "239.1.1.1", UDP port 3456 
If UdpClient1.ConnectMulticast("239.1.1.1", 3456) = True Then 
  UdpClient1.Send(&S"SomeData") 
  UdpClient1.Disconnect() 
End If
```

# Disconnect

The command disconnects a client from the server. 

#### Syntax 

```visualbasic
Function Disconnect()
```

# GetMyIpAddress

The command retrieves the controller’s IP address and port of the connection. It returns True on success or False if an error occurs. 

A function GetLastError returns the error number that occurred while executing the command. The errors that can happen:  <u>[91](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#91)</u>, <u>[139](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#139)</u>. 

#### Syntax 

```visualbasic
Function GetMyIpAddress( 
  ByRef ipaddress As Text, 
  ByRef port As Long) 
As Boolean
```

#### Parameters 

ipaddress  

<u>[Text](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the IP address of the controller. 

port  

<u>[Long](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Long)</u> It is the port of the connection on the controller. 

#### Return value 

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. A function GetLastError returns the error number. 

# Receive

The command reads data from the communication channel. It returns True on success or False if an error occurs. 

A function <u>[GetLastError](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[91](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#91)</u>, <u>[92](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#92)</u>. 

#### Syntax 

```visualbasic
Function Receive( 
  ByRef data As ByteString, 
  Optional timeout As Time := :10.0) 
As Boolean
```

#### Parameters 

data 

<u>[ByteString](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#ByteString)</u> It is the data to be received. 

timeout 

<u>[Time](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Time)</u> Optional, defaults to 10 seconds. It is the timeout. 

#### Return value 

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. A function GetLastError returns the error number. 

#### Example 

```visualbasic
Var RecvData As ByteString[100] 

// connect to address "192.168.100.100", UDP port 3456 
If UdpClient1.Connect("192.168.100.100", 3456) = True Then 
  UdpClient1.Send(&S"SomeRequest") 
  // wait for an answer 
  If UdpClient1.Receive(RecvData) = True Then 
    // process answer 
  End If 
  UdpClient1.Disconnect() 
End If
```

# Send

The command sends data to the communication channel. It returns True on success or False if an error occurs. 

A function <u>[GetLastError](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The errors that can happen: <u>[91](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#91)</u><u>, </u><u>[356](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#356)</u>. 

> ⚠️ Since firmware version 21.90 onwards, this command may return False and set the error number to 356.
> ⚠️ 
> ⚠️ If the command returns False and set the error number to 356 (unable to send data via the UDP client), you must call the Disconnect function and then the Connect function to reconnect. This error may occur if the unit’s IP address has changed since the Connect function was called for the UDP client.

#### Syntax 

```visualbasic
Function Send(data As ByteString) As Boolean
```

#### Parameters 

data 

<u>[ByteString](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#ByteString)</u> It is the data to be sent. 

#### Return value 

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is True on success or False if an error occurs. A function GetLastError returns the error number. 

#### Example 

```
Var RecvData As ByteString[100] 

// connect to address "192.168.100.100", UDP port 3456 
If UdpClient1.Connect("192.168.100.100", 3456) = True Then 
  UdpClient1.Send(&S"SomeRequest") 
  // wait for an answer 
  If UdpClient1.Receive(RecvData) = True Then 
    // process answer 
  End If 
  UdpClient1.Disconnect() 
End If
```