UdpSend Class
UdpSend UDP/IP socket. Sends generic udp packets to a specified port on a specified IP address. More...
Header: | #include <OSAPI/Network/UdpSend.h> |
Public Types
enum | Status { OK, ERR } |
Public Functions
UdpSend() | |
UdpSend(IpAddress &ipRemote, IpAddress &ipLocal, bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false) | |
UdpSend(IpAddress &ipLocal, bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false) | |
virtual | ~UdpSend() |
Status | Close() |
virtual void | EnableBroadcast() |
void | EnableReuse(bool bReuse) |
double | GetBlockingTimer() |
virtual bool | GetBroadcastSocket() |
virtual unsigned int | GetInterfaceNo() |
int | GetLastErrorCode() |
double | GetMaxBlockingTimer() |
virtual bool | GetMessageTransportPackets() |
SOCKET | GetSocket() |
Status | Init(IpAddress &ipRemote, IpAddress &ipLocal, const bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false) |
Status | Init(IpAddress &ipLocal, const bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false) |
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) |
void | ResetMaxBlockingTimer() |
Status | SendPacket(const void *buf, const int size) |
Status | SendPacketTo(const void *buf, const int size, IpAddress &destination) |
virtual bool | SetMulticastTTL(int timeToLive) |
Status | SetTargetIpAddress(IpAddress &ipAddress) |
virtual bool | SetTypeOfServiceField(int tos) |
Protected Functions
Status | Open() |
bool | SendingUnicast(const void *buf, const IpAddress &dest) |
Detailed Description
UdpSend UDP/IP socket. Sends generic udp packets to a specified port on a specified IP address.
By using the SendPacketTo(...) method it is not necessary to intialize with remote ip address.
The following demonstrates how to send a UDP message to a target application running on localhost, listening for messages on port 5555. We do not specify a port for the local IP object as any available port will do. This selects default value 0 which means use any free port. The local IP address has to be valid for the machine or INADDR_ANY can be used.
IpAddress ipLocal = static_cast<unsigned int>(INADDR_ANY); IpAddress ipTarget = "127.0.0.1"; ipTarget.SetPort(5555); char data[] = "TextMessage"; UdpSend udpSender(ipTarget,ipLocal); if(udpSender.IsOpen()) udpSender.SendPacket(static_cast<char*>(data), sizeof(data)); udpSender.Close();
To broadcast a message to all applications running in the same subnet, the above could've been modified to generate a broadcast address. The broadcast address can be generated by subnet | ~subnet_mask or in code:
IpAddress ipLocal = EthernetManager::GetInstance()->GetNetworkInterface(0); unsigned int invMask = ~static_cast<unsigned long>(ipLocal.SubnetMask()); IpAddress ipTarget = ipLocal.Subnet() | invMask; ... udpSender.EnableBroadcast();
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 receiver code can be implemented using UdpReceive or UdpSendReceive.
See also UdpReceive.
Member Type Documentation
enum UdpSend::Status
This enum type specifies operation status:
Constant | Value | Description |
---|---|---|
UdpSend::OK | 0 | Operation succeded |
UdpSend::ERR | -1 | Operation failed |
Member Function Documentation
UdpSend::UdpSend()
Constructor
UdpSend::UdpSend(IpAddress &ipRemote, IpAddress &ipLocal, bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false)
Constructor. By using this constructor, there is no need to call Init().
The remote port must be set on the ipRemote
object.
Set nonBlocking to true if wanting the internal socket to get initialized as non blocking.
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.
UdpSend::UdpSend(IpAddress &ipLocal, bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false)
Construct without remote address.
[virtual]
UdpSend::~UdpSend()
Virtual destructor.
Status UdpSend::Close()
Close the connection (socket) for sending data.
[virtual]
void UdpSend::EnableBroadcast()
Enable broadcast acceptance.
void UdpSend::EnableReuse(bool bReuse)
Enable reuse of socket, causes SO_REUSEADDR flag to be set in next Init-call.
double UdpSend::GetBlockingTimer()
Get the last measured BlockingTimeout
Only updated if ::debug is at least DEBUGLEVEL_NORMAL.
[virtual]
bool UdpSend::GetBroadcastSocket()
Returns true
if the socket only sends broadcast-messages, false
otherwise.
[virtual]
unsigned int UdpSend::GetInterfaceNo()
Returns which interface number this socket sends packets from.
int UdpSend::GetLastErrorCode()
If an UdpSend-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 UdpSend-function where m_lastErrorCode may get an error-code.
double UdpSend::GetMaxBlockingTimer()
Get the max BlockingTimeout
Only updated if ::debug is at least DEBUGLEVEL_NORMAL.
[s] dMaxBlockingTimer filtered like this: no filtereng when value increases, decreasing with 1% of difference when value decreases.
[virtual]
bool UdpSend::GetMessageTransportPackets()
Returns true
if the packets to be sent are only of type MessageTransportPacket, false
otherwise. This bool value is specified in constructor or Init().
SOCKET UdpSend::GetSocket()
Returns the socket descriptor.
Status UdpSend::Init(IpAddress &ipRemote, IpAddress &ipLocal, const bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false)
Initializes the socket with IP-address/port. May be called several times to re-initialize.
The remote port must be set on the ipRemote
object. For the local ip address, any available port can usually be used. If any local ip-address may be used as source address, specify INADDR_ANY as ipLocal. However, if having multiple network interfaces, use the ip of the wanted interface.
Set nonBlocking to true if wanting the internal socket to get initialized as non blocking.
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.
Status UdpSend::Init(IpAddress &ipLocal, const bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false)
Initialize without remote address.
bool UdpSend::IsOpen() const
Determines if the connection is open and ready to send.
bool UdpSend::IsSocketError()
Determines if socket has error.
bool UdpSend::IsSocketReadReady(int nTimeoutMs)
Determines if socket can be read from (data available). Returns false
if not.
bool UdpSend::IsSocketWriteReady(int nTimeoutMs)
Determines if socket can be written to. Returns false
if not.
[virtual]
bool UdpSend::MulticastJoin(IpAddress &multicastGroup, IpAddress &localInterface)
Join a multicast group.
[virtual]
bool UdpSend::MulticastLeave(IpAddress &multicastGroup, IpAddress &localInterface)
Leave a multicast group.
[protected]
Status UdpSend::Open()
Open connection for sending (Create socket).
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 UdpSend::ResetMaxBlockingTimer()
Reset the max BlockingTimeout.
Status UdpSend::SendPacket(const void *buf, const int size)
Send a packet.
Status UdpSend::SendPacketTo(const void *buf, const int size, IpAddress &destination)
Send a packet to a specified destination ipaddress.
[protected]
bool UdpSend::SendingUnicast(const void *buf, const IpAddress &dest)
[virtual]
bool UdpSend::SetMulticastTTL(int timeToLive)
Set TimeToLive for multicast (i.e. number of network hops).
Status UdpSend::SetTargetIpAddress(IpAddress &ipAddress)
Set/change the IP-address to which the packet is sent.
[virtual]
bool UdpSend::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)
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.