• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Product
    • Design UI
    • Develop
    • Analyze and test
    • Deploy
    • Framework and toolbox
    • Compatibility
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Framework - CDP Core
  • UdpReceive
  • 4.11.14

UdpReceive Class

This class implements code to receive UDP/IP packets. More...

Header: #include <OSAPI/Network/UdpReceive.h>
  • List of all members, including inherited members

Public Types

enum Status { OK, ERR }

Public Functions

UdpReceive()
UdpReceive(IpAddress &ipLocal, const bool nonBlocking, const bool messageTransportPackets)
virtual ~UdpReceive()
Status Close()
void EnableReuse(bool bReuse)
virtual bool GetBroadcastSocket()
virtual unsigned int GetInterfaceNo()
int GetLastErrorCode()
virtual bool GetMessageTransportPackets()
bool GetNumberOfBytesPending(unsigned int *numberOfBytesPending)
IpAddress &GetRemoteIp()
SOCKET GetSocket()
Status Init(IpAddress &ipLocal, const bool nonBlocking, const bool messageTransportPackets)
bool IsOpen() const
bool IsSocketError()
bool IsSocketReadReady(int nTimeoutMs)
bool IsSocketWriteReady(int nTimeoutMs)
virtual bool MulticastJoin(IpAddress &multicastGroup, IpAddress &localInterface)
virtual bool MulticastLeave(IpAddress &multicastGroup, IpAddress &localInterface)
Status Open()
void PeekPacket(int *numberOfBytesReceived, int maxNumberOfBytes, void *data)
virtual void ReceivePacket(int *numberOfBytesReceived, int maxNumberOfBytes, void *data)
virtual bool SetMulticastTTL(int timeToLive)
virtual bool SetTypeOfServiceField(int tos)
Status Shutdown()

Detailed Description

This class implements code to receive UDP/IP packets.

The UdpReceive class can be initialized as blocking or non-blocking. When set to blocking, the receive functions will block until a message is received. When configured with the blocking flag set to true, the user has to be careful not to use the object in code that is executed by the CDPEngine, like state machines and message handlers.

The messageTransportPackets argument should be set to true if the packets to be sent are only of type MessageTransportPackets. This type of packet are used internally by the CDP protocol and rarely in user code.

The following demonstrates how to receive UDP messages from an application listening on port 5555. Usually, you will not place the code in a sequence like this, but rater execute the initialization in a constructor and have the while loop running in a thread function or remove the while and place the code in a CDP process/state function (if UdpReceive has been initialized as non blocking).

IpAddress ipLocal = EthernetManager::GetInstance()->GetNetworkInterface(0);
ipLocal.SetPort(5555);

UdpReceive udpReceiver;
if(udpReceiver.Init(ipLocal,false, false)==UdpReceive::ERR)
  CDPMessage("Error while initializing UDPReceive\n");

while(!Stopped())
{
  if(udpReceiver.IsSocketReadReady(0))
  {
    int bytesReceived = 0;
    int bufferSize = 1500;
    char buffer[bufferSize];
    udpReceiver.ReceivePacket(&bytesReceived, bufferSize-1, buffer);
    CDPMessage("My received data: %s \n",buffer);
  }
  OSAPISleep(100);
}

If the communication is not working as expected, you may increase the debug- level in your Application. Set the Debug property to 1 or 2 in the Properties- section, and you may get printouts in the Application Output console window giving indication of what might be wrong.

The sender code can be implemented using UdpSend.

See also UdpSend.

Member Type Documentation

enum UdpReceive::Status

This enum type specifies operation status:

ConstantValueDescription
UdpReceive::OK0Operation succeded
UdpReceive::ERR-1Operation failed

Member Function Documentation

UdpReceive::UdpReceive()

Constructor. If this constructor is used, Init() must be called.

UdpReceive::UdpReceive(IpAddress &ipLocal, const bool nonBlocking, const bool messageTransportPackets)

Constructor. By using this constructor, one doesn't have to call Init().

The port to listen on must be specified in the IpAddress.

Use the nonBlocking argument to specify whether or not the receive functions should block until a message is received. When configured with the blocking flag set to true, the user has to be careful not to use the object in code that is executed by the CDPEngine, like state machines and message handlers.

The messageTransportPackets argument should be set to true if the packets to be sent are only of type MessageTransportPackets. This type of packet are used internally by the CDP protocol and rarely in user code.

[virtual] UdpReceive::~UdpReceive()

