---
title: "UdpServer (_UdpServer class)"
canonical: "https://docs.cuesystem.com/space/CVCHELP/522223662/UdpServer%20(_UdpServer%20class)"
format: markdown
---
> Macro (toc)

---

# ClearReceiveBuffer

The command removes received data from a receive buffer. 

#### Syntax 

```visualbasic
Function ClearReceiveBuffer()
```

#### Example 

```visualbasic
UdpServer1.ClearReceiveBuffer()
```

# Close

The command closes the communication. 

#### Syntax 

```visualbasic
Function Close()
```

#### Example 

```visualbasic
Var RecvPacket As ByteString[100] 
Var ClientIpAdd As Text[20] 
Var ClientPort As Long 

// open UDP port 3456 
If UdpServer1.Open(3456) Then 
  Do While True 
    If UdpServer1.Receive(RecvPacket, ClientIpAdd, ClientPort) Then 
      // process answer 
      // send reply 
      UdpServer1.Send(&S"Reply", ClientIpAdd, ClientPort) 
    End If 
  End Do 
  UdpServer1.Close() 
End If
```

# Open

The command opens a connection on a specified UDP port. 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>[95](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#95)</u>, <u>[96](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#96)</u>. 

#### Syntax 

```visualbasic
Function Open(udpport As Long) As Boolean
```

#### Parameters 

udpport 

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

#### 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 RecvPacket As ByteString[100] 
Var ClientIpAdd As Text[20] 
Var ClientPort As Long  

// open UDP port 3456 
If UdpServer1.Open(3456) Then 
  Do While True 
    If UdpServer1.Receive(RecvPacket, ClientIpAdd, ClientPort) Then 
      // process answer 
      // send reply 
      UdpServer1.Send(&S"Reply", ClientIpAdd, ClientPort) 
    End If 
  End Do 
  UdpServer1.Close() 
End If
```

# OpenMulticast

The command opens a connection to a specified multicast group to receive data packets. 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>[95](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#95)</u>, <u>[96](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#96)</u>, <u>[148](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#148)</u>. 

#### Syntax 

```visualbasic
Function OpenMulticast(
  mutlicastaddress as Text,
  udpport As Long) 
As Boolean
```

#### Parameters 

multicastipaddress 

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

udpport 

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

#### 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 RecvPacket As ByteString[100] 
Var SenderIpAdd As Text[20] 
Var SenderPort As Long  

// Join the multicast to receive data packets  
If UdpServer1.OpenMulticast(“239.1.1.1”,3456) Then 
  Do While True 
    If UdpServer1.Receive(RecvPacket, SenderIpAdd, SenderPort) Then 
      //… 
    End If 
  End Do 
  UdpServer1.Close() 
End If
```

# 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, 
  ByRef clientipaddress As Text, 
  ByRef clientport As Long, 
  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. 

clientipaddress 

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

clientport 

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

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 RecvPacket As ByteString[100] 
Var ClientIpAdd As Text[20] 
Var ClientPort As Long 

// open UDP port 3456 
If UdpServer1.Open(3456) Then 
  Do While True 
    If UdpServer1.Receive(RecvPacket, ClientIpAdd, ClientPort) Then 
      // process answer 
      // send reply 
      UdpServer1.Send(&S"Reply", ClientIpAdd, ClientPort) 
    End If 
  End Do 
  UdpServer1.Close() 
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>. 

#### Syntax 

```visualbasic
Function Send( 
  data As ByteString, 
  clientipaddress As Text, 
  clientport As Long) 
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. 

clientipaddress 

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

clientport 

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

#### 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 RecvPacket As ByteString[100] 
Var ClientIpAdd As Text[20] 
Var ClientPort As Long 

// open UDP port 3456 
If UdpServer1.Open(3456) Then 
  Do While True 
    If UdpServer1.Receive(RecvPacket, ClientIpAdd, ClientPort) Then 
      // process answer 
      // send reply 
      UdpServer1.Send(&S"Reply", ClientIpAdd, ClientPort) 
    End If 
  End Do 
  UdpServer1.Close() 
End If
```