Destructor deletes a socket statistics member (Close() should have been called before destructor).

Status UdpReceive::Close()

Close the connection to free port and socket.

void UdpReceive::EnableReuse(bool bReuse)

Enable reuse of socket, causes SO_REUSEADDR flag to be set in next Init-call.

[virtual] bool UdpReceive::GetBroadcastSocket()

Returns true if the socket only receives broadcast-messages , false otherwise.

[virtual] unsigned int UdpReceive::GetInterfaceNo()

Returns which interface number this socket receives packets from.

int UdpReceive::GetLastErrorCode()

If an UdpReceive-function indicates error, this function can be called immediately after to get the error code.

m_lastErrorCode is set to 0 in the start of every UdpReceive-function where m_lastErrorCode may get an error-code.

[virtual] bool UdpReceive::GetMessageTransportPackets()

Returns true if the packets to be received are only of type MessageTransportPacket, false otherwise.

This bool value is specified in constructor or Init().

bool UdpReceive::GetNumberOfBytesPending(unsigned int *numberOfBytesPending)

Returns false if error. Otherwise, numberOfBytesPending gives total number of bytes in the first UDP packet queued on the socket.

IpAddress &UdpReceive::GetRemoteIp()

Returns the remote ipaddress that we received data from last time.

SOCKET UdpReceive::GetSocket()

Returns the socket descriptor.

Status UdpReceive::Init(IpAddress &ipLocal, const bool nonBlocking, const bool messageTransportPackets)

Initializes connection with machine's IP address. The port to listen on must be specified in the IpAddress.

Use the nonBlocking argument to specify whether or not the receive functions should block until a message is received. When configured with the blocking flag set to true, the user have to be careful not to use the object in code that is executed by the CDPEngine, like state machines and message handlers.

The messageTransportPackets argument should be set to true if the packets to be sent are only of type MessageTransportPackets. This type of packet are used internally by the CDP protocol and rarely in user code.

bool UdpReceive::IsOpen() const

Returns true if socket is open, false if not.

bool UdpReceive::IsSocketError()

Determines if socket has error.

bool UdpReceive::IsSocketReadReady(int nTimeoutMs)

Determines if socket can be read from (data available). Returns false if not.

bool UdpReceive::IsSocketWriteReady(int nTimeoutMs)

Determines if socket can be written to. Returns false if not.

[virtual] bool UdpReceive::MulticastJoin(IpAddress &multicastGroup, IpAddress &localInterface)

Join a multicast group.

[virtual] bool UdpReceive::MulticastLeave(IpAddress &multicastGroup, IpAddress &localInterface)

Leave a multicast group.

Status UdpReceive::Open()

Create a socket for receiving data.

This method is used to create a socket for communication. If one is already open it will be used. Some configuration of the socket is also done.

void UdpReceive::PeekPacket(int *numberOfBytesReceived, int maxNumberOfBytes, void *data)

Peek into packetdata without removing it. Call ReceivePacket() to remove.

The object provided in numberOfBytesReceived will be set to 0 if an error occurred.

[virtual] void UdpReceive::ReceivePacket(int *numberOfBytesReceived, int maxNumberOfBytes, void *data)

Waits for a packet to be received, and store the received packet in the buffer pointed to by "data".

The maximum buffer size is given by the second parameter. The first parameter will be set to the actual number of bytes received. Will be set to 0 if an error occurred.

[virtual] bool UdpReceive::SetMulticastTTL(int timeToLive)

Set TimeToLive for multicast (i.e. number of network hops).

[virtual] bool UdpReceive::SetTypeOfServiceField(int tos)

Set the Type Of Service field (8bits) for outgoing packets.

For more info, see RFC 1349. Returns false if failure.

  • 0x04 (IPTOS_RELIABILITY): QoS level 2
  • 0x08 (IPTOS_THROUGHPUT) : QoS level 2
  • 0x10 (IPTOS_LOWDELAY): QoS level 2
  • 0xF1: QoS level 1 (lowest priority level)
  • 0xF2: QoS level 2
  • 0xF3: QoS level 3
  • 0xF4: QoS level 4 (highest priority level)

Status UdpReceive::Shutdown()

Calls shutdown() on the socket.

Useful when another thread is blocked waiting on a socket (e.g. calling ReceivePacket()). Shutdown() will unblock the thread. Use Close() shutdown and free resources.

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

    © Copyright 2022 CDP Technologies. Privacy and cookie policy.

    Return to